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

public class Clock { private int hr; private int min; private int sec; public Cl

ID: 3903248 • Letter: P

Question

public class Clock
{
    private int hr;
    private int min;
    private int sec;

    public Clock(int hours, int minutes, int seconds)
    {
          setTime(hours, minutes, seconds);
    }

    public Clock()
    {
        setTime(0, 0, 0);
    }

    public void setTime(int hours, int minutes, int seconds)
    {
          if(0 <= hours && hours < 24)
               hr = hours;
          else
               hr = 0;

        if(0 <= minutes && minutes < 60)
               min = minutes;
          else
               min = 0;

          if(0 <= seconds && seconds < 60)
               sec = seconds;
          else
             sec = 0;
    }

    public int getHours()
    {
            return hr;
    }

    public int getMinutes()
    {
            return min;
    }

    public int getSeconds()
    {
            return sec;
    }

    public void printTime()
    {
             if(hr < 10)
               System.out.print("0");
          System.out.print(hr + ":");

            if(min < 10)
               System.out.print("0");
            System.out.print(min + ":");

            if(sec < 10)
               System.out.print("0");
            System.out.print(sec);
    }

    public void incrementSeconds()
    {
          sec++;
            if(sec > 59)
            {
               sec = 0;
               incrementMinutes();
            }
    }

    public void incrementMinutes()
    {
            min++;
            if(min > 59)
            {
               min = 0;
               incrementHours();
            }
    }

    public void incrementHours()
    {
          hr++;
          if(hr > 23)
              hr = 0;
    }

    public String toString()
    {
        String str = "";

        if(hr < 10)
             str = "0";
        str = str + hr + ":";

        if(min < 10)
             str = str + "0" ;
        str = str + min + ":";

        if(sec < 10)
             str = str + "0";
        str = str + sec;

        return str;
    }

From the code above, I need help with creating a child class called ClockChild. I need to create a constructor that calls the parent’s default constructor and an overloaded constructor that takes in hours, minutes and seconds and sets the member variables accordingly. I then need to create a printTime method, for the child. Now it will take in a parameter called typeOfClock. This will be an integer that will be either 12 or 24. This tells the function how to print the time, either in military time (the parents type, call the parent method) or in normal type (12-hour) 11:30pm or 9:09am.

Explanation / Answer


public class ClockChild extends Clock
{
    public int type;

    public ClockChild()
    {
        setTime(23, 30, 30);
    }

    public ClockChild (int hours, int minutes, int seconds)
    {
        setTime(11, 30, 30);
    }

    public void printTime(int typeOfClock)
    {
        type = typeOfClock;
        if (typeOfClock == 24)
        {
            setTime(23, 30, 30);
            super.printTime();
        }
        if (typeOfClock == 12)
        {
            setTime(11, 30, 30);
            this.printTime();
        }
    }
}