Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a subclass time2tz which allows you to maintain the time zone in addition

ID: 665178 • Letter: W

Question

Write a subclass time2tz  which allows you to maintain the time zone in addition to the time. Allowed values for time zone include: EST, CST, MST, PST. You will need to override the toString and the toUniversalString methods to return the time with its corresponding time zone. Your class should include at least 2 constructors (your choice). Turn in your source code and do a demo showing your testing of the class where you create several time objects and several “time zoned” objects and show the polymorphic behavior of either the toString or the toUniversalString methods.

Note: Its the second part of the question i post earlier. Here is the link for full question.

https://www.chegg.com/homework-help/questions-and-answers/part--modify-original-time2java-include-two-overloaded-methods-add-time-time-objects-follo-q7874901

Explanation / Answer

public class Time2tz
{
private int hour;   
private int minute;
private int second;

  
public Time2tz()
{
this( 0, 0, 0 );
}


public Time2tz( int h )
{
this( h, 0, 0 );
}


public Time2tz( int h, int m )
{
this( h, m, 0 );
}

  
public Time2tz( int h, int m, int s )
{
setTime( h, m, s );
}

public Time2( Time2 time )
{
  
this( time.getHour(), time.getMinute(), time.getSecond() );
}


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

// 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


public int getHour()
{
return hour;
}
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

  
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 toString
}

public class TimeZone {
public static void main(String[] args) throws ParseException {

long currentLong = System.currentTimeMillis();
TimeZone.setDefault(TimeZone.getTimeZone("EST"));
System.out.println("EST=="+ new java.util.Date(currentLong).toString());

TimeZone.setDefault(TimeZone.getTimeZone("CST"));
System.out.println("CST=="+ new java.util.Date(currentLong).toString());

TimeZone.setDefault(TimeZone.getTimeZone("MST"));
System.out.println("MST=="+ new java.util.Date(currentLong).toString());

TimeZone.setDefault(TimeZone.getTimeZone("PST"));
System.out.println("PST=="+ new java.util.Date(currentLong).toString());


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote