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

Exercise 3: Consider a class Time that represents a time of day. It has attribut

ID: 3568188 • Letter: E

Question

Exercise 3: Consider a class Time that represents a time of day. It has attributes for the hour and minute. The hour value ranges from 0 to 23. where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. a. Write a default constructor that initializes the time to 0 hours. 0 minutes b. Write a private method is Valid (hour, minute that returns true if the green hour and minute values are in the appropriate range. c. Write a method setTime(hour, minute) that sets the time if the given values are valid. d. Write another method setTime( hour, minute, is AM) that sets the time if the given values are valid. The given hour should be in the range I to 12. The parameter is Am is true if the time is an a.m. time and false otherwise.

Explanation / Answer

class Time
   {
   int hour;
   int minute;
  
   Time()
   {
   hour=0;
   minute=0;
   }
  
   private boolean isValid(hour,minute)
   {
   boolean valid=true;
   if((hour>=0 && hour<=23) && (minute>=0 && minute<=59))  
       valid=true;
   else
       valid=false;
      
       return valid;
   }
  
   void setTime(hour,minute)
   {
   boolean valid=true;
   if((hour>=0 && hour<=23) && (minute>=0 && minute<=59))  
       valid=true;
   else
       valid=false;
      
   if(valid==true)
       {
       this.hour=hour;
       this.minute=minute;
       }
   }
  
   void setTime(hour,minute,isAM)
   {
   boolean valid=true;
   if((hour>=1 && hour<=12) && (minute>=0 && minute<=59) && isAM==true)  
       valid=true;
   else if((hour>=13 && hour<=23) && (minute>=0 && minute<=59) && isAM==false)  
       valid=true;
      
   else
       valid=false;
      
   if(valid==true)
       {
       this.hour=hour;
       this.minute=minute;
       }
   }