bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 10, Problem 10.1PE

(The Time class) Design a class named Time. The class contains:

  • The data fields hour, minute, and second that represent a time.
  • A no-arg constructor that creates a Time object for the current time. (The values of the data fields will represent the current time.)
  • A constructor that constructs a Time object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. (The values of the data fields will represent this time.)
  • A constructor that constructs a Time object with the specified hour, minute, and second.
  • Three getter methods for the data fields hour, minute, and second, respectively.
  • A method named setTime(long elapseTime) that sets a new time for the object using the elapsed time. For example, if the elapsed time is 555550000 milliseconds, the hour is 10, the minute is 19, and the second is 10.

Draw the UML diagram for the class then implement the class. Write a test program that creates three Time objects (using new Timed, new Time(555550000),and new Time(5, 23, 55)) and displays their hour, minute, and second in the format hour:minute:second.

(Hint: The first two constructors will extract the hour, minute, and second from the elapsed time. For the no-arg constructor, the current time can be obtained using System.currentTimeMillis(), as shown in Listing 2.7, ShowCuncntTime.java. Assume the time is in GMT.)

Expert Solution & Answer
Check Mark
Program Plan Intro

Program to print the current time with required data fields

Program Plan:

  • Define the required public class.
    • Define the main function in the class using public static void main(String[] args).
      • Create the object to the class and pass the arguments.
      • Print the data field using System.out.println() .
    • Close the main function.
  • Close the public class definition.
  • Define the required class.
    • Define the integers
    • Declare the public function.
      • Define the setter method.
      • Pass the arguments to the public function.
      • Define the getter method.
      • Define the void function.
      • Compute the data fields.
    • Close the main function.
  • Close the public class definition.
Program Description Answer

Program Description:

The following java program has the class named Time and shows the current time with the data fields hour, minute and second respectively.

Explanation of Solution

Program:

//Class definition

public class Time {

//Define main function

public static void main(String[] args) {

//Create object to the class and assign the arguments

    MyTime time1 = new MyTime();

    //Print the data field

    System.out.println(time1.getHour() + ":" +

      time1.getMinute() + ":" + time1.getSecond());

    //Create object to the class and assign the arguments

    MyTime time2 = new MyTime(555550000);

    //Print the data field

    System.out.println(time2.getHour() + ":" +

      time2.getMinute() + ":" + time2.getSecond());

    //Create object to the class and assign the arguments

    MyTime time3 = new MyTime(5, 23, 55);

    //Print the data field

    System.out.println(time3.getHour() + ":" +

      time3.getMinute() + ":" + time3.getSecond());

  }

}

//Class definition

class MyTime {

  //Define the integer

  private int hour;

  private int minute;

  private int second;

  //Declare public function

  public MyTime() {

    this(System.currentTimeMillis());

  }

  //Declare public method

  public MyTime(long elapsedTime) {

//Define setter method

    setTime(elapsedTime);

  }

  //Pass the arguments to the public function

  public MyTime(int hour, int minute, int second) {

    this.hour = hour;

    this.minute = minute;

    this.second = second;

  }

  //Define getter method

  public int getHour() {

    return hour;

  }

  //Define getter method

  public int getMinute() {

    return minute;

  }

  //Define getter method

  public int getSecond() {

    return second;

  }

  //Define void function

  public void setTime(long elapsedTime) {

    // Obtain the total seconds since the midnight, Jan 1, 1970

    long totalSeconds = elapsedTime / 1000;

    // Compute the current second in the minute in the hour

    second = (int)(totalSeconds % 60);

    // Obtain the total minutes

    long totalMinutes = totalSeconds / 60;

    // Compute the current minute in the hour

    minute = (int)(totalMinutes % 60);

    // Obtain the total hours

    int totalHours = (int)(totalMinutes / 60);

    // Compute the current hour

    hour = (int)(totalHours % 24);

  }

}

Sample Output

17:57:23

10:19:10

5:23:55

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Please help with the following: C# .NET change the main class so that the user is the one that as to put the name Write a driver class (app) that prompts for the person’s data input, instantiates an object of class HeartRates and displays the patient’s information from that object by calling the DisplayPatientRecord, method. MAIN CLASS---------------------- static void Main(string[] args) { // instance of patient record with each of the 4 parameters taking in a value HeartRates heartRate = new HeartRates("James", "Kill", 1988, 2021); heartRate.DisplayPatientRecord(); // call the method to display The Patient Record } CLASS HeartRATES------------------- class HeartRates { //class attributes private private string _First_Name; private string _Last_Name; private int _Birth_Year; private int _Current_Year; // Constructor which receives private parameters to initialize variables public HeartRates(string First_Name, string Last_Name, int Birth_Year, int Current_Year) { _First_Name =…
Employee Class (Medium)     Write a class named Employee that has the following member variables: name. A string that holds the employee’s name. idNumber. An int variable that holds the employee’s ID number. department. A string that holds the name of the department where the employee works. position. A string that holds the employee’s job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name, employee’s ID number, department, and position. A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name and ID number. The department and position fields should be assigned an empty string (""). A default constructor that assigns empty strings ("") to the name, department, and position member variables, and 0 to the idNumber member variable. Write appropriate mutator functions that…
Bank Accounts (Use Python) Write a program that accepts bank transactions and prints out the balance of an account afterwards. PROGRAM DESIGN Create a class named bankAccount. A bank account should have the following attributes: accNumber, balance, and dateOpened. This class should also have the option to do the following transactions: deposit, withdraw and drop. Initially there is only one bankAccount active in the program, with a balance amount of 1515, and was opened back in 10/01/1987. Depositing adds to a current bankAccount’s balance, while withdrawing – does otherwise. Dropping would equate the current balance to zero. Refer to the following class diagram for more details about the current bankAccount: bankAccountaccNumber = 1balance = 1515dateOpened = "10/27/1987" deposit(dep)withdraw(wd)drop(accNumber)   INPUTThe input would be a string that contains the following data: the transaction to be done (dep – for deposit, wd – for withdraw and drop – for drop), the accNumber, and…

Chapter 10 Solutions

Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition

Ch. 10.7 - Prob. 10.7.5CPCh. 10.8 - What are autoboxing and autounboxing? Are the...Ch. 10.8 - Show the output of the following code. public...Ch. 10.9 - What is the output of the following code? public...Ch. 10.10 - Suppose s1, s2, s3, and s4 are four strings, given...Ch. 10.10 - To create the string Welcome to Java, you may use...Ch. 10.10 - What is the output of the following code? String...Ch. 10.10 - Let s1 be Welcome and s2 be welcome Write the...Ch. 10.10 - Prob. 10.10.5CPCh. 10.10 - Prob. 10.10.6CPCh. 10.10 - Prob. 10.10.7CPCh. 10.10 - Prob. 10.10.8CPCh. 10.10 - What is wrong in the following program? 1public...Ch. 10.10 - Show the output of the following code: public...Ch. 10.10 - Show the output of the following code: public...Ch. 10.11 - Prob. 10.11.1CPCh. 10.11 - Prob. 10.11.2CPCh. 10.11 - Prob. 10.11.3CPCh. 10.11 - Prob. 10.11.4CPCh. 10.11 - Prob. 10.11.5CPCh. 10.11 - Suppose s1 and s2 are given as fot tows:...Ch. 10.11 - Show the output of the following program: public...Ch. 10 - (The Time class) Design a class named Time. The...Ch. 10 - (The BMI class) Add the following new constructor...Ch. 10 - (The MyInteger class) Design a class named...Ch. 10 - Prob. 10.4PECh. 10 - (Display the prime factors) Write a program that...Ch. 10 - (Display the prime numbers) Write a program that...Ch. 10 - Prob. 10.7PECh. 10 - Prob. 10.8PECh. 10 - (The Course class) Revise the Course class as...Ch. 10 - Prob. 10.10PECh. 10 - Prob. 10.11PECh. 10 - (Geometry: the Triangle2D class) Define the...Ch. 10 - (Geometry: the MyRectangle 2D class) Define the...Ch. 10 - (The MyDate class) Design a class named MyDate....Ch. 10 - (Geometry: the bounding rectangle) A bounding...Ch. 10 - (Divisible by 2 or 3) Find the first 10 numbers...Ch. 10 - (Square numbers) Find the first 10 square numbers...Ch. 10 - (Mersenne prime)A prime number is called a...Ch. 10 - (Approximate e) Programming Exercise 5.26...Ch. 10 - (Divisible by 5 or 6) Find the first 10 numbers...Ch. 10 - (Implement the String class) The String class is...Ch. 10 - (Implement the String class) The String class is...Ch. 10 - (Implement the Character class) The Character...Ch. 10 - (New string split method) The split method in the...Ch. 10 - (Implement the StringBuilder class) The...Ch. 10 - (Implement the StringBuilder class) The...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Describe a method that can be used to gather a piece of data such as the users age.

Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)

List four activities of a typical operating system.

Computer Science: An Overview (12th Edition)

(Keyword new) Whats the purpose of keyword new? Explain what happens when you use it.

Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)

Give an example of a data constraint.

Database Concepts (8th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
What is Abstract Data Types(ADT) in Data Structures ? | with Example; Author: Simple Snippets;https://www.youtube.com/watch?v=n0e27Cpc88E;License: Standard YouTube License, CC-BY