Question:
how can i make this java code to do what i want?
lookadistraction
2008-09-28 22:50:34 UTC
i am trying to Write a class declaration for a class Clock. It should have instance variables for hours, minutes, seconds (all integers) and methods to set each of these values to an integer value. It should also have a checkTime() method that verifies that the values stored in hours, minutes, and seconds are valid (not too large or negative) and prints out:

The time is valid. or The time is invalid.

Finally, it needs a getTime() method to show the time in the format shown:

The time is 3:45:00

Write a separate ClockTestDrive class to:

* create an instance of a Clock
* set the hours, minutes, and seconds to random (but valid) numbers
* verify the validity of the time using checkTime()
* show the time using getTime()

i am having trouble making it so that i prints out correctly right now i prints out 1:045:7

here are my two files

class Clock
{
int hour;
int min;
int sec;
String col = ":";
String zero = "0";


void checkTime( )
{

if ( hour>0 && min<=59 && sec<=59 )
{
System.out.println( "The time is valid." );
}
else
{
System.out.println( "The time is invalid." );
}

}
void time( )
{
if (min<10 || sec<10 )
{
System.out.println( hour + col + zero + min + col+ zero + sec);
}

else
{
System.out.println( hour + col + min + col + sec );
}

}
}

and

class ClockTestDrive
{
public static void main( String[] args )
{

Clock a = new Clock();
a.hour = ( int )( Math.random( )*12 );
a.min = ( int )( Math.random( )*59 );
a.sec = ( int )( Math.random( )*59 );
a.time( );
a.checkTime( );



} //end main
} //end class ClockTestDriv

when i do this it keeps on making that mistake. i might be able to use a while statement not sure if i can
Three answers:
bjb.dragon
2008-09-28 23:33:08 UTC
You have to check for < 10 for each of the sets of digits. Sounds like the job for a function... it's called 'digit' in my correction.



You also had an off-by-one error, easy enough to fix.



This is what Math.random() will return, in Interval Notation:

Math.random() = [0.0, 1.0)



(int)(Math.random( )*1) = {0}

(int)(Math.random( )*12) = {0, 1, ... 11} + 1 = {1, 2, ... 12}

(int)(Math.random( )*60) = {0, 1, ... 59}



Hopefully you see what I mean. You need to multiply by one more than the max value you want, assuming the sequence starts at 0. Which is NOT what you want for hours, as zero oh clock doesn't exist. So make it a number from 0 to 11, by (int)(Math.random()*12), then add a + 1.



Correction is below:



class Clock

{

int hour;

int min;

int sec;

String col = ":";

String zero = "0";



public static String digit ( int number ) {

if (number < 10)

return "0" + number;

else

return "" + number;

}



void checkTime( )

{



if ( hour>0 && min<=59 && sec<=59 )

{

System.out.println( "The time is valid." );

}

else

{

System.out.println( "The time is invalid." );

}



}



void time( )

{

System.out.println( digit(hour) + col + digit(min) + col + digit(sec) );

}



}



class ClockTestDrive

{

public static void main( String[] args )

{



Clock a = new Clock();

for(int i=0; i<20;i++){

a.hour = ( int )( Math.random( )*12) + 1;

a.min = ( int )( Math.random( )*60 );

a.sec = ( int )( Math.random( )*60 );

a.time( );

a.checkTime( );

}







} //end main

} //end class ClockTestDriv
saintmarl
2008-09-29 06:35:15 UTC
As answered above, your if condition in method time() is erroneous.



To revise your code, change the ff. lines:



if (min<10 || sec<10 )

{

System.out.println( hour + col + zero + min + col+ zero + sec);

}



else

{

System.out.println( hour + col + min + col + sec );

}



Into this SINGLE line:



System.out.println(hour + col + (min<10 ? zero : "") + min + col + (sec<10 ? zero : "") + sec);



It's not code-readable but it does the trick!



It is called a single-line if, just like in C++.

condition ? process_true : process_false



So in your example:

if (min < 10) {

System.out.println(zero+min);

} else {

System.out.println(min);

}



can be written simply as :

System.out.println(min < 10 ? zero+min : min);



Enjoy.
Anh Nguyen
2008-09-29 06:06:25 UTC
your if/else statement in the time function is funky



you need:

if (min < 10 && sec < 10) { ... }

else if (min < 10) { ... }

else if (sec < 10) { ... }

else System.out.println( hour + col + min + col + sec );



make sure it's in that order so it can short circuit correctly



edit: making it easier to read


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...