View Full Version : Help with driver program. Not sure what to do Java

07-07-2005, 03:01 AM
This was out assignment
Modify the 'Time3' class in Deitel fig. 8.8 to include the 'tick' method that increments the time stored in a Time3 object by one second. Also provide method incrementMinute to increment the minute and method incrementHour to increment the hour. The Time3 object should always remain in a consistent state. Write a driver program that tests the 'tick' method, the 'incrementMinute' and 'incrementHour' method to be sure they work correctly. Be sure to test the following cases:

a) incrementing into the next minute
b) incrementing into the next hour
c) incrementing into the next day

And then he added this update to our disccussion board

1. First, you need to add (or modify) the tick method to Test into your Time3 class.

2. Add two new methods, incrementMinute and incrementHour to Time3 class.

So your new Time3 does not only include all its old methods (coded as textbook, like setTime, getHour....) but also has combined these 3 new methods.

3. Must have a driver program. For example, old time is 13(H):59(M):59(S). If use wants to add 1 seconds (using your tick method). The new time should show (output) 14:00:00. If use wants to add 1 Minute and 1 Second, the new time should show 14:01:00.

"This is the timetest3.java. code

//Ronald Rule
//Time3 class declaration with set and get methods
import java.text.DecimalFormat;

public class Time3 {
private int hour; //0-23
private int minute; //0-59
private int second; //0-59

//Time3 constructor intializes eacb instance variable
// to zero;
//ensures that Time object starts in a consistent state

public Time3()
this(0, 0, 0); //invoke Time3 constructor with three arguments

//Time3 constructor; hour supplied, minute and second defaulted to 0
public Time(int h)
this(h, 0, 0); //invoke Time3 constructor with three arguments

//Time3 constructor; hour supplied, minute and second defaulted to 0
public Time(int h, int m)
this(h, m, 0); //invoke Time3 constructor with three arguments

//Time3 constructor; hour, minute, and second supplied
public Time(int h, int m, int s)
this(h, m, s); //invoke Time3 constructor with three arguments

//Time3 constructor; another Time3 object supplied
public Time3 (Time3 time)
//invoke Time3 constructor with three arguments
this(time.getHour(), time.getMinute(), time.getSecond() );

//Set Methods
//set a mew time value using universal time; perform
//validity checks on data; set invalid values to zero
public void setTime( int h, int m, int s)
setHour( h); //set the hour
setMinute( m ); //set the minute
setSecond( s ); //set the second

//Validate and set hour
public void setHour( int h)
hour = ( ( h>= 0 && h < 24) ? h:0);

//validate and set minute
public void setMinute(int m)
minute = ( (m>= 0 && m <60) ? m:0);

//validate and set second
public void setSecond(int s)
second = (( s>=0 && s<60) ? s:0);

public int getHour()
return hour;

public int getMinute()
return minute;

public int getSecond()
return second;

//Convert to String in universal-time format
public String toUniversalString()
DecimalFormat twoDigits = new DecimcalFormat( "00" );

return twoDigits.format (getHour() ) + ":" +
twoDigits.format (getMinute() ) + ":" +
twoDigits.format (getSecond() );

//convert to String in standard-time format
public String toStandardString()
DecimalFormat twoDigits = new DecimalFormat ("00");

return ((getHour() == 12 || getHour() == 0) ?
12 : getHour() % 12 ) + ":" + twoDigits.format( getMinute() ) +
(getHour() < 12 ? " AM" : "PM");

} //end class Time 3

Then this is timetest4, but it is only an appletviewer not a driver program

//Ronald G. Rule
//Program 5
//July 2, 2005
//This program will give the user the ability to
//to increase the second, minute, and hour.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TimeTest4 extends JApplet implements ActionListener {
private Time3 time;

public void init()
time= new Time3(); //create Time3 object

//get applet's container pane and change its layout to FlowLayout
Container container= getContentPane();
Container.setLayout (new FlowLayout() );

//set up hourlabel and hourfield
hourLabel = new JLabel("Set Hour");
hourField = new JTextField (10);
container.add (hourLabel) ;
container.add (hourField) ;

//set up minutelabel and minutefield
minuteLabel = new JLabel("Set Minute");
minuteField = new JTextField (10);
container.add (minuteLabel) ;
container.add (minuteField) ;

//set up secondlabel and secondlabel
secondLabel = new JLabel("Set Second");
secondField = new JTextField (10);
container.add (secondLabel) ;
container.add (secondField) ;

//set up displayField
displayField= new JTextField(30);

//set up tickButton

tickButton = new JButton("Add 1 to Second");

tickButton1=new JButton("Add 1 to Minute");

tickButton2=new JButton("Add 1 to Hour");

hourField.addActionListener( this);
minuteField.addActionListener( this);
secondField.addActionListener( this);
tickButton.addActionListener( this);
tickButton1.addActionListener( this);
tickButton2.addActionListener( this);


public void actionPerformed(ActionEvent event)
if( event.getSource() ==tickButton)

//process hourfield event
else if (event.getSource()==hourField) {
time.setHour(Integer.parseInt (event.getActionCommand() ) );

else if (event.getSource()==minuteField) {
time.setMinute(Integer.parseInt (event.getActionCommand() ) );

else if (event.getSource()==secondField) {
time.setSecond(Integer.parseInt (event.getActionCommand() ) );
} //end method actionPerformed

//update displayField and applet container's status bar
public void displayTime()
displayField.setText("Hour:" + time.getHour() + ":Minute:" +
time.getMinute() + ":Second:" + time.getSecond() );

showStatus("Standard time is:" + time.toStandardString() +
": Universal time is: " + time.toUniversalString() );

} //end method displayTime

//add one to second and update hour/minute if necessary
public void tick()
time.setSecond((time.getSecond() + 1) % 60 );

if(time.getSecond() ==0) {
time.setMinute( ( time.getMinute() + 1) % 60);

time.setHour((time.getHour() +1) %24);

}//end method tick

public void ticky()
time.setMinute( ( time.getMinute() + 1) % 60);

time.setHour((time.getHour() +1) %24);

}//end method tick

public void ticker()
time.setHour((time.getHour() +1) %24);

}//end method tick

}// end class TimeTest4

I know i probably need to make a driver program that brings in the timetest3 and timetest4 to run it


Harry Armadillo
07-07-2005, 05:53 AM
1. We don't do your homework.

2. As you've been told before, java != javascript.

07-07-2005, 02:04 PM
i know you guys wont do my homework. i was just asking for some help and some ideas that can get me along with it.

The reason i dont have anything down is because i really have no idea what i am doing. Java confuses me so badly that i get headaches, i really dont understand any of it and it seems to get harder and harder each week.

I know for this driver program that i need to bring in the timetest3 and timetest4, but i would think that timetest 4 pretty much does most of the work that is need for the program.

Should i just create a "Driver" program that does the work of timetest4, pretty much just cut the information from it and then work on making it an execulatable program.


can you recommend any good forums for Java only???

07-07-2005, 05:24 PM

Java Forums (http://www.google.co.uk/search?hl=en&q=java+forums&meta=)