Question

In: Computer Science

//Time24.java import java.util.StringTokenizer; import java.text.DecimalFormat; /** A data structure that stores integer values for hour (0..23)...

//Time24.java

import java.util.StringTokenizer;

import java.text.DecimalFormat;

/**

A data structure that stores integer values for hour (0..23) and minute (0..59) to represent the time of day in a 24-hour clock

*/

public class Time24

{

private int hour;

private int minute;

//Post: Sets the hour value in the range 0 to 23 and the minute value in the range 0 to 59

    private void normalizeTime()

    {

       int extraHours = minute / 60;

       minute %= 60;

       hour = (hour + extraHours) % 24;

    }

/**

Desc:Initializes this Time24 object

Post:hour and minute of this Time24 object both initialized to 0

*/

public Time24()

     {

         this(0,0); //calls the 2-argument constructor of class Time24

     }

/**

Desc:Initializes this Time24 object

Pre:h and m cannot be negative

Post:hour and minute of this Time24 object initialized to h and m

respectively. This operation will normalize the time if necessary (e.g.

9:75 is stored as 10:15).

Throw:IllegalArgumentException if h or m is negative

*/

public Time24(int h, int m)

    {

       setTime(h, m);

   }

/**

Desc:Sets the hour and minute of this Time24 object to a particular time

Pre:h and m cannot be negative

Post:hour and minute of this Time24 object set to h and m

respectively. This operation will normalize the time if necessary (e.g.

9:75 is stored as 10:15).

Throw:IllegalArgumentException if h or m is negative

*/

public void setTime(int h, int m)

    {

if (h < 0 || m < 0)

          throw new IllegalArgumentException("Time24.setTime: argument"

+ " must not be negative");

       this.hour = h;

      this.minute = m;

normalizeTime();

    }

/**

Desc:Adds minutes to this Time24 object

Pre:m cannot be negative

Post:This Time24 object set to m minutes later. This operation will

normalize the time if necessary (e.g. 9:75 is stored as 10:15).

Throw:IllegalArgumentException if m is negative

*/

public void addTime(int m)

    {

       if (m < 0)

          throw new IllegalArgumentException("Time24.addTime: argument"

+ " must not be negative");

       minute += m;

       normalizeTime();

    }

/**

Desc:Measures the interval from this Time24 object to another time

Return:The interval from this Time24 object to t as a Time24

*/

public Time24 interval(Time24 t)

    {

int currTime = hour * 60 + minute;

       int tTime = t.hour * 60 + t.minute;

if (tTime < currTime) tTime += 24 * 60;

return new Time24(0, tTime-currTime);

    }

/**

Desc:Gets the hour value of this Time24 object

Return:The hour value of this Time24 object

*/

public int getHour()

    {

return hour;

}

/**

Desc:Gets the minute value of this Time24 object

Return:The minute value of this Time24 object

*/

public int getMinute()

    {

return minute;

}

/**

Desc:Converts this Time24 object to a string

Return:This Time24 object as a String in the form "hh:mm"

*/

public String toString()

    {

DecimalFormat f = new DecimalFormat("00");

return hour + ":" + f.format(minute);

    }

/**

Desc:Convert a String to a Time24

Pre:s must be in the form "hh:mm" where hh and mm are positive integers

Return:A Time24 object that corresponds to s

*/

public static Time24 parseTime(String s)

    {

StringTokenizer t = new StringTokenizer(s, ":");

int h = Integer.parseInt(t.nextToken());

int m = Integer.parseInt(t.nextToken());

return new Time24(h, m);

}

}

Add 1 method, readTime, to class Time24.

Usage: void readTime(Scanner f)

Desc: Read from f the hour and minute for this Time24 object

Pre: f is ready to be read, and the next line contains hh:mm where hh:mm gives a valid 24-

hour time

Post: The entire line hh:mm read in from f.

The hour and minute of this Time24 object set to hh and mm respectively.  

The operation will normalize the time if necessary (e.g. 9:75 is stored as 10:15).

Solutions

Expert Solution


import java.util.Scanner;
import java.util.StringTokenizer;
import java.text.DecimalFormat;

/**
A data structure that stores integer values for hour (0..23) and minute (0..59) to represent the time of day in a 24-hour clock
*/

public class Time24 {

   private int hour;
   private int minute;

   //Post: Sets the hour value in the range 0 to 23 and the minute value in the range 0 to 59
   private void normalizeTime()
   {
       int extraHours = minute / 60;
       minute %= 60;
       hour = (hour + extraHours) % 24;
   }
  
   /**
   Desc:Initializes this Time24 object
   Post:hour and minute of this Time24 object both initialized to 0
   */
   public Time24()
   {
       this(0,0); //calls the 2-argument constructor of class Time24
   }
  
   /**
   Desc:Initializes this Time24 object
   Pre:h and m cannot be negative
   Post:hour and minute of this Time24 object initialized to h and m
   respectively. This operation will normalize the time if necessary (e.g.
   9:75 is stored as 10:15).
   Throw:IllegalArgumentException if h or m is negative
   */

   public Time24(int h, int m)
   {
       setTime(h, m);
   }
  
   /**
   Desc:Sets the hour and minute of this Time24 object to a particular time
   Pre:h and m cannot be negative
   Post:hour and minute of this Time24 object set to h and m
   respectively. This operation will normalize the time if necessary (e.g.
   9:75 is stored as 10:15).
   Throw:IllegalArgumentException if h or m is negative
   */
   public void setTime(int h, int m)
   {

       if (h < 0 || m < 0)
           throw new IllegalArgumentException("Time24.setTime: argument"
                   + " must not be negative");
       this.hour = h;
       this.minute = m;
       normalizeTime();
   }
  
