Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 10, Problem 10.1PE
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
2. Perform resource allocation for the following project. Resource limits are 6 labors and 2 helpers. Legend: Activity Dur Resources G H 2 3 2L 1H 2L OH A 1 3L 1H + B D F J K 3 4 6 2 4 4L 2H 3L OH 4L 1H 2L 2H 4L 2H C E 2 2 I 1 2L 1H 3L 1H 5L 1H
Need Java method please. Thank you.
Need Java method please. Thank you.

Chapter 10 Solutions

Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
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