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

JAVA Need help explaining this I have tried different code not getting it to wor

ID: 3863255 • Letter: J

Question

JAVA

Need help explaining this I have tried different code not getting it to work.

package hw6;

public class DateTime implements Comparable<DateTime> {
   private final int month ,day, year;
   private final int hour, minute;
   private final boolean am;
  
   public DateTime(int m, int d, int y, int h, int mi, boolean a) {
       month = m;
       day = d;
       year = y;
       hour = h;
       minute = mi;
       am = a;
   }
  
   // returns a negative integer if this date is earlier than the date "o"
   // returns zero if this date is the same date as "o"
   // returns a positive integer if this date is later than the date "o"
   public int compareTo(DateTime o) {
       return 0;
   }

  
   public String toString() {
       return hour + ":" + minute + (am ? "am" : "pm") + " " + month + "/" + day + "/" + year;
   }
}

Explanation / Answer

I think you want to make object of DateTime with passing month ,day , year , hour, minute and am or pm .

you used boolean true or false for am and pm and after making this object you used toString() method and overrided it .

in this toString() method you returned a string after evaluation of this expression   - return hour + ":" + minute + (am ? "am" : "pm") + " " + month + "/" + day + "/" + year;

because you passed true boolean value for am and false for pm , then here in ternary operator you checked true or false . then am or pm will be add after string and return string will be like this 12:40am 11/10/1996.

there is no problem in this . But if you want to compare two dates , then you use this method :

The java.util.Date.compareTo(Date anotherDate) method compares two Dates .

anotherDate -- date to be compared to

firstly according to you created your DateTime object

DateTime date1=new DateTime(11,10,1996,12,40, true);

DateTime date2=new DateTime(23,5,1998,12,40, false);

and compared them .

besides it , there are many ways in java to compare two dates .

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

try this , this is the code to check if given date-time is larger then the Present Date-time.Below method takes perticular date-time string as argument and returns true if provided date-time is larger then the present date-time