'Time java. Problem getting the correct outcome
So I'm having an issue getting the correct output for this java problem.
public class GetTime {
/**/
public static void main ( String [] arg ) {
/**/
int option;
Time t;
/**/
System.out.println();
option=option();
/**/
if ( option == 1 ) t=new Time();
else t=new Time12();
/**/
System.out.println();
System.out.println(t);
/**/
return;
}
/**/
private static int option () {
/**/
int rv;
ConsoleInput ci;
/**/
ci=new ConsoleInput();
System.out.println(" Enter option:");
System.out.println("1 = 24-hour clock");
System.out.println("2 = 12-hour clock");
while ( true ) {
rv=ci.readInt(" option = ? ");
if ( ( rv == 1 ) || ( rv == 2 ) ) break;
}
ci.close();
/**/
return rv;
}
}
This coding just runs the files. When I choose option 1, the outcome is the current time in a 24 hour format. When I choose option 2, the outcome is the current time in a 12 hour format.
import java.util.Calendar;
/**/
public class Time {
/**/
private int hour, minute, second;
/**/
public Time () {
init();
}
/*
* 0, 1, ... , 23
*/
public int getHour () {
return hour;
}
/**/
public int getMinute () {
return minute;
}
/**/
public int getSecond () {
return second;
}
/**/
public String toString () {
return i2s(hour) + ":" + i2s(minute) + ":" + i2s(second);
}
/*
* i = 0 , 1 , ... , 59
* return "00", "01", ... , "59"
*/
public static String i2s ( int i ) {
return String.format("%02d",i);
}
/**/
private void init () {
/**/
Calendar c;
/**/
c=Calendar.getInstance();
hour=c.get(Calendar.HOUR_OF_DAY);
minute=c.get(Calendar.MINUTE);
second=c.get(Calendar.SECOND);
/**/
return;
}
}
I have this file which extends the coding I am having.
pubic class Time12 extends Time {
public Time12 () {
super();
}
@Override
pubic String toString () {
int h12, h24;
h24=getHour();
if ( h24 == 12 ) h12=0;
else h12=h24;
return Time.i2s(h12) + ":" + Time.i2s(getMinute()) + ":" + Time.i2s(getSecond());
}
Basically my problem is that I have to run this by using both a 24 hour clock and 12 hour clock. The code already automatically uses A 24 hour clock, I just need to do a 12 hour clock. When I try to run it using option 1 and 2, they only run using the 24 hour clock.
Do I make another if statement? Do I change the else statement to something else?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
