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

ASS1gnment: 1. Create a java class with three private integer data fields naned

ID: 3607642 • Letter: A

Question

ASS1gnment: 1. Create a java class with three private integer data fields naned hours, minutes, and seconds, and implement the following public methods: TimeTypeCO no argument contructor. sets al1 private data to o. TimeType(int, int, int) he three parameters. The first paraneter represents hours, the second minutes, and the third seconds. If any o parameters are invalid, set all private data to 0. TimeType(TimeType) he rimeType contructor function. Initializes the private data to the values stored in t parameter Returns the time stored in the object in the format HH:MM:SS AM/PM boolean set(int,int,int) e in the obiect according to the three parameters. The first parameter Sets the tim represents hours, the second minutes, and the third seconds. If any of the parameters are invalid, does nothing and returns false. otherwise, set the private data according to the three parameters and returns true. void IncreaseO Increases the time in the object by 1 SECOND. Adjust the minutes and hours as necessary void DecreaseO Decreases the time in the object by 1 SECOND. Adjust the minutes and hours as necessary e TimeType class. This application, named TestTime, should do the following and display its results using a Scanner object: 2. After you have created and compiled the Ti - create a TimeType object called timel using the default constructor. - Create a TimeType object called time2 initialized to 23 hours, 59 minutes, and 58 seconds - create a TimeType object called time3 initialized with the values from the time2 object. - write the values of timel, time2, and time3 on separate 1ines. - Attempt to set the time in timel to 25 hours, 0 minutes, and o seconds. If successful, display the time in timel otherwise display the message "Invalid Time". - Create a loop that will increase and write the time in time2 10 times. - Create a loop that wi11 decrease and write the time in time2 10 times.

Explanation / Answer

import java.io.*;

class TimeType{
    private int hr;
    private int min;
    private int sec;

    public TimeType(){
        hr = 0;
        min = 0;
        sec = 0;
    }
    public TimeType(int h, int m, int s){
        if (h<25 && m <60 && s < 60){
           hr = h;
           min = m;
           sec = s;
        }
        else {
           System.out.println("Invalid Time");
        }
    }
    public int getHr(){
          return hr;
    }
    public int getMin(){
          return min;
    }
    public int getSec(){
          return sec;
    }
    public boolean set(int h, int m,int s){
       if (h<25 && m <60 && s < 60){
           hr = h;
           min = m;
           sec = s;
           return true;
        }
        else {
           System.out.println("Invalid Time");
           return false;
        }
    }
    public TimeType(TimeType t){
         hr = t.getHr();
         min = t.getMin();
         sec = t.getSec();
    }
    public String toString(){
        String s = "";
       
        if (hr > 12){
            s = s + Integer.toString(hr-12) + ":" + Integer.toString(min) + ":" + Integer.toString(sec) + " PM";
           return s;
        }
        else {
           
            s = s + Integer.toString(hr) + ":" + Integer.toString(min) + ":" + Integer.toString(sec) + " AM";
           return s;
        }
    }
    public void increase(){
        if (sec == 59){
          sec = (sec + 1) % 60;
          if (min == 59){
              min = (min + 1) % 60;
              hr = (hr + 1) % 24;
          }
          else
             min = (min + 1) % 60;
        }
        else {
          sec = (sec + 1) % 60;
        }
        
    }
    public void decrease(){
        if (sec == 0){
          sec = 59;
          if (min == 0){
              min = 59;
              if (hr == 0)
                 hr = 23;
              else
                 hr = hr -1;
          }
          else
             min--;
        }
        else {
          sec--;
        }
        
    }
}


public class DemoTime1{

   public static void main(String arg[]){

      TimeType time1 = new TimeType();
      TimeType time2 = new TimeType(23,59,58);
      TimeType time3 = new TimeType(time2);
      System.out.println(time1.toString());
      System.out.println(time2.toString());
      System.out.println(time3.toString());
      time1.set(25,0,0);
      for (int i = 0; i<10; i++){
           time1.increase();
           time2.decrease();
      }
      System.out.println(time1.toString());
      System.out.println(time2.toString());     
   }
}

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