Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
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
#this is a python program #topic: OOP   Design a class called Pokemon using a parameterized constructor so that after  executing the following line of code the desired result shown in the output box will be  printed. First object along with print has been done for you, you also need to create  other objects and print accordingly to get the output correctly.  [You are not allowed to change the code below]  #Write your code for class here  team_pika = Pokemon('pikachu', 'charmander', 90, 60, 10) print('=======Team 1=======')  print('Pokemon 1:',team_pika.pokemon1_name,   team_pika.pokemon1_power)  print('Pokemon 2:',team_pika.pokemon2_name,   team_pika.pokemon2_power)  pika_combined_power = (team_pika.pokemon1_power +  team_pika.pokemon2_power) * team_pika.damage_rate print('Combined Power:', pika_combined_power)  #Write your code for subtask 2,3,4 here  Output:  =======Team 1=======  Pokemon 1: pikachu 90  Pokemon 2: charmander 60  Combined Power: 1500  =======Team 2=======  Pokemon 1:…
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…
Java:

Chapter 10 Solutions

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
T F The getline member function can be used to read text that contains whitespaces.

Starting Out with C++ from Control Structures to Objects (9th Edition)

Find the errors in the following code: 3. // Warning! This code contains ERRORS! if (num2 == 0) System.out.prin...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

List four activities of a typical operating system.

Computer Science: An Overview (13th Edition) (What's New in Computer Science)

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
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
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
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
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