Here is the code for the time class
// Represents the time of the day in hours
// and minutes using the 24-hour clock.
public class Time
{
private int hours;
private int mins;
public Time(int hours,int mins)
{
this.hours=hours;
this.mins=mins;
if (hours>0&&hours<=12&&mins>=0&&mins<=60 )
{
hours=hours;
mins=mins;
}
else
{
throw new IllegalArgumentException("\nINVALID INPUT!!");
}
}
//This method returns the time in minutes
private int toMins()
{
return (hours*60)+mins;
}
// Returns true if this time is earlier,
// false if a time passed as an argument or the times are the same
public boolean lessThan(Time t)
{
return toMins()<t.toMins();
}
public String toString()
{
return hours+":"+mins;
}
// Calculates the number of minutes that have passed by
// between the argument Time t and this time object.
public int elapsedSince(Time t)
{
if(t.toMins()<toMins())
return toMins()-t.toMins();
else
return (24*60)-(t.toMins()-toMins());
}
}
Here is the TestTime class which has the main method in it and i am not sure what to write in this> It is in this that i take in hours and minutes and create a 12 hour clock object!!
import java.util.Scanner;
public class TestTime
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Time 1" );
System.out.print("Enter hours: " );
int h1 = keyboard.nextInt();
System.out.print("Enter mins: " );
int m1 = keyboard.nextInt();
System.out.println("Enter your choice 1)A.M 2)P.M");
keyboard.nextLine();
int choice=keyboard.nextInt();
convert(choice);
Time t1 = new Time(h1, m1);
System.out.println("Time 2" );
System.out.print("Enter hours: " );
int h2 = keyboard.nextInt();
System.out.print("Enter mins: " );
int m2 = keyboard.nextInt();
Time t2 = new Time(h2, m2);
System.out.println(t1 + " <= " + t2 + "? " + t1.lessThan(t2));
System.out.println(t2.elapsedSince(t1) + " minutes elapsed from t1 to t2.");
}
public static void convert(int choice,Time t)
{
if(choice==1)
/// ??????????
else//add 12 hours and call the toMins() method og the time class
///????
}
}
*Edited for a more descriptive title
This post has been edited by pbl: 19 January 2009 - 10:02 PM

New Topic/Question
Reply




MultiQuote




|