programtotestthetimeclass.java
// Program to test the Time class
import java.io.*;
public class TestTime
{
public static void main ( String args[]) throws IOException
{
int h, m, s; // variable for the time class to use
int menu; // for the switch
String answer; // for the do while loop
String x; // used for reading in values
// sets up in as the buffer reader
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
System.out.flush(); // clears the system ready for use
// start of the do while loop to allow the
user to go around and around
do
{
System.out.println("Please use the menu below to choose
your input types");
System.out.println("Menu : ");
System.out.println("Enter 0 if the default time is to
be set");
System.out.println("Enter 1 if just the hours are to be
set");
System.out.println("Enter 2 if the hours and minutes are
to be set");
System.out.println("Enter 3 if the hours, minutes and
seconds are to be set");
System.out.flush();
x=in.readLine(); // reads the users input in
menu = Integer.parseInt(x);
switch(menu)
{
case 0: // default time
System.out.println("No parameters were selected
and the default time is selected");
Time t1 = new Time(); // initialises a new
instance of the Time class
System.out.println("The default time is "
+ t1.getTime() + " or " + t1.get24hrTime());
break;
case 1: // one parameter
System.out.println("You have selected to enter
one parameter");
System.out.println("Please enter the hour
for the clock between 0 and 23 "); // prompts the user for the input
System.out.flush();
x=in.readLine(); // reads the users input
in
h=Integer.parseInt(x); // converts the string
to an integer
Time t2 = new Time(h); // initialises a new
instance of the time class
System.out.println("The time you have selected
is " + t2.getTime() + " or " + t2.get24hrTime());
System.out.println("The Hours entered were
" + t2.getHour());
break;
case 2: // two parameters
System.out.println("You have selected to enter
two parameters.");
System.out.println("Please enter the hour
for the clock "); // prompts the user for the first variable
System.out.println("Enter a value between
0 and 23");
System.out.flush();
x=in.readLine(); // reads the users input
in
h=Integer.parseInt(x); // converts the string
into an integer
System.out.flush();
System.out.println("Please enter the minutes
for the clock"); //prompts the user for the second variable
System.out.println("Enter a value between
0 and 59");
System.out.flush();
x=in.readLine(); // reads the users input
in
m=Integer.parseInt(x); // converts the string
to an integer
Time t3 = new Time(h,m); // Initialises a
new instance of the time class
System.out.println("The time you selected
is " + t3.getTime() + " or " + t3.get24hrTime());
System.out.println("The Hours entered were
" + t3.getHour() + " and the minutes were " + t3.getMinute());
break;
case 3: // three parameters
System.out.println("You have selected to enter
3 parameters");
System.out.println("Please enter the hour
for the clock "); // prompts the user for the first variable
System.out.println("Enter a value between
0 and 23");
System.out.flush();
x=in.readLine(); // reads the users input
in
h=Integer.parseInt(x); // converts the string
into an Integer
System.out.flush();
System.out.println("Please enter the minutes
for the clock"); //prompts the user for the second variable
System.out.println("Enter a value between
0 and 59"); // read the users input in
System.out.flush();
x=in.readLine(); // reads the users input
in
m=Integer.parseInt(x); // converts the string
to an integer
System.out.println("Enter the seconds for
the clock"); // prompts the user for the third variable
System.out.println("Enter a value between
0 and 59");
System.out.flush();
x=in.readLine();
s=Integer.parseInt(x); // converts the string
to an integer
Time t4 = new Time(h,m,s); // initialises
a new instance of the class time
System.out.println("The time you have selected
is " + t4.getTime() + " or " + t4.get24hrTime());
System.out.println("The Hours entered were
" + t4.getHour() + " and the minutes were " + t4.getMinute()+ " and the
seconds were " + t4.getSecond());
break;
default:
System.out.println("Please select a number
between 0 and 3");
break;
}
System.out.println("Enter Y to continue or anything else
to stop ");
System.out.flush();
answer=in.readLine(); // reads the users input in
} while(answer.equalsIgnoreCase("Y"));
}// ends the main
}// ends the class
Return to : Java Programming Hints
and Tips