Help with Clock (advance and reverse day,hour,minute)
FYI - This is my first post. This is an assignment for a JAVA I class. I cannot import anything or change the public class. My problem right now focusing on the ADVANCE method, I don't know how to advance the day. So my question is how should I get a if/then, for, or while component in there to anchor my advancing minutes to my days?
Code Java:
public class ClockDriver {
public static void main(String[] args) {
Clock MyClock = new Clock("Thursday", 12,0); // Instantiate a clock object
System.out.println(MyClock); // Should display: Thursday, 12:00 p.m.
System.out.println(); // print blank line
MyClock.advance(150); // advance time 150 minutes (2 hours 30 minutes)
System.out.println(MyClock); // Should display: Thursday, 2:30 p.m.
System.out.println();
MyClock.advance(585); // advance time by 585 minutes (9 hours 45 minutes)
System.out.println(MyClock); // Should display: Friday, 12:15 a.m.
System.out.println();
MyClock.advance(10080); // advance time by 10080 minutes (7 days)
System.out.println(MyClock); // Should display: Friday, 12:15 a.m.
System.out.println();
MyClock.reverse(585); // reverse time by 585 minutes (9 hours 45 minutes)
System.out.println(MyClock); // Should display: Thursday, 2:30 p.m.
System.out.println();
MyClock.reverse(17280); // reverse time by 17280 minutes (12 days)
System.out.println(MyClock); // Should display: Saturday, 2:30 p.m.
System.out.println();
} // end of main
} // end of class ClockDriver
class Clock{
/***************************************************************
* Objects of this class represent time (in hours and minutes) *
* The time data is stored in 24 hour (HHMM military format) *
***************************************************************/
private int day_of_week; // valid range of values is 1 thru' 7
private int hours; // range of values is 0 thru' 23
private int minutes; // range of values is 0 thru' 59
private String[] dow = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
public Clock(String s, int h, int m){
//Initialize day_of_week, hours and minutes - 20 points
if (s.equals("Sunday")) {
day_of_week = 1;
}
else if (s.equals("Monday")) {
day_of_week = 2;
}
else if (s.equals("Tuesday")) {
day_of_week = 3;
}
else if (s.equals("Wednesday")) {
day_of_week = 4;
}
else if (s.equals("Thursday")) {
day_of_week = 5;
}
else if (s.equals("Friday")) {
day_of_week = 6;
}
else if (s.equals("Saturday")) {
day_of_week = 7;
} else {
day_of_week = -1;
}
hours = h;
minutes = m;
}
public void advance(int m){
// advance day_of_week, hours and minutes - 30 points
int allMinA = (hours * 60) + minutes + m;
minutes = allMinA % 60;
hours = (allMinA / 60) % 24;
}
public void reverse(int m){
// reverse day_of_week, hours and minutes - 30 points
int allMinB = (hours * 60) + minutes - m;
minutes = allMinB % 60;
hours = (allMinB / 60) % 24;
}
public String toString(){
//return Day of the week, hours and minutes as a string to be displayed - 20 points
if (hours >12){
hours = hours - 12;
}
String result = dow[day_of_week - 1] + ", " + String.format("%2d", hours) + ":" + String.format("%02d", minutes) + " amORpm > ";
return result;
}
} // end of class Clock
Re: Help with Clock (advance and reverse day,hour,minute)
You seem to be figuring out how to advance the hours based on the minutes, so can't you do the same thing, advance the days based on the hours?
Re: Help with Clock (advance and reverse day,hour,minute)
I can visualize a set of total minutes that I can divide by to find out how many days, hours, and minutes but... the way these methods are set up I am having a hard time getting total minutes. Because hours needs to be set to zero after the first advance otherwise it adds unnecessarily to totalMinutes. So below I have a new approach. Once again any suggestions would be appreciated.
Code Java:
public void advance(int m){
// advance day_of_week, hours and minutes - 30 points
minutes = minutes + m;
if (minutes < 60){
} else {
hours = hours + (minutes / 60);
minutes = minutes % 60;
if (hours < 24){
} else {
int addDays = day_of_week + (hours / 24);
if (addDays < 8){
day_of_week = addDays;
} else {
int weekAdds = addDays - 8;
day_of_week = weekAdds;
}
}
}
}
Re: Help with Clock (advance and reverse day,hour,minute)
So is that working now? If not, what happens? What did you expect to happen?
Re: Help with Clock (advance and reverse day,hour,minute)
No, the code in my previous post doesn't work. I can't keep track of the days in any consistent fashion with that approach. My problem is I don't understand the best approach to keep track of time in this program. I need a counter that knows the starting point and can keep track of minutes (0-59) and hours (0-23) and days (1-7). I think I am now going to stop keeping track of total minutes and try to keep track of total hours. Hours are what I need to know when I need to add ++ to days.
----------
For example, if I start at Thursday at 12:00pm and time advances by 10 days (240 hrs or 14,400 mins) then the clock would stay at 12:00pm but the day should be Sunday. If Sun = 1, Mon = 2, .... Sat = 7, then I have (Thursday) 5 + 10 (days) = 15. But how does that 15 help me?
Re: Help with Clock (advance and reverse day,hour,minute)
Think of Sunday as zero, and Saturday as six.
That means Thursday is 4. Add 10 days, and you're at 14. What index do you want? What if you had added 15 days? 16? Are you starting to see a pattern?
Re: Help with Clock (advance and reverse day,hour,minute)
Quote:
Originally Posted by
KevinWorkman
Think of Sunday as zero, and Saturday as six.
That means Thursday is 4. Add 10 days, and you're at 14. What index do you want? What if you had added 15 days? 16? Are you starting to see a pattern?
I'm with you having it start at 0 but in his instructions he says that valid ranges for the days are 1 - 7. But I understand your point.
If Thursday is 4 and I add 10 days I'm at 14 for the number of days. The actual day would be Sunday.
4-thurs(4)
5-fri(5)
6-sat(6)
0-sun(7)
1-mon(8)
2-tues(9)
3-wed(10)
4-thurs(11)
5-fri(12)
6-sat(13)
0-sun(14)
How would I get the "0" (for Sunday) I need from the integer 14?
Re: Help with Clock (advance and reverse day,hour,minute)
I'm much happier with this method for keeping track of the correct hours but I am still having issues keeping track of days. Any help out there? Kevin? Anyone! lol
Code Java:
public void advance(int m){
// advance day_of_week, hours and minutes - 30 points
minutes = minutes + m;
if(minutes > 59){
int spillOverMins = minutes % 60;
int spillOverHours = minutes / 60;
hours = hours + spillOverHours;
minutes = spillOverMins;
} else {
// not sure if I need something here
}
if (hours > 23){
int spillOverHours2 = hours % 24;
int spillOverDays = hours / 24;
hours = hours + spillOverHours2;
if(spillOverDays + day_of_week > 7){
// what to put here that will account for the original day_of_week
} else {
day_of_week = day_of_week + spillOverDays;
}
}
}
Re: Help with Clock (advance and reverse day,hour,minute)
Have you considered the % operator?
Chris
Re: Help with Clock (advance and reverse day,hour,minute)
The advance method works:
Code Java:
public void advance(int m){
// advance day_of_week, hours and minutes - 30 points
minutes = minutes + m;
if(minutes > 59){
int spillOverMins = minutes % 60;
int spillOverHours = minutes / 60;
hours += spillOverHours;
minutes = spillOverMins;
} // END if(minutes > 59){
if (hours > 23){
int spillOverHours2 = hours % 24;
int spillOverDays = hours / 24;
hours = spillOverHours2;
day_of_week += spillOverDays;
} // END if (hours > 23){
if (day_of_week > 7) {
day_of_week = day_of_week%7;
if (day_of_week == 0) day_of_week = 7;
}// END if (day_of_week > 7) {
} // END advance()
but the reverse is not as easy to figure out. What is best way to get keep of time going backwards, in this case?
Code Java:
public void reverse(int m){
// reverse day_of_week, hours and minutes - 30 points
int backMins = m % 60;
int backHrs = m / 60;
int backDays = backHrs / 24;
int addBackHr = 0;
int addBackDay = 0;
int minCheck = minutes - backMins;
if (minCheck < 0) {
addBackHr = 1;
minutes = minCheck * -1;
} else {
minutes = minCheck;
} // END if (minCheck < 0) {
int hrCheck = hours - (backHrs + addBackHr);
if (hrCheck < 0) {
hrCheck *= -1;
if (hrCheck > 24) hrCheck = (backHrs + addBackHr) % 24;
if (hours == 0) hours = 24;
hours = hours - hrCheck;
} else {
hours = hours - hrCheck;
}// END if (tmpHoldHrs < 0) {
day_of_week -= (backDays + addBackDay);
if(day_of_week == 0) day_of_week = 1;
if (day_of_week < 0){
day_of_week *= -1;
if(day_of_week > 7) day_of_week = day_of_week % 7;
}
} // END reverse()