1. You are given class Time2. In this class I have attempted to implement a setT
ID: 3840558 • Letter: 1
Question
1. You are given class Time2. In this class I have attempted to implement a setTime(Time2 time)method, that takes a Time2 object as argument and modifies the current time objects hour,minutes and seconds to match the time in this object. So the functionality of the setTimemethod, is similar to you synchronizing the time on your watch with the time on a wall clock,where the time on the wall clock is the time passed as an argument and the time in your watch is the time on which you are calling the setTime method. I am providing two implementation, setTimev1 and setTimev2.
Question 1a: By looking at the code, which method is performing the functionality described above and which is not?
Question 1b: Design a test in order to verify your question 1 reply. Implement the respectiveTimeTest class and run the test. Explain what you expected to see when you run the test and how this verifies your initial reply. Please provide the source code of the test class and execution screenshots.
Time2.java
public class Time2
{
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// Time2 no-argument constructor: initializes each instance variable
// to zero; ensures that Time2 objects start in a consistent state
public Time2()
{
this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
} // end Time2 no-argument constructor
// Time2 constructor: hour supplied, minute and second defaulted to 0
public Time2( int h )
{
this( h, 0, 0 ); // invoke Time2 constructor with three arguments
} // end Time2 one-argument constructor
// Time2 constructor: hour and minute supplied, second defaulted to 0
public Time2( int h, int m )
{
this( h, m, 0 ); // invoke Time2 constructor with three arguments
} // end Time2 two-argument constructor
// Time2 constructor: hour, minute and second supplied
public Time2( int h, int m, int s )
{
setTime( h, m, s ); // invoke setTime to validate time
} // end Time2 three-argument constructor
// Time2 constructor: another Time2 object supplied
public Time2( Time2 time )
{
// invoke Time2 constructor with three arguments
this( time.getHour(), time.getMinute(), time.getSecond() );
} // end Time2 constructor with Time2 argument
// Set Methods
// set a new time value using universal time; perform
// validity checks on data; set invalid values to zero
public void setTime( int h, int m, int s )
{
setHour( h ); // set the hour
setMinute( m ); // set the minute
setSecond( s ); // set the second
} // end method setTime
public void setTimev1( Time2 time )
{
time.setTime ( this.getHour(), this.getMinute(), this.getSecond() ); //
} // end method setTime v1
public void setTimev2( Time2 time )
{
this.setTime ( time.getHour(), time.getMinute(), time.getSecond() ); //
} // end method setTime v2
// validate and set hour
public void setHour( int h )
{
hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
} // end method setHour
// validate and set minute
public void setMinute( int m )
{
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
} // end method setMinute
// validate and set second
public void setSecond( int s )
{
second = ( ( s >= 0 && s < 60 ) ? s : 0 );
} // end method setSecond
// Get Methods
// get hour value
public int getHour()
{
return hour;
} // end method getHour
// get minute value
public int getMinute()
{
return minute;
} // end method getMinute
// get second value
public int getSecond()
{
return second;
} // end method getSecond
// convert to String in universal-time format (HH:MM:SS)
public String toUniversalString()
{
return String.format(
"%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
} // end method toUniversalString
// convert to String in standard-time format (H:MM:SS AM or PM)
public String toString()
{
return String.format( "%d:%02d:%02d %s",
( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 ),
getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
} // end method toStandardString
} // end class Time2
Explanation / Answer
Question 1a: By looking at the code, which method is performing the functionality described above and which is not?
This method:
public Time2( Time2 time )
{
// invoke Time2 constructor with three arguments
this( time.getHour(), time.getMinute(), time.getSecond() );
} // end Time2 constructor with Time2 argument
performing the functionality described above
2)
public class TimeTest {
public static void main(String[] args) {
Time2 t1 = new Time2(4);
System.out.println(t1);
t1.setTime(5, 21, 45);
System.out.println(t1);
Time2 t2 = new Time2(t1);
System.out.println(t2);
Time2 t3 = new Time2(17, 34, 32);
System.out.println(t3);
t3.setTimev1(t2);
System.out.println(t3);
}
}
/*
Sample run:
4:00:00 AM
5:21:45 AM
5:21:45 AM
5:34:32 PM
5:34:32 PM
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.