   /**
   Desc:Adds minutes to this Time24 object
   Pre:m cannot be negative
   Post:This Time24 object set to m minutes later. This operation will
   normalize the time if necessary (e.g. 9:75 is stored as 10:15).
   Throw:IllegalArgumentException if m is negative
   */
   public void addTime(int m)
   {
       if (m < 0)
           throw new IllegalArgumentException("Time24.addTime: argument"
                   + " must not be negative");
       minute += m;
       normalizeTime();
   }
  
   /**
   Desc:Measures the interval from this Time24 object to another time
   Return:The interval from this Time24 object to t as a Time24
   */
   public Time24 interval(Time24 t)
   {
       int currTime = hour * 60 + minute;
       int tTime = t.hour * 60 + t.minute;
       if (tTime < currTime)
           tTime += 24 * 60;
       return new Time24(0, tTime-currTime);
   }
  
   /**
   Desc:Gets the hour value of this Time24 object
   Return:The hour value of this Time24 object
   */
   public int getHour()
   {
       return hour;
   }
  
   /**
   Desc:Gets the minute value of this Time24 object
   Return:The minute value of this Time24 object
   */
   public int getMinute()
   {
       return minute;
   }

   /**
   Desc:Converts this Time24 object to a string
   Return:This Time24 object as a String in the form "hh:mm"
   */
   public String toString()
   {
       DecimalFormat f = new DecimalFormat("00");
       return hour + ":" + f.format(minute);
   }
  
   /**
   Desc:Convert a String to a Time24
   Pre:s must be in the form "hh:mm" where hh and mm are positive integers
   Return:A Time24 object that corresponds to s
   */
   public static Time24 parseTime(String s)
   {
       StringTokenizer t = new StringTokenizer(s, ":");
       int h = Integer.parseInt(t.nextToken());
       int m = Integer.parseInt(t.nextToken());
       return new Time24(h, m);
   }
  
   /**
   * Desc: Read from f the hour and minute for this Time24 object
   * Pre: f is ready to be read, and the next line contains hh:mm where hh:mm gives a valid 24-hour time
   * Post: The entire line hh:mm read in from f.
   * The hour and minute of this Time24 object set to hh and mm respectively.
   * The operation will normalize the time if necessary (e.g. 9:75 is stored as 10:15).
   */
   public void readTime(Scanner f)
   {
       // read the entire line from f and set it to line
       String line = f.nextLine();
       Time24 obj = parseTime(line); // parse the line using the method parseTime and set the resultant object to obj
       // update hour and minute to obj's hour and minute
       this.hour = obj.hour;
       this.minute = obj.minute;
   }
  
}



Related Solutions

Assume that a given binary tree stores integer values in its nodes. Write a Java method...
Assume that a given binary tree stores integer values in its nodes. Write a Java method "smallest" that returns the smallest item in the tree.
Assignment: Create data file consisting of integer, double or String values. Create unique Java application to...
Assignment: Create data file consisting of integer, double or String values. Create unique Java application to read all data from the file echoing the data to standard output. After all data has been read, display how many data were read. For example, if 10 integers were read, the application should display all 10 integers and at the end of the output, print "10 data values were read" My issue is displaying how many integers were read and how many strings...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Write a java application that will read a set of values from the user and stores...
Write a java application that will read a set of values from the user and stores them in array a[]. The application should ask the user for the number of values he will enter. You need to do the following methods: 1. Read: This method will read n values from the user and store them in an array a 2. Print: this method will print the array as given in the sample output 3. maxEven: This method will find the...
In C# using a Console App, create an array that stores 20 integer values. Use the...
In C# using a Console App, create an array that stores 20 integer values. Use the random number generator to select an integer value between 0 and 10. Store the 20 random integers in the array. Once the array has been created: Print the values in the array. Print the values in the array in reverse order. Sort and print the values in the array in ascending order. Sort and print the values in the array in descending order. Count...
A data structure can be defined as a mechanism for organizing the data a program stores...
A data structure can be defined as a mechanism for organizing the data a program stores in memory. An example of a data structure is a Linked List. Discuss how a linked list works and the methods of this data structure. (Please be super detailed and type the solution)
Q#2 Write a C++ program that reads 10 integer values and stores them in an array....
Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below the average.
Q#1 Write a C++ program that reads n integer values and stores them in an array...
Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements. Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below...
Consider that time could be internally represented as 3 integers. hour (between 0 - 23) minute...
Consider that time could be internally represented as 3 integers. hour (between 0 - 23) minute (between 0 - 59) second (between 0 - 59) MyTime class consists of the following members: Default constructor: set the time to 00:00:00 Constructor taking 3 integers i.e., hour, minute and second respectively. This constructor sets this object's Time based on the given parameters. Constructor taking 2 integers i.e., hour and minute respectively. This constructor sets this object's hour and minute based on the...
Java Program that prompts the user and reads in an integer between 0 to 50, inclusively....
Java Program that prompts the user and reads in an integer between 0 to 50, inclusively. If the entered value is outside the range, program’s output will display the following message: “Error: the entered number is out of range”, and continually re-prompts the user to enter the integer again until the user enters a valid integer. • Once a valid integer has been entered, program prompts the user and reads in a second integer between -5 to 20, inclusively. If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT