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

Help modify Java Date class. Could someone help me get my Date class running? It

ID: 675503 • Letter: H

Question

Help modify Java Date class.

Could someone help me get my Date class running? It should store month, day, year, and have some error detection (months can't be larger than 12, days 31, etc.) as well as report the date. Below, I've provided the Date class, as well as the driver main code. Thanks!

Here is the desired 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

////////////////////////////////// Date class

public class Date {

private int month, day, year;
  
public Date() {
}

public Date( int m, int d, int y ) {

if ( ( 0 < m ) && ( m <= 12 ) ) {
setMonth( m );
} else {
System.out.println( "Error: Month must be between 1 and 12" );
}
if ( ( 0 < d ) && ( m <= 31 ) ) {
setMonth( m );
} else {
System.out.println( "Error: Day must be between 1 and 31" );
}

setYear( y );
}

public String toString() {
return month + "." + day + "." + year;
}
  
public boolean equals( Object other ) {
Date that = ( Date )other;   
if ( ( this.month == that.month ) && ( this.day == that.day )
&& ( this.year == that.year ) ) {
return true;
} else {
return false;
}
}
  
public void setMonth( int m ) {
this.month = m;
}
  
public int getMonth() {
return month;
}
  
public void setDay( int d ) {
this.day = d;
}
  
public int getDay() {
return day;
}
  
public void setYear( int y ) {
this.year = y;
}
  
public int getYear() {
return year;
}
}

//////////////////////// Driver code

public static void main( String[] args ) {

Date a = new Date();
       Date b = new Date(1,2,2030);
       Date c = new Date(b);
       System.out.println("a is :"+a.toString());
       System.out.println("b is :"+b.toString());
       System.out.println("c is :"+c.toString());
       a.Date(5,5,2010);
       System.out.println("B and A are equal : " + b.equals(a));
       System.out.println("B and C are equal : " + b.equals(c));

}

Explanation / Answer


public class Date {

    private int month, day, year;
  
    public Date() {
    }

    public Date( int m, int d, int y ) {

        if ( ( 0 < m ) && ( m <= 12 ) ) {
            setMonth( m );
        } else {
            System.out.println( "Error: Month must be between 1 and 12" );
        }
        if ( ( 0 < d ) && ( d <= 31 ) ) {
            setDay( d );
        } else {
            System.out.println( "Error: Day must be between 1 and 31" );
        }

        setYear( y );
    }
  
    public Date(Date d) {
       this.month = d.getMonth();
       this.day = d.getDay();
       this.year = d.getYear();
    }

    public String toString() {
        return day + "." + month + "." + year;
    }
  
    public boolean equals( Object other ) {
        Date that = ( Date )other;  
        return ( this.month == that.getMonth() ) && ( this.day == that.getDay() )
                         && ( this.year == that.getYear() ) ;
    }
  
    public void setMonth( int m ) {
        this.month = m;
    }
  
    public int getMonth() {
        return month;
    }
  
    public void setDay( int d ) {
        this.day = d;
    }
  
    public int getDay() {
        return day;
    }
  
    public void setYear( int y ) {
        this.year = y;
    }
  
    public int getYear() {
        return year;
    }
}

--------------------------------------------------------------------------------------------------------------------------------

public static void main( String[] args ) {

        Date a = new Date();
        Date b = new Date(1,2,2030);
        Date c = new Date(b);
        System.out.println("a is :"+a.toString());
        System.out.println("b is :"+b.toString());
        System.out.println("c is :"+c.toString());
        //a.Date(5,5,2010);
        System.out.println("B and A are equal : " + b.equals(a));
        System.out.println("B and C are equal : " + b.equals(c));

}