// WRITE YOUR CODE BELOW TO MAKE THIS CLASS   // WORK WITH MAIN TO PRODUCE THE OUTPUT SHOWN THERE.    // The comments below can help you figure out what to do.   // You need to write two methods here:    // showCurTime and compareTo   // static showCurTime() function displays the current time   // using its .toString() method.   // standard .compareTo function to compare this Time to another Time,   // returns 0 if they are equal,    // 1 if this time is later than the parameter,   // -1 if this time is earlier than the parameter

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

This class is used to store a time of day and output it.
It also stores the current time of day and manipulates it.

class Time
{
  // instance variables for an object to store a time of day:
  private int hours;
  private int minutes;

  // static Time object to store the current time of day,
  // defaults to midnight:
  private static Time curTime = new Time();

  // Default Constructor
  // initialize time to 00:00 (midnight)
  public Time()
  {
    hours = minutes = 0;
  }

  // Constructor to parse a string hh:mm as the Time
  public Time(String hourColonMinute)
  {
    int colonIdx = hourColonMinute.indexOf(':');
    hours = Integer.parseInt(hourColonMinute.substring(0, colonIdx));
    minutes = Integer.parseInt(hourColonMinute.substring(colonIdx+1));
    normalize();
  }

  // initialize time using parameters for hour and minute
  public Time(int hr, int min)
  {
    hours = hr;
    minutes = min;
    normalize();
  }

  // set current time to parameters
  public static void setCurTime(int hr, int min)
  {
    curTime = new Time(hr, min);
  }

  // set current time from a string in hh:mm format
  public static void setCurTime(String hourColonMinute)
  {
    curTime = new Time(hourColonMinute);
  }

  // return true if this object's time is after the current time
  public boolean isLaterToday()
  {
    return (this.compareTo(curTime) > 0);
  }

  // String representation, using 24-hour time in standard hh:mm format
  // Copied from ThinkJava textbook at:
  // https://books.trinket.io/thinkjava2/chapter11.html#sec139
  public String toString()
  {
    return String.format("%02d:%02d", hours, minutes);
  }

  // Private utility method to ensure that hours is 
  // between 0 and 23 and that minutes is between 0 and 59,
  // making logical adjustments if not.
  private void normalize()
  {
    if (hours >= 24 || minutes >= 60 || minutes < 0 || hours < 0)
    {  // handle extra minutes or hours, or negative minutes or hours
      int allMinutes = hours * 60 + minutes;
      allMinutes %= 1440; // 1440 minutes = 24 hours.  Remove any full days from our time
      if (allMinutes < 0) // if it's before midnight
        allMinutes += 1440;  // add a day to get the time it should be (yesterday)
      hours = allMinutes / 60; // integer division for integer result
      minutes = allMinutes % 60;
    }
  }
  
  // DO NOT CHANGE THE ABOVE CODE


  // WRITE YOUR CODE BELOW TO MAKE THIS CLASS
  // WORK WITH MAIN TO PRODUCE THE OUTPUT SHOWN THERE. 
  // The comments below can help you figure out what to do.
  // You need to write two methods here: 
  // showCurTime and compareTo

  // static showCurTime() function displays the current time
  // using its .toString() method.

  // standard .compareTo function to compare this Time to another Time,
  // returns 0 if they are equal, 
  // 1 if this time is later than the parameter,
  // -1 if this time is earlier than the parameter

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY