In: Computer Science
Must be written in Java:
After completing this lab, you should be able to:
Write a Java class to represent Time.
Create default and parameterized constructors.
Create accessor and mutator methods.
Write a toString method.
This project will represent time in hours, minutes, and seconds.
Part 1:
Create a new Java class named Time that has the following instance fields in the parameterized constructor:
hours
minutes
seconds
Create a default constructor that sets the time to that would represent the following time: 12:00:00.
Part 2:
Create getter methods.
getHours() : returns the current hours of Time
getMinutes() : returns the current minutes of Time
getSeconds() : returns the current seconds of Time.
Create setter methods. Do they return anything?:
setHours() : method allows user to change the hours of Time
Should have 1 input parameter.
setMinutes() : method allows user to change the minutes of Time
Should have 1 input parameter.
setSeconds() : method allows user to change the seconds of Time
Should have 1 input parameter.
setTime() method with 3 parameters -- to change the hours, minutes, and seconds of Time.
DON’T FORGET TO JAVADOC
Part 3:
In your TimeTester class you should have a main method that uses your Time class.
Create 2 Time objects;
One object that will use the default constructor.
One that uses the parameterized constructor.
Print the just hours of both Time objects.
Print the just minutes of both Time objects.
Print the just seconds of both Time objects.
Change the parameterized Time object.
Using setTime() method, change to 12:34:56.
Change the default Time object.
Hours to 4 using setHours().
Minutes to 2 using setMinutes().
Seconds to 3 using setSeconds().
Print both Time objects. What do you notice?
In Time Class
Create a toString() method that, when called, returns the state of the Time object in the format hh:mm:ss i.e. 12:23:33.
In your timeTester class
Print both Times objects. What do you notice?
Don’t forget to check for errors! Make sure to ask questions if you have any.
Compile/build your code, there should be no errors.
Run your code because you just created your main/tester class.
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
Time.java
package c15;
public class Time {
private int hours;
private int minutes;
private int seconds;
Time(){
seconds=0;
minutes=0;
hours=12;
}
public Time(int hours, int minutes, int seconds)
{
super();
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
public void setTime(int hours, int minutes, int
seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
@Override
public String toString() {
return
String.format("Time[%02d:%02d:%02d]", hours,minutes,seconds);
}
public static void main(String[] args) {
Time defaultTime = new
Time();
System.out.println(defaultTime);
Time valTime = new
Time(12,34,30);
System.out.println(valTime);
System.out.println("Hour of
first object : "+defaultTime.getHours());
System.out.println("Hour of second
object : "+valTime.getHours());
System.out.println("Minutes of
first object : "+defaultTime.getMinutes());
System.out.println("Minutes of
second object : "+valTime.getMinutes());
System.out.println("Seconds of
first object : "+defaultTime.getSeconds());
System.out.println("Seconds of
second object : "+valTime.getSeconds());
valTime.setTime(12, 34,
56);
System.out.println(valTime);
defaultTime.setHours(4);
defaultTime.setMinutes(2);
defaultTime.setSeconds(3);
System.out.println(defaultTime);
}
}
output