Explain the following code. import java.text.DecimalFormat; public class Time1 extends Object { private int hour; private int minute; private int second; public Time1()

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter7: Characters, Strings, And The Stringbuilder
Section: Chapter Questions
Problem 18RQ
icon
Related questions
Question

Explain the following code.
import java.text.DecimalFormat;
public class Time1 extends Object {
private int hour;
private int minute;
private int second;
public Time1()
{
setTime( 0,0,0 );
}
public void setTime( int h, int m, int s )
{
hour = ( ( h >= 0 && h < 24 ) ? h : hour );
minute = ( ( m >= 0 && m < 60 ) ? m : minute );
second = ( ( s >= 0 && s < 60 ) ? s : second );
}
public String toUniversalString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
return twoDigits.format( hour ) + ":" +
twoDigits.format( minute ) + ":" + twoDigits.format( second );
}
public String toStandardString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" +
twoDigits.format( minute ) + ":" + twoDigits.format( second ) +
( hour < 12 ? " AM" : " PM" );
}
}
public class TimeTest1 {
public static void main( String args[] )
{
Time1 time = new Time1();
String output = "The initial universal time is: " +
time.toUniversalString() + "\nThe initial standard time is: " +
time.toStandardString();
time.setTime( 13, 27, 6 );
output += "\n\nUniversal time after setTime is: " +
time.toUniversalString() +
"\nStandard time after setTime is: " + time.toStandardString();
time.setTime( 99, 99, 99 );
output += "\n\nAfter attempting invalid settings: " +
"\nUniversal time: " + time.toUniversalString() +
"\nStandard time: " + time.toStandardString();
System.out.println(output);
}
}

Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT