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 (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)
- (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 and then implement the class. Write a test program that creates two Time objects (using new Time (), new Time(555550000),…arrow_forward5 (The Time class) Design a class named Time. The class contains: ■ Data fields hour, minute, and second that represent a time. ■ A no-arg constructor that creates a Time object for the current time. ■ A constructor that constructs a Time object with a specified elapse time since the middle of night, Jan 1, 1970, in seconds. ■ A constructor that constructs a Time object with the specified hour, minute, and second. ■ Three get functions for the data fields hour, minute, and second. ■ A function named setTime(int elapseTime) that sets a new time for the object using the elapsed time. Draw the UML diagram for the class. Implement the class. Write a test program that creates two Time objects, one using a no-arg constructor and the other using Time (555550), and display their hour, minute, and second. (Hint: The first two constructors will extract hour, minute, and second from the elapse time. For example, if the elapse time is 555550 seconds, the hour is 10, the minute is 19, and the…arrow_forwardSummary In this lab, you create a derived class from a base class, and then use the derived class in a Python program. The program should create two Motorcycle objects, and then set the Motorcycle’s speed, accelerate the Motorcycle object, and check its sidecar status. Instructions Open the file named Motorcycle.py. Create the Motorcycle class by deriving it from the Vehicle class. Call the parent class __init()__ method inside the Motorcycle class's __init()__ method. In theMotorcycle class, create an attribute named sidecar. Write a public set method to set the value for sidecar. Write a public get method to retrieve the value of sidecar. Write a public accelerate method. This method overrides the accelerate method inherited from the Vehicle class. Change the message in the accelerate method so the following is displayed when the Motorcycle tries to accelerate beyond its maximum speed: "This motorcycle cannot go that fast". Open the file named MyMotorcycleClassProgram.py. In the…arrow_forward
- Q1: Class called (num) and an objeet of this class called (numl).This class contains two data members of type int ( a and b) with private access and two member funetions with public access: Constructor to give value to variables and show_number() to display the sum and multiply of variables. Write a program to set and display the object fields.arrow_forwardUse Python Programming Language (The Stock class) Design a class named Stock to represent a company’s stock that contains: A private string data field named symbol for the stock’s symbol. A private string data field named name for the stock’s name. A private float data field named previousClosingPrice that stores the stock price for the previous day. A private float data field named currentPrice that stores the stock price for the current time. A constructor that creates a stock with the specified symbol, name, previous price, and current price. A get method for returning the stock name. A get method for returning the stock symbol. Get and set methods for getting/setting the stock’s previous price. Get and set methods for getting/setting the stock’s current price. A method named getChangePercent() that returns the percentage changed from previousClosingPrice to currentPrice. Write a test program that creates a Stock object with the stock symbol INTC, the name Inte lCorporation, the…arrow_forward#pyhton programing topic: Introduction to Method and Designing class Method overloading & Constructor overloading ------------------ please find the attached imagearrow_forward
- Object oriented programming C++ Use Classes Write a C++ program in which each flight is required to have a date of departure, a time of departure, a date of arrival and a time of arrival. Furthermore, each flight has a unique flight ID and the information whether the flight is direct or not.Create a C++ class to model a flight. (Note: Make separate classes for Date and Time and use composition).add two integer data members to the Flight class, one to store the total number of seats in the flight and the other to store the number of seats that have been reserved. Provide all standard functions in each of the Date, Time and Flight classes (Constructors, set/get methods and display methods etc.).Add a member function delayFlight(int) in the Flight class to delay the flight by the number of minutes pass as input to this function. (For simplicity, assume that delaying a flight will not change the Date). Add a member function reserveSeat(int) which reserves the number of seats passed as…arrow_forwardPlease help with the following: C# .NET change the main class so that the user is the one that as to put the name in other words. Write a driver class that prompts for the person’s data input, instantiates an object of class HealtProfile and displays the patient’s information from that object by calling the DisplayHealthRecord, method. MAIN CLASS---------------------- static void Main(string[] args) { // Instance created holding 6 parameters which are field with value HealthProfile heartRate = new HealthProfile("James","Kill",1978, 79.5 ,175.5, 2021 ); heartRate.DisplayPatientRecord();// call the patient record method } CLASS HeartProfile------------------- public class HealthProfile { // attibutes which holds the following value private String _FirstName; private String _LastName; private int _BirthYear; private double _Height; private double _Weigth; private int _CurrentYear; public HealthProfile(string firstName, string lastName, int birthYear, double height, double wt, int…arrow_forwardReadme.md: Stars(C++) This lab exercise will practice creating objects with constructors and destructors and demonstrate when constructors and destructors are called. Star Class Create a class, Star. A Star object has two member variables: its name, and a solar radius. This class should have a constructor which takes a std::string, the name of the star, and a double, the solar radius of the star. In the constructor, the Star class should print to the terminal that the star was born. For example, if you create a Star as follows: Star my_star("Saiph", 22.2); Then the constructor should print: The star Saiph was born. In the destructor, the Star class should print to the terminal that the star was destroyed, along with the number of times the volume of the sun that that star was, formatted to two decimal places. Hint: use the following line to set the precision to 2 decimal places: std::cout << std::fixed << std::setprecision(2); For example, when my_star above has its…arrow_forward
- PS: see image for question.arrow_forwardPlease written by computer source Problem Description:(The Account class) Design a class named Account that contains:A private int data field named id for the account (default 0).A private string data filed named first name for customer first name.A private string data filed named last name for customer last name.A private double data field named balance for the account (default 0).A private double data field named annualInterestRate that stores thecurrent interest rate (default 0). Assume all accounts have the sameinterest rate.A private Date data field named dateCreated that stores the date whenthe account was created.A no-arg constructor that creates a default account.A constructor that creates an account with the specified id, firstname, last name and initial balance.The accessor and mutator methods for id, name, balance, andannualInterestRate.The accessor method for dateCreated.A method named getMonthlyInterestRate() that returns the monthlyinterest rate.A method named withdraw…arrow_forwardanswer question 3 in detail in regards through java and explain each step and reasons to the codearrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,