Hey
I'm in an AP Computer Science class working on classes. We have been working on a project that is a date class that does certain things like setYear, getMonth, getDay, etc. I have gotten all of those to work, except for one. The last one is a boolean method to test if the old date is equal to the new date.
It is public boolean equals(Date otherDate)

I can't get anything to work. Below is my class code:

public class Date
{
   private int mnth;
   private int dy;
   private int yr;
 
   public Date()
   {
       mnth = 1;
       dy = 1;
       yr = 1;
   }
   public Date(int month, int day, int year)
   {
       mnth = month;
       dy = day;
       yr = year;
   }
   
   /**
    * Accessors
    */
   
   /**
    * Modifiers
    */
   public int getMonth()
   {
       return mnth;
   }
   public int getYear()
   {
       return yr;
   }
   public int getDay()
   {
       return dy;
   }
   public String getMonthName()
   {
        String str = new String();
        
        if (mnth==1)
            return("January");
        if (mnth==2)
            return("February");
        if (mnth==3)
            return("March");
        if (mnth==4)
            return("April");
        if (mnth==5)
            return("May");
        if (mnth==6)
            return("June");
        if (mnth==7)
            return("July");
        if (mnth==8)
            return("August");
        if (mnth==9)
            return("September");
        if (mnth==10)
            return("October");
        if (mnth==11)
            return("November");
        if (mnth==12)
            return("December");
        return "You input an incorrect month.";   
   }
   public void setYear(int newYear)
   {
       yr = newYear;
   }
   public void setEqual(int newMonth, int newDay, int newYear)
   {
       mnth = newMonth;
       dy = newDay;
       yr = newYear;
   }
   public boolean equals(Date otherDate)
   {
       
   }
}

Hope you can help me out
Thanks!

Forget the code for a moment... how (in English) would you check if two dates were equal / the same?

I have been going along with the thinking of do if and else statements. Like: if (oldMonth==newMonth && so long for day and year) then boolean equals true.

Everything I try doesn't work.

if statements would be OK. Just like you said, except instead of oldMonth and newMonth it will be the month of this date and the month (hint: getMonth()) of otherDate.
You can't be far from a decent answer, but if you get stuck go back to explaining it in English before attempting to write code - that way you focus on getting the logic right rather than messing about with different random bits of Java syntax.

if statements would be OK. Just like you said, except instead of oldMonth and newMonth it will be the month of this date and the month (hint: getMonth()) of otherDate.
You can't be far from a decent answer, but if you get stuck go back to explaining it in English before attempting to write code - that way you focus on getting the logic right rather than messing about with different random bits of Java syntax.

Thanks
I tried something like this:

if (mnth==getMonth() and same for day and year)
return true;
else
return false;

Now i'm just working on a way to test it because we have to make a tester

Edited 5 Years Ago by jhellr13: n/a

have you tried
if ( mnth == otherDate.getMonth())

for as far as I can see, your code as you describe it should be returning true.

have you tried
if ( mnth == otherDate.getMonth())

for as far as I can see, your code as you describe it should be returning true.

I have tried what you said, but it gives a syntax error. I have been working with this:
if (mnth==getMonth() and same for day and year)
return true;
else
return false;

I believe this correct, but I can't find a way to test it in my tester

no, that's not what I said..
mnth == getMonth() will always return true, since they are both referring to variables of the same instance.
could you paste your entire code (including the code you use to call your date class) here?

no, that's not what I said..
mnth == getMonth() will always return true, since they are both referring to variables of the same instance.
could you paste your entire code (including the code you use to call your date class) here?

Okay.

Here is my Date class:

public class Date
{
   private int mnth;
   private int dy;
   private int yr;
 
   public Date()
   {
       mnth = 1;
       dy = 1;
       yr = 1;
   }
   public Date(int month, int day, int year)
   {
       mnth = month;
       dy = day;
       yr = year;
   }
   
   
   public int getMonth()
   {
       return mnth;
   }
   public int getYear()
   {
       return yr;
   }
   public int getDay()
   {
       return dy;
   }
   public String getMonthName()
   {
        String str = new String();
        
        if (mnth==1)
            return("January");
        if (mnth==2)
            return("February");
        if (mnth==3)
            return("March");
        if (mnth==4)
            return("April");
        if (mnth==5)
            return("May");
        if (mnth==6)
            return("June");
        if (mnth==7)
            return("July");
        if (mnth==8)
            return("August");
        if (mnth==9)
            return("September");
        if (mnth==10)
            return("October");
        if (mnth==11)
            return("November");
        if (mnth==12)
            return("December");
        return "You input an incorrect month.";   
   }
   public void setYear(int newYear)
   {
       yr = newYear;
   }
   public void setEqual(int newMonth, int newDay, int newYear)
   {
       mnth = newMonth;
       dy = newDay;
       yr = newYear;
   }
   public boolean equals(Date otherDate)
   {
      if (mnth==getMonth() && dy==getDay() && yr==getYear()) //need to change all of this
      return true;
      else
      return false;
   }
}

And here is my tester class:

public class DateTester
{
    public static void main(String [] args)
    {
        System.out.print("\f");
        System.out.println("Starting the Date Program");
        
        Date one = new Date(3, 17, 2011);
        
        //Tests getMonth()
        System.out.println();
        System.out.println("The month provided is: " + one.getMonth());
        
        //Tests getDay()
        System.out.println();
        System.out.println("The day provided is: " + one.getDay());
        
        //Tests getYear()
        System.out.println();
        System.out.println("The year provided is: " + one.getYear());
        
        //Tests getMonthName
        System.out.println();
        System.out.println("The month provided in words is: " + one.getMonthName());
        
        //Tests setYear
        System.out.println();
        one.setYear(2010);
        System.out.println("The year is now: " + one.getYear());
        
        //Tests setEqual
        System.out.println();
        one.setEqual(4, 5, 2006);
        System.out.println("The new date is: " + one.getMonth() +"/" + one.getDay() +"/" + one.getYear());
        
        //Tests boolean equals
        System.out.println();
        if (true)
        System.out.println("The dates are the same.");  //I think this will accurately test it, but not sure
        if (false)
        System.out.println("The dates are NOT the same.");
        
        
    }
}

Hope this helps

yes, no, that doesn't really help, since you still have the same logical errors in your code

for instance:

public boolean equals(Date otherDate)
{
if (mnth==getMonth() && dy==getDay() && yr==getYear()) //need to change all of this
return true;
else
return false;
}

let's just talk about comparing the months:

you are never looking at the value of month that is stored in the Object with which you're trying to compare it.
1. mnth => this references to the current value of the mnth variable of the instance you are in
2. getMonth() => this also references to the current value of the mnth variable of the instance you are in

getMonth() is a method, unless you actually call it on an object, the compiler assumes you mean that method within the same class for the current instance

