
Concept explainers
(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
(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.)

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.
- Define the main function in the class using public static void main(String[] args).
- 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:
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);
}
}
17:57:23
10:19:10
5:23:55
Want to see more full solutions like this?
Chapter 10 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version, Student Value Edition (11th Edition)
Additional Engineering Textbook Solutions
Management Information Systems: Managing The Digital Firm (16th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Introduction To Programming Using Visual Basic (11th Edition)
Database Concepts (8th Edition)
- Explain Five reasons if computers should replace teachers. Provide three references with your answer. List three advantages and three disadvantages face to face learning and online learning may have on children. Provide two references with your answer.arrow_forwardYou were requested to design IP addresses for the following network using the address block 10.10.10.0/24. Specify an address and net mask for each network and router interfacearrow_forwardFor the following network, propose routing tables in each of the routers R1 to R5arrow_forward
- How can I type the Java OOP code by using JOptionPane with this following code below: public static void sellCruiseTicket(Cruise[] allCruises) { //Type the code here }arrow_forwardDraw a system/level-0 diagram for this scenario: You are developing a new customer relationship management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary. Example of what a level-0 diagram looks like is attached.arrow_forwardWhat is the value of performing exploratory data analysis in designing data visualizations? What are some examples?arrow_forward
- Draw a level-0 diagram for this scenario: You are developing a new customer relationship management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary.arrow_forwardDraw a context diagram for this scenario: You are developing a new customer relationship Management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in. the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary.arrow_forwardWrite a complete Java program named FindSumAndAverage that performs the following tasks in 2-D array: Main Method: a. The main() method asks the user to provide the dimension n for a square matrix. A square matrix has an equal number of rows and columns. b. The main() method receives the value of n and calls the matrixSetUp() method that creates a square matrix of size n and populates it randomly with integers between 1 and 9. c. The main method then calls another method named printMatrix() to display the matrix in a matrix format. d. The main method also calls a method named findSumAndAverage() which: • Receives the generated matrix as input. • Calculates the sum of all elements in the matrix. • Calculates the average value of the elements in the matrix. • Stores these values (sum and average) in a single-dimensional array and returns this array • e. The main method prints the sum and average based on the result returned from findSumAndAverage()). Enter the dimension n for the square…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning




