Winkster
11-27-2004, 11:06 PM
I have written a class to convert military time to standard time. What I need to do is modify this code so a user can input the military time. I realize I can take out some extraneous code. Thanks.
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
// Time abstract data type (ADT) definition
public class Time
{
private int hour; // 0 - 23 (attributes)
private int minute; // 0 - 59 (attributes)
//private int second; // 0 - 59 (attributes)
private Date dt;
private long offset;
int number;
// Time constructor initializes each data member to zero.
// Ensures all Time objects start in a consistent state.
public Time( )
{
setOffset();
setTime(0,0); //removed seconds
}
// set a new Time value hour, minute
public void
setTime(int h, int m)throws IllegalArgumentException
{
if(h > 23 || h <0 || m > 59 ||m < 0 )
throw(new IllegalArgumentException());
hour = h;
minute = m;
dt = new Date(((h*3600)+(m*60))*1000-offset);
}
void setOffset()
{
Calendar c = Calendar.getInstance();
offset =c.get(c.ZONE_OFFSET);
}
// Print time in standard format
public void printStandard( )
{
System.out.println(new SimpleDateFormat("h:mm a").format(dt));
}
public void printMilitary()
{
System.out.println(new SimpleDateFormat("HHmm").format(dt));
}
// Driver to test simple class Time
public static void main( String args[ ] )
{
Time t = new Time( ); // instantiate object t of class time
System.out.print("\nThe initial standard time is ");
t.printStandard( );
System.out.print("\nThe initial military time is ");
t.printMilitary();
try
{
System.out.println("Enter time in 24 hour notation:");
t=SavitchIn.readLine();//THIS IS WHAT I NEED TO FIX???
t.setTime(13,47);
}
catch(IllegalArgumentException e){System.out.println(e);}
System.out.print("That is the same as ");
t.printStandard( );
System.out.print("That is the same as ");
t.printMilitary( );
try{t.setTime(99, 99);} // attempt invalid settings
catch(IllegalArgumentException e){System.out.println(e);}
t.printStandard( );
}
}
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
// Time abstract data type (ADT) definition
public class Time
{
private int hour; // 0 - 23 (attributes)
private int minute; // 0 - 59 (attributes)
//private int second; // 0 - 59 (attributes)
private Date dt;
private long offset;
int number;
// Time constructor initializes each data member to zero.
// Ensures all Time objects start in a consistent state.
public Time( )
{
setOffset();
setTime(0,0); //removed seconds
}
// set a new Time value hour, minute
public void
setTime(int h, int m)throws IllegalArgumentException
{
if(h > 23 || h <0 || m > 59 ||m < 0 )
throw(new IllegalArgumentException());
hour = h;
minute = m;
dt = new Date(((h*3600)+(m*60))*1000-offset);
}
void setOffset()
{
Calendar c = Calendar.getInstance();
offset =c.get(c.ZONE_OFFSET);
}
// Print time in standard format
public void printStandard( )
{
System.out.println(new SimpleDateFormat("h:mm a").format(dt));
}
public void printMilitary()
{
System.out.println(new SimpleDateFormat("HHmm").format(dt));
}
// Driver to test simple class Time
public static void main( String args[ ] )
{
Time t = new Time( ); // instantiate object t of class time
System.out.print("\nThe initial standard time is ");
t.printStandard( );
System.out.print("\nThe initial military time is ");
t.printMilitary();
try
{
System.out.println("Enter time in 24 hour notation:");
t=SavitchIn.readLine();//THIS IS WHAT I NEED TO FIX???
t.setTime(13,47);
}
catch(IllegalArgumentException e){System.out.println(e);}
System.out.print("That is the same as ");
t.printStandard( );
System.out.print("That is the same as ");
t.printMilitary( );
try{t.setTime(99, 99);} // attempt invalid settings
catch(IllegalArgumentException e){System.out.println(e);}
t.printStandard( );
}
}