Comparing two objects always returns false

I’m trying to create a simple date class. My professor also wants us to include our own .equals method in the date class which should compare two objects. My problem is my method returns false unless I compare the exact same object, even if their values are the same.

Here is my driver:

public class Lab3Driver {

    public static void main(String[] args) {
     Date theDate = new Date(6, 30, 1995);
     Date anotherDate = new Date(6, 30, 1995);
     System.out.println(theDate.equals(anotherDate));
     System.out.println(theDate);
     System.out.println(anotherDate);
    }
}

Here is my date class:

public class Date {
    private int month;
    private int day;
    private int year;
    public Date() // default no arg constructor
    {
        this.month = 1; // set to date I completed this class, for fun.
        this.day = 26;
        this.year = 2019;
    }

    public Date(int m, int d, int y) // normal constructor in case you want to initialize variables upon object declaration
    {
        this.month = m;
        this.day = d;
        this.year = y;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month)
    {
        if (month >= 1 && month <= 12) // if else that checks and makes sure months are between 1 and 12
        {
            this.month = month;
        }
        else
        {
            System.out.println("Invalid month input. Months are between 1 and 12.");
        }
    }

    public int getDay()
    {
        return day;
    }

    public void setDay(int day)
    {
        if (day >= 1 && day <= 31) // if else that checks and makes sure days are between 1 and 31
        {
            this.day = day;
        }
        else
        {
            System.out.println("Invalid day input. Days are between 1 and 31.");
        }
    }

    public int getYear()
    {
        return year;
    }

    public void setYear(int year) // year can be set to anything, in the case that this program is used for something
    {                             // other than the present day, as in a reference to the past or future
        this.year = year;
    }

    public String toString() // to string in order to print out the date that is stored
    {
        String theDate = "The date is: " + this.month + "/" + this.day + "/" + this.year;
        return theDate;
    }

    public boolean equals(Object that) // compares two objects and checks for null/type casting
    {
        if (this == that)
            return true;
        else if(that == null || that.getClass()!= this.getClass())
        {
            System.out.println("Null or type casting of argument.");
            return false;
        }
        else
            return false;
    }

Something with this method is creating a problem I think:

 public boolean equals(Object that) // compares two objects and checks for null/type casting
    {
        if (this == that)
            return true;
        else if(that == null || that.getClass()!= this.getClass())
        {
            System.out.println("Null or type casting of argument.");
            return false;
        }
        else
            return false;
    }

Answer

if (this == that)

This line does not compare the objects. This only verifies if your object is in the same memory space, basically asking if it is exactly the same object (pointing to the same place).

If you want to compare two different objects, two different instances like

Date theDate = new Date(6, 30, 1995);
Date anotherDate = new Date(6, 30, 1995);

then you’ll have to add more lines of code that check each value in each variable in each of the objects, or override the ‘ == ‘ method to make it compare the values.

Leave a Reply

Your email address will not be published. Required fields are marked *