1. You are given class Time2. In this class I have attempted to implement a setT
ID: 3840556 • 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.
2. You are given classes Employee and SalariedEmployee (the ones from your Lab 6). The classes create a hierarchy where the abstract class Employee is a super class for the SalariedEmployee. Please add an overloaded no argument constructor for the SalariedEmployee. The constructor should set the first, lastname and SSN to “ ” (a string with a single space character) and the salary to 600.00 (minimum salary for a 40 hour week). Do not use calls to set methods from the constructor, instead call the four argument constructor of the class SalariedEmployee class.
Question 2a: Did you use both a call to super() and this() in your implementation of the no argument constructor for the SalariedEmployee? If yes did this compile? If not is a call to the super class constructor taking place when you construct an object of the SalariedEmployee class using the no argument constructor? Explain.
Question 2b: Create a test class in order to showcase your new no argument constructor and its functionality. Please provide the code (.java file) for your new SalariedEmployee class and your test class. Please provide 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
Employee.java
public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
// three-argument constructor
public Employee( String first, String last, String ssn )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// return String representation of Employee object
public String toString()
{
return String.format( "%s %s social security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber() );
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
SalariedEmployee.java
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee( String first, String last, String ssn,
double salary )
{
super( first, last, ssn ); // pass to Employee constructor
setWeeklySalary( salary ); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary;
} // end method setWeeklySalary
// return salary
public double getWeeklySalary()
{
return weeklySalary;
} // end method getWeeklySalary
// calculate earnings; override abstract method earnings in Employee
public double earnings()
{
return getWeeklySalary();
} // end method earnings
// return String representation of SalariedEmployee object
public String toString()
{
return String.format( "salaried employee: %s %s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() );
} // end method toString
} // end class SalariedEmployee
Explanation / Answer
The implementations of the method setTimev2() is correct as it correctly sets the current time to be equal to the input time.
The method setTimev1() will set the current time equal to the current time itself. It is ignoring the input parameter.
Test Class to test this:
public class Time2Test
{
public static void main( String args[] ){
Time2 t1 = new Time2( 8, 25, 30 ); // 8:25:30
Time2 t2 = new Time2( 17, 14, 56 ); // 17:14:56
System.out.println("t1 Time");
System.out.printf( " %s ", t1.toUniversalString() );
System.out.printf( " %s ", t1.toString() );
System.out.println("t2 Time");
System.out.printf( " %s ", t2.toUniversalString() );
System.out.printf( " %s ", t2.toString() );
System.out.println("After setting t1 time same as t2 time");
// setting t1 time same as t5.
t1.setTimev2(t5);
System.out.println("Modified t1 Time");
System.out.printf( " %s ", t1.toUniversalString() );
System.out.printf( " %s ", t1.toString() );
}
}
OUTPUT:
t1 Time
08:25:30
08:25:30 AM
t2 Time
17:14:56
05:14:56 PM
Modified t1 Time
17:14:56
05:14:56 PM
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.