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

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...
java. Consider a binary tree with integer values in its nodes, implement a method that returns...
java. Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
STAT 150 Homework 23. Random Variable X takes integer values and has the Moment Generating Function:...
STAT 150 Homework 23. Random Variable X takes integer values and has the Moment Generating Function: Mx(t)= 4/(2-e^t)  -  6/(3-e^t). 1) What is the variance of X. 2) Find the probability P(X ≤ 2).
STAT 150 Homework 23. Random Variable X takes integer values and has the Moment Generating Function:...
STAT 150 Homework 23. Random Variable X takes integer values and has the Moment Generating Function: Mx(t)= 4/(2-e^t)  -  6/(3-e^t). Find the probability P(X ≤ 2).
Data Structure in Java The following java method counts how many triples of integers in an...
Data Structure in Java The following java method counts how many triples of integers in an array of n distinct integers sum to zero. public static int count(int[] a) { int n = a.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) count++; } } }...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
write a java program that implements the splay tree data structure for the dictionary abstract data...
write a java program that implements the splay tree data structure for the dictionary abstract data type. Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line. in.dat file contents: 3456 5678 1234 2369 7721 3354 1321 4946 3210 8765 Then the program starts an interactive mode. The commands are as follows. S 1000 - splay the tree at 1000 F...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT