//class Clock // I need help to add the two clock times together and subtract it
ID: 3771917 • Letter: #
Question
//class Clock
// I need help to add the two clock times together and subtract it I am having problems with it can someone please help me i posted another problem just like this but this is the orginal code and i really need help on it thank you vry much
public class Clock
{
private int hr; //store hours
private int min; //store minutes
private int sec; //store seconds
//Postcondition: hr = 0; min = 0; sec = 0
public Clock()
{
setTime(0, 0, 0);
System.out.println("Inside the version with NO parameters!");
}
public Clock(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
System.out.println("Inside the version with parameters!");
}
//Method to set the time
//The time is set according to the parameters
//Postcondition: hr = hours; min = minutes; sec = seconds
public void setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
//Method to return the hours
//Postcondition: The value of hr is returned.
public int getHours()
{
return hr;
}
//Method to return the minutes
//Postcondition: The value of min is returned.
public int getMinutes()
{
return min;
}
//Method to return the seconds
//Postcondition: The value of sec is returned.
public int getSeconds()
{
return sec;
}
//Method to print the time
//Postcondition: Time is printed in the form hh:mm:ss.
public void printTime()
{
if (hr < 10)
System.out.print("0");
System.out.print(hr + ":");
if (min < 10)
System.out.print("0");
System.out.print(min + ":");
if (sec < 10)
System.out.print("0");
System.out.print(sec);
}
//Method to increment the time by one second
//Postcondition: The time is incremented by one second.
//If the before-increment time is 23:59:59, the time
//is reset to 00:00:00.
public void incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes(); //increment minutes
}
}
//Method to increment the time by one minute
//Postcondition: The time is incremented by one minute.
//If the before-increment time is 23:59:53, the time
//is reset to 00:00:53.
public void incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours(); //increment hours
}
}
//Method to increment the time by one hour
//Postcondition: The time is incremented by one hour.
//If the before-increment time is 23:45:53, the time
//is reset to 00:45:53.
public void incrementHours()
{
hr++;
if (hr > 23)
hr = 0;
}
//Method to compare the two times
//Postcondition: Returns true if this time is equal to
// otherClock; otherwise returns false
public boolean equals(Clock otherClock)
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
//Method to copy the time
//Postcondition: The instance variables of otherClock are
// copied into the corresponding data members
// of this time.
// hr = otherClock.hr; min = otherClock.min;
// sec = otherClock.sec.
public void makeCopy(Clock otherClock)
{
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
//Method to return a copy of the time
//Postcondition: A copy of the object is created
// and a reference of the copy is returned.
public Clock getCopy()
{
Clock temp = new Clock();
temp.hr = hr;
temp.min = min;
temp.sec = sec;
return temp;
}
public String toString()
{
String str = "";
if (hr < 10)
str = "0";
str = str + hr + ":";
if (min < 10)
str = str + "0" ;
str = str + min + ":";
if (sec < 10)
str = str + "0";
str = str + sec;
return str;
}
public void addTimes(Clock a, Clock b)
{
hr=7;
min=7;
sec=7;
}
}
import java.util.*;
public class TestProgClock
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
Clock myClock = new Clock(5,4,30); //Line 1
Clock yourClock = new Clock(0,0,0); //Line 2
Clock sumClock = new Clock(0,0,0);
int hours; //Line 3
int minutes; //Line 4
int seconds; //Line 5
System.out.println("Line 6: myClock: "
+ myClock); //Line 6
System.out.println("Line 7: yourClock: "
+ yourClock); //Line 7
yourClock.setTime(5,45,16); //Line 8
System.out.println("Line 13: After setting the "
+ "time, yourClock: "
+ yourClock); //Line 9
if (myClock.equals(yourClock)) //Line 10
System.out.println("Line 11: Both times "
+ "are equal."); //Line 11
else //Line 12
System.out.println("Line 13: The two times "
+ "are not equal"); //Line 13
System.out.print("Line 14: Enter the hours, "
+ "minutes, and seconds: "); //Line 14
hours = console.nextInt(); //Line 15
minutes = console.nextInt(); //Line 16
seconds = console.nextInt(); //Line 17
System.out.println();
myClock.setTime(hours,minutes,seconds); //Line 19
System.out.println("Line 20: New myClock: "
+ myClock); //Line 20
myClock.incrementSeconds(); //Line 21
//Line 18
System.out.printf("Line 22: After incrementing "
+ "the clock by one second, "
+ "myClock: %s%n", myClock); //Line 22
yourClock.makeCopy(myClock); //Line 23
System.out.println("Line 24: After copying "
+ "myClock into yourClock, "
+ "yourClock: " + yourClock); //Line 24
System.out.println("MyClock= " + myClock); //Line 18
sumClock.addTimes(yourClock, myClock);
System.out.println("sumClock= " + sumClock);
System.out.println("MyClock= " + myClock); //Line 18
myClock.subTimes(yourClock, myClock);
System.out.println("diffClock= " + myClock);
}//end main
}
Explanation / Answer
Note :
check the desired methods highligted
Code:
import java.util.*;
class Clock
{
private int hr; //store hours
private int min; //store minutes
private int sec; //store seconds
//Postcondition: hr = 0; min = 0; sec = 0
public Clock()
{
setTime(0, 0, 0);
System.out.println("Inside the version with NO parameters!");
}
public Clock(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
System.out.println("Inside the version with parameters!");
}
//Method to set the time
//The time is set according to the parameters
//Postcondition: hr = hours; min = minutes; sec = seconds
public void setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
//Method to return the hours
//Postcondition: The value of hr is returned.
public int getHours()
{
return hr;
}
//Method to return the minutes
//Postcondition: The value of min is returned.
public int getMinutes()
{
return min;
}
//Method to return the seconds
//Postcondition: The value of sec is returned.
public int getSeconds()
{
return sec;
}
//Method to print the time
//Postcondition: Time is printed in the form hh:mm:ss.
public void printTime()
{
if (hr < 10)
System.out.print("0");
System.out.print(hr + ":");
if (min < 10)
System.out.print("0");
System.out.print(min + ":");
if (sec < 10)
System.out.print("0");
System.out.print(sec);
}
//Method to increment the time by one second
//Postcondition: The time is incremented by one second.
//If the before-increment time is 23:59:59, the time
//is reset to 00:00:00.
public void incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes(); //increment minutes
}
}
//Method to increment the time by one minute
//Postcondition: The time is incremented by one minute.
//If the before-increment time is 23:59:53, the time
//is reset to 00:00:53.
public void incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours(); //increment hours
}
}
//Method to increment the time by one hour
//Postcondition: The time is incremented by one hour.
//If the before-increment time is 23:45:53, the time
//is reset to 00:45:53.
public void incrementHours()
{
hr++;
if (hr > 23)
hr = 0;
}
//Method to compare the two times
//Postcondition: Returns true if this time is equal to
// otherClock; otherwise returns false
public boolean equals(Clock otherClock)
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
//Method to copy the time
//Postcondition: The instance variables of otherClock are
// copied into the corresponding data members
// of this time.
// hr = otherClock.hr; min = otherClock.min;
// sec = otherClock.sec.
public void makeCopy(Clock otherClock)
{
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
//Method to return a copy of the time
//Postcondition: A copy of the object is created
// and a reference of the copy is returned.
public Clock getCopy()
{
Clock temp = new Clock();
temp.hr = hr;
temp.min = min;
temp.sec = sec;
return temp;
}
public String toString()
{
String str = "";
if (hr < 10)
str = "0";
str = str + hr + ":";
if (min < 10)
str = str + "0" ;
str = str + min + ":";
if (sec < 10)
str = str + "0";
str = str + sec;
return str;
}
public void addTimes(Clock a, Clock b)
{
hr = a.hr+b.hr;
min= a.min+b.min;
sec= a.sec+b.sec;
if(sec>59)
{
sec=sec-60;
min++;
}
if(min>59)
{
min=min-60;
hr++;
}
hr = hr%24;
}
public void subTimes(Clock a, Clock b)
{
hr = a.hr-b.hr;
min= a.min-b.min;
sec= a.sec-b.sec;
if(sec<0)
{
sec=sec+60;
min--;
}
if(min<0)
{
min=min+60;
hr--;
}
if(hr<0)
hr = hr+24;
}
}
public class TestProgClock
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
Clock myClock = new Clock(5,4,30); //Line 1
Clock yourClock = new Clock(0,0,0); //Line 2
Clock sumClock = new Clock(0,0,0);
int hours; //Line 3
int minutes; //Line 4
int seconds; //Line 5
System.out.println("Line 6: myClock: "
+ myClock); //Line 6
System.out.println("Line 7: yourClock: "
+ yourClock); //Line 7
yourClock.setTime(5,45,16); //Line 8
System.out.println("Line 13: After setting the "
+ "time, yourClock: "
+ yourClock); //Line 9
if (myClock.equals(yourClock)) //Line 10
System.out.println("Line 11: Both times "
+ "are equal."); //Line 11
else //Line 12
System.out.println("Line 13: The two times "
+ "are not equal"); //Line 13
System.out.print("Line 14: Enter the hours, "
+ "minutes, and seconds: "); //Line 14
hours = console.nextInt(); //Line 15
minutes = console.nextInt(); //Line 16
seconds = console.nextInt(); //Line 17
System.out.println();
myClock.setTime(hours,minutes,seconds); //Line 19
System.out.println("Line 20: New myClock: "
+ myClock); //Line 20
myClock.incrementSeconds(); //Line 21
//Line 18
System.out.printf("Line 22: After incrementing "
+ "the clock by one second, "
+ "myClock: %s%n", myClock); //Line 22
yourClock.makeCopy(myClock); //Line 23
System.out.println("Line 24: After copying "
+ "myClock into yourClock, "
+ "yourClock: " + yourClock); //Line 24
System.out.println("MyClock= " + myClock); //Line 18
sumClock.addTimes(yourClock, myClock);
System.out.println("sumClock= " + sumClock);
System.out.println("MyClock= " + myClock); //Line 18
myClock.subTimes(yourClock, myClock);
System.out.println("diffClock= " + myClock);
}//end main
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.