what you want (and I'm only showing it for the month value) is:

public boolean equals(Date otherDate)
{
return mnth==otherDate.getMonth();
}

in this code:
1. mnth references to the value of the mnth variable of the current instance
2. otherDate.getMonth() refers to the value of the mnth value of the otherDate instance of this class.

also, I'll explain why this test is completely useless:

one.setEqual(4, 5, 2006);
System.out.println("The new date is: " + one.getMonth() +"/" + one.getDay() +"/" + one.getYear());
 
//Tests boolean equals
System.out.println();
if (true)
System.out.println("The dates are the same."); 
//I think this will accurately test it, but not sure 
if (false)
System.out.println("The dates are NOT the same.");

1. one.setEqual(4, 5, 2006);
here, you are not creating a new instance, you are just changing the values of your original object, so, even if the values were not identical, the very weird equals call would always return true


2. and here I don't understand how you missed this:
if (true)
System.out.println("The dates are the same.");
//I think this will accurately test it, but not sure
if (false)
System.out.println("The dates are NOT the same.");

your instructions will only run if the expression in the if equals to 'true', so, you could have
if ( 1 == (2-1)) System.out.println("this will run");
if ( true ) System.out.println("this will run");
simply because: true = true

and when you say:
if ( false )
well, the chances of false being recognized as true are pretty slim.

So i'll try the code you gave:

public boolean equals(Date otherDate)
{
return mnth==otherDate.getMonth();
}

Would i do one for month, day, and year, or can i just do what you have above with && for day and year?

Also, how would you fix setEquals()? I mean it works so I don't see the problem. It's supposed to just change the date to whatever you set it to, which was 4, 5, 2006 in this case.

yes, no, that doesn't really help, since you still have the same logical errors in your code

for instance:

public boolean equals(Date otherDate)
{
if (mnth==getMonth() && dy==getDay() && yr==getYear()) //need to change all of this
return true;
else
return false;
}

let's just talk about comparing the months:

you are never looking at the value of month that is stored in the Object with which you're trying to compare it.
1. mnth => this references to the current value of the mnth variable of the instance you are in
2. getMonth() => this also references to the current value of the mnth variable of the instance you are in

getMonth() is a method, unless you actually call it on an object, the compiler assumes you mean that method within the same class for the current instance

what you want (and I'm only showing it for the month value) is:

public boolean equals(Date otherDate)
{
return mnth==otherDate.getMonth();
}

in this code:
1. mnth references to the value of the mnth variable of the current instance
2. otherDate.getMonth() refers to the value of the mnth value of the otherDate instance of this class.

also, I'll explain why this test is completely useless:

one.setEqual(4, 5, 2006);
System.out.println("The new date is: " + one.getMonth() +"/" + one.getDay() +"/" + one.getYear());
 
//Tests boolean equals
System.out.println();
if (true)
System.out.println("The dates are the same."); 
//I think this will accurately test it, but not sure 
if (false)
System.out.println("The dates are NOT the same.");

1. one.setEqual(4, 5, 2006);
here, you are not creating a new instance, you are just changing the values of your original object, so, even if the values were not identical, the very weird equals call would always return true


2. and here I don't understand how you missed this:
if (true)
System.out.println("The dates are the same.");
//I think this will accurately test it, but not sure
if (false)
System.out.println("The dates are NOT the same.");

your instructions will only run if the expression in the if equals to 'true', so, you could have
if ( 1 == (2-1)) System.out.println("this will run");
if ( true ) System.out.println("this will run");
simply because: true = true

and when you say:
if ( false )
well, the chances of false being recognized as true are pretty slim.

well, you would have something like:

public boolean equals(Date otherDate)
    {
    return mnth==otherDate.getMonth() && day == otherDate.getDay();
    }

as for the setEquals mehtod.. why would you want one anyway? you already have your setters that do that for you.
the values of your variables already were 4, 5 en 2006, so why did you call it anyway?

if you want another instance of the class with it's own values, for which the calling the equals method would actually make sense, you have to create a new instance.

so, again, you should run something like

Date newDate = new Date(4, 5, 2006);
Date dateToVerify = new Date(8, 7, 2005);

so, just drop the setEquals method, and run the equals (called on your original Date object, with the above two as parameters).
the first one should return true, the second one should return false, not because they are the exact same instance or object, but, because the values of the variables which you have to compare are equal to each other.

well, you would have something like:

public boolean equals(Date otherDate)
    {
    return mnth==otherDate.getMonth() && day == otherDate.getDay();
    }

as for the setEquals mehtod.. why would you want one anyway? you already have your setters that do that for you.
the values of your variables already were 4, 5 en 2006, so why did you call it anyway?

if you want another instance of the class with it's own values, for which the calling the equals method would actually make sense, you have to create a new instance.

so, again, you should run something like

Date newDate = new Date(4, 5, 2006);
Date dateToVerify = new Date(8, 7, 2005);

so, just drop the setEquals method, and run the equals (called on your original Date object, with the above two as parameters).
the first one should return true, the second one should return false, not because they are the exact same instance or object, but, because the values of the variables which you have to compare are equal to each other.

Yeah i see what you mean about the setEquals method. I only need it because it was one of the things required for the assignment. It is pretty dumb when you think about it.

well ... maybe you didn't understand that part of the assignment correctly? maybe you're supposed to do something different than just perform the task of the setters.

This article has been dead for over six months. Start a new discussion instead.