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: 9780134700144
Author: 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
What are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

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