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
Instructions-Java Assignment is to define a class named Address. The Address class will have three private instance variables: an int named street_number a String named street_name and a String named state. Write three constructors for the Address class: an empty constructor (no input parameters) that initializes the three instance variables with default values of your choice, a constructor that takes the street values as input but defaults the state to "Arizona", and a constructor that takes all three pieces of information as input Next create a driver class named Main.java. Put public static void main here and test out your class by creating three instances of Address, one using each of the constructors. You can choose the particular address values that are used. I recommend you make them up and do not use actual addresses. Run your code to make sure it works.  Next add the following public methods to the Address class and test them from main as you go: Write getters and…
Please fill in the blanks for the following statements: To define a class use the keyword ________________ followed by the name of the class.
C++ Using Card and Deck class created during the lecture or your own implementation that follows the spec for a card and deck, implement the following game: game is designed for a single player who is playing against the computeryou draw three cards for each player (player and a computer) Using the compare function from the Card class you compare each pair drawnIf the player's card is higher, the player gets a pointYou repeat this comparison 3 times (for each pair of cards you draw)The player wins if he gets more points than a computer (has more higher cards than the computer)

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
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++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
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