time validator

I am trying to come up with some way of producing an applet that will deal with the following:

In this task you are required to design, create and test a new class called newTime.

1. Design a solution for a new class called newTime.

a. public boolean valid24Time (int hh, int mm)

The hours (hh) can only be in the range 0-23 and the

minutes (mm) can only be in the range 0-59.

This method must return true if the time is valid and false

if the time is invalid.

b. public String convert24ToStd (int hh, int mm)

The time is passed to the method as hours (hh) and minutes

(mm)with the time in 24

hour format. The 24 hour format must be changed into standard

format in the form

hh:mm AM for a morning time and hh:mm PM for an afternoon time.

This method must return a string containing the time in

standard format.

c. public int convertStdTo24 (String hhmm)

The time is passed to the method as a string in standard format

in the form hh:mm

AM for a morning time and hh:mm PM for an afternoon time. The

standard formatmust be changed into 24 hour format.

This method must return an integer containing the time in 24

hour format.

d. public int addTime (int hh1, int mm1, int hh2, int mm2)

Two times are passed to the method in 24 hour format as hours

and minutes (hh1 and mm1, hh2 and mm2). The two times must be

added together.

This method returns the calculated time as an integer in 24

hour format.

e. public int subtractTime (int hh1, int mm1, int hh2, int mm2)

Two times are passed to the method in 24 hour format as hours

and minutes (hh1 and mm1, hh2 and mm2). The second time must be

subtracted from the first.

This method must return the calculated time as an integer in 24

hour format.

2. Error handling routines must be included to deal with exceptions.

3. Write the code for the newTime class.

4. Design and create a testnewTime class which tests the methods in the newTime class.

Document the layout of any input and output screens used.

5. Create test data to test the newTime class and determine the expected results.

6. Prepare a test plan.

7. Test the software, compare the actual results to the expected results keeping a log for

each test which identifies any discrepancies between actual and expected results and

records any amendments made to correct errors.

8. Produce technical documentation to describe the class interface and purpose and

operation of the newTime class.

9. Produce a printed program listing.

Any help would be grateful

Regards

Rary Geddi

[2775 byte] By [rarygeddia] at [2007-9-29 2:11:23]
# 1
Seems quite straightforward to me, except that you're trying to produce an applet without any description of its visible user interface. Anyway, can you be more specific about your actual problem?
DrClapa at 2007-7-13 18:29:03 > top of Java-index,Other Topics,Algorithms...
# 2

Its not that simple to me. The first problem I have is 'public boolean'.

I haven't got a clue what to put here.

Have thought....

public class newTime {

private int hour;

private int min;

public boolean valid24Time ( int hh, int mm)

{

hour = (( hh>= 0 && hh<24) ? true : false);

min = (( mm>= 0 && mm <60) ? true : false);

}

public String convert24ToStd( int hh, int mm)

{

DecimalFormat twoDigits = new DecimalFormat ("00");

Return (( getHour()== 12 || getHour ()==0);

12:getHour()%12)+":"+

twoDigits.format (getMinute()) +":"+

(getHour()<12? "AM":"PM");

}

public int getHour ()

{

return hour;

}

public int getMinute ()

{

return min;

}

}

Thats as much as I have at the moment and I'm not sure if its correct or not. Your comments?

Thanks

rarygeddia at 2007-7-13 18:29:03 > top of Java-index,Other Topics,Algorithms...
# 3

A shorter way of coding

(( hh>= 0 && hh<24) ? true : false)

is simply

( hh>= 0 && hh<24)

And the result of that is a boolean value, not an int value, so you'll want to assign it to a boolean variable instead of an int variable. Then your valid24time method will need to include a return statement that provides its value. And your variable names "hour" and "min" aren't much good because they don't describe their purpose (which is to tell if the hour and minute components are valid). Names like "isHourValid" would be better. And those variables are only local to the valid24time method, so they don't need to be instance variables of the class. Taking all those things together you get this:public boolean valid24Time ( int hh, int mm)

{

boolean isHourValid = ( hh>= 0 && hh<24);

boolean isMinValid = ( mm>= 0 && mm <60);

return (isHourValid & isMinValid);

}

Hope that gets you started. Carry on from there.

And by the way, if your assignment doesn't tell you to make an applet then don't make one.

DrClapa at 2007-7-13 18:29:03 > top of Java-index,Other Topics,Algorithms...
# 4
Thanks very much for your help.Why not use an applet?
rarygeddia at 2007-7-13 18:29:03 > top of Java-index,Other Topics,Algorithms...
# 5

I've have tried all ways to get this to work but with out any luck.

What am I doing wrong?

public class Frame1 extends JFrame {

String inputHour;

String inputMin;

int hh = 0;

int mm = 0;

boolean isHourValid;

boolean isMinValid;

public boolean valid24Time ( int hh, int mm)

{ boolean isHourValid = ( hh>= 0 && hh<24);

boolean isMinValid = ( mm>= 0 && mm <60);

return (isHourValid & isMinValid);

}

private JPanel contentPane;

private JPanel jPanel1 = new JPanel();

private JButton jButton1 = new JButton();

private JButton jButton2 = new JButton();

private JButton jButton3 = new JButton();

private JButton jButton4 = new JButton();

private JButton jButton5 = new JButton();

private GridBagLayout gridBagLayout1 = new GridBagLayout();

private GridBagLayout gridBagLayout2 = new GridBagLayout();

void jButton1_actionPerformed(ActionEvent e) {

inputHour = JOptionPane.showInputDialog("Enter Hour");

hh = Integer.parseInt(inputHour);

inputMin = JOptionPane.showInputDialog("Enter minute");

mm = Integer.parseInt(inputMin);

if ( isHourValid & isMinValid )

JOptionPane.showMessageDialog

(null,"Time invalid" +hh + mm,"Invalid time", JOptionPane.PLAIN_MESSAGE);

else

JOptionPane.showMessageDialog

(null, "Time valid" +hh + mm, "Valid time", JOptionPane.PLAIN_MESSAGE);

}

}

rarygeddia at 2007-7-13 18:29:03 > top of Java-index,Other Topics,Algorithms...