//Clock.java
public class Clock {
//3 private ints to represent hours, minutes, and seconds.
int hours;
int minutes;
int seconds;
//a default constructor that always initializes the hour/minute/second to 0,0,0 (midnight).
public Clock()
{
hours=0;
minutes=0;
seconds=0;
}
/*
a parameterized constructor that receives the hour, minute, second as 3 ints and sets
the values in the instance to whatever is received.
*/
public Clock(int h, int m, int s)
{
/*If the hour is illegal (negative or > 23) or if the minute or the second are illegal (negative or >
59),
use the following statement to throw an exception: throw new IllegalArgumentException*/
if(h>23 || m>59 || s>59)
throw new IllegalArgumentException(\"Wrong time to set\") ;
hours=h;
minutes=m;
seconds=s;
}
//copy constructor
public Clock(Clock C)
{
hours=C.hours;
minutes=C.minutes;
seconds=C.seconds;
}
//adds numSeconds to how many seconds it has (and fixes the hour/min/sec
//so that they “carry” to the next unit if possible)
void addSeconds(int numSeconds)
{
seconds+=numSeconds;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
/*
Tick : increments itself by 1 second (and fixes the hour/min/sec so they “carry” to the next unit
if possible).
*/
void tick()
{
seconds+=1;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
// returns true if it is “night” (between 8PM and 5AM inclusive), returns false otherwise.
boolean isNight()
{
if((hours>=20 && hours <=23)||(hours>=0 && hours<=5))
{
return true;
}
else
return false;
}
//returns a representation of the current instance in the form hh:mm:ss AMorPM
public String toString()
{
String time=\"\";
String ampm=\"\";
if(hours == 0)
{
time=time +\"12\";
ampm=\"AM\";
}
else if( hours>=1 && hours<=11)
{
time=time+ hours;
ampm=\"AM\";
}
else if(hours == 12)
{
time=time+\"12\";
ampm=\"PM\";
}
else if(hours>=13 && hours<=23)
{
time=time+(hours-12);
ampm=\"PM\";
}
if(minutes<10)
{
time+=\":0\"+minutes;
}
else
{
time+=\":\"+minutes;
}
if(seconds<10)
{
time+=\":0\"+seconds;
}
else
{
time+=\":\"+seconds;
}
time+=\" \"+ampm;
return time;
}
}
///////////////////////////////////////////////////////
//driver
public class ClockDriver {
public static void main(String[] args) {
try{
Clock clock1=new Clock(13,29,45);
System.out.println(\"clock1: \"+clock1.toString());
clock1.addSeconds(34);
System.out.println(\"clock1 after adding 34 seconds : \"+clock1.toString());
clock1.tick();
System.out.println(\"clock1 after tick : \"+clock1.toString());
Clock clock2=new Clock(25,45,50);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Solution
//Clock.java
public class Clock {
//3 private ints to represent hours, minutes, and seconds.
int hours;
int minutes;
int seconds;
//a default constructor that always initializes the hour/minute/second to 0,0,0 (midnight).
public Clock()
{
hours=0;
minutes=0;
seconds=0;
}
/*
a parameterized constructor that receives the hour, minu.
Andreas Schleicher presents at the launch of What does child empowerment mean...
Clock.java public class Clock { 3 private ints to represent .pdf
1. //Clock.java
public class Clock {
//3 private ints to represent hours, minutes, and seconds.
int hours;
int minutes;
int seconds;
//a default constructor that always initializes the hour/minute/second to 0,0,0 (midnight).
public Clock()
{
hours=0;
minutes=0;
seconds=0;
}
/*
a parameterized constructor that receives the hour, minute, second as 3 ints and sets
the values in the instance to whatever is received.
*/
public Clock(int h, int m, int s)
{
/*If the hour is illegal (negative or > 23) or if the minute or the second are illegal (negative or >
59),
use the following statement to throw an exception: throw new IllegalArgumentException*/
if(h>23 || m>59 || s>59)
throw new IllegalArgumentException("Wrong time to set") ;
hours=h;
minutes=m;
seconds=s;
}
//copy constructor
public Clock(Clock C)
{
hours=C.hours;
minutes=C.minutes;
seconds=C.seconds;
}
2. //adds numSeconds to how many seconds it has (and fixes the hour/min/sec
//so that they “carry” to the next unit if possible)
void addSeconds(int numSeconds)
{
seconds+=numSeconds;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
/*
Tick : increments itself by 1 second (and fixes the hour/min/sec so they “carry” to the next unit
if possible).
*/
void tick()
{
seconds+=1;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
// returns true if it is “night” (between 8PM and 5AM inclusive), returns false otherwise.
boolean isNight()
3. {
if((hours>=20 && hours <=23)||(hours>=0 && hours<=5))
{
return true;
}
else
return false;
}
//returns a representation of the current instance in the form hh:mm:ss AMorPM
public String toString()
{
String time="";
String ampm="";
if(hours == 0)
{
time=time +"12";
ampm="AM";
}
else if( hours>=1 && hours<=11)
{
time=time+ hours;
ampm="AM";
}
else if(hours == 12)
{
time=time+"12";
ampm="PM";
}
else if(hours>=13 && hours<=23)
{
time=time+(hours-12);
ampm="PM";
}
if(minutes<10)
{
time+=":0"+minutes;
5. }
}
Solution
//Clock.java
public class Clock {
//3 private ints to represent hours, minutes, and seconds.
int hours;
int minutes;
int seconds;
//a default constructor that always initializes the hour/minute/second to 0,0,0 (midnight).
public Clock()
{
hours=0;
minutes=0;
seconds=0;
}
/*
a parameterized constructor that receives the hour, minute, second as 3 ints and sets
the values in the instance to whatever is received.
*/
public Clock(int h, int m, int s)
{
/*If the hour is illegal (negative or > 23) or if the minute or the second are illegal (negative or >
59),
use the following statement to throw an exception: throw new IllegalArgumentException*/
if(h>23 || m>59 || s>59)
throw new IllegalArgumentException("Wrong time to set") ;
hours=h;
minutes=m;
seconds=s;
}
//copy constructor
public Clock(Clock C)
6. {
hours=C.hours;
minutes=C.minutes;
seconds=C.seconds;
}
//adds numSeconds to how many seconds it has (and fixes the hour/min/sec
//so that they “carry” to the next unit if possible)
void addSeconds(int numSeconds)
{
seconds+=numSeconds;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
}
}
}
/*
Tick : increments itself by 1 second (and fixes the hour/min/sec so they “carry” to the next unit
if possible).
*/
void tick()
{
seconds+=1;
if(seconds>60)
{
minutes+=seconds/60;
seconds%=60;
if(minutes>60)
{
hours+=minutes/60;
minutes%=60;
7. }
}
}
// returns true if it is “night” (between 8PM and 5AM inclusive), returns false otherwise.
boolean isNight()
{
if((hours>=20 && hours <=23)||(hours>=0 && hours<=5))
{
return true;
}
else
return false;
}
//returns a representation of the current instance in the form hh:mm:ss AMorPM
public String toString()
{
String time="";
String ampm="";
if(hours == 0)
{
time=time +"12";
ampm="AM";
}
else if( hours>=1 && hours<=11)
{
time=time+ hours;
ampm="AM";
}
else if(hours == 12)
{
time=time+"12";
ampm="PM";
}
else if(hours>=13 && hours<=23)
{
time=time+(hours-12);