Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Date Class It should store a month, day, and year, report date (toString())

ID: 674407 • Letter: J

Question

Java Date Class

It should store a month, day, and year, report date (toString()), date setting (constructors and getters/setters), as well as some basic error detection (for example, all month values should be between 1-12, if represented as an integer). All month values should be in between 1-12 , all day values should be in between 1-31, all year values should be positive.

- Declare a variable for month, day, and year

- Provide getter/setter methods for each of the variables above

- public Date() {} // This is the default no-arg constructor.

- public Date(int m, int d, int y);

- public Date(Date other);

- public String toString(); //report on your date

- public boolean equals(Object other); // compare "this" to "that"

Sample Output:

a is :0.0.0
b is :2.1.2030
c is :2.1.2030
B and A are equal : false B and C are equal : true

Explanation / Answer

class Date
{
   int day, month, year;
   Date()
   {
       day=0;
       month=0;
       year=0;
   }

   Date(int d, int m, int y)
   {
       day=d;
       month=m;
       year=y;
   }

   Date(Date other)
   {
       other.day=2;
       other.month=1;
       other.year=2030;
   }

   public String toString()
   {
       return String.valueOf

(this.day)+"."+String.valueOf(this.month)+"."+String.valueOf

(this.year);
      
   }
  
   public boolean equals(Date other)
   {
       if(other.day==this.day && other.month

== this.month && other.year==this.year)
       {
           return true;  
       }
       else
       {
           return false;
       }
   }
}      
      
class date_comp
{
   public static void main(String args[])
   {
       Date o=new Date();
       Date o1=new Date(2,1,2030);
       Date o2=new Date();
       o2=o2;
       String s1=o.toString();
       System.out.println("a is :"+s1);
      
       o2.toString();
       String s2=o1.toString();
       System.out.println("a is :"+s2);
       boolean ans=o.equals(o1);
       if(ans)
       {
           System.out.println("B and A

are equal : true");
       }
       else
       {
           System.out.println("B and A

are equal : false");
       }


  
   }
}

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote