Hey all so I am new to this website and Java in general. I have some C++ experience and some BASIC but that's about it. Anyways, I am a CS major and I am a Java class that focuses heavily on OOP. We are using the IDE BlueJ (Not sure if you have heard of it, seems like an obscure only for learning IDE). Well, we have a really kind of dumb program that just displays a time in 12 hour format. In its current state, this program just displays it in in hour:minute format but our professor wants us to display it hour:minute:second format. This code is made in two different classes, one class creating objects out of the second class. I will post both classes and tell you what the issue is.
public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; // simulates the actual display /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at 00:00. */ public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); updateDisplay(); } /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at the time specified by the * parameters. */ public ClockDisplay(int hour, int minute) { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); setTime(hour, minute); } /** * This method should get called once every minute - it makes * the clock display go one minute forward. */ public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); } /** * Set the time of the display to the specified hour and * minute. */ public void setTime(int hour, int minute) { hours.setValue(hour); minutes.setValue(minute); updateDisplay(); } /** * Return the current time of this display in the format HH:MM. */ public String getTime() { return displayString; } /** * Update the internal string that represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } }
public class NumberDisplay { private int limit; private int value; /** * Constructor for objects of class NumberDisplay. * Set the limit at which the display rolls over. */ public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } /** * Return the current value. */ public int getValue() { return value; } /** * Return the display value (that is, the current value as a two-digit * String. If the value is less than ten, it will be padded with a leading * zero). */ public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; } } /** * Set the value of the display to the new specified value. If the new * value is less than zero or over the limit, do nothing. */ public void setValue(int replacementValue) { if((replacementValue >= 0) && (replacementValue < limit)) { value = replacementValue; } } /** * Increment the display value by one, rolling over to zero if the * limit is reached. */ public void increment() { value = (value + 1) % limit; } }
Well, as you see the program does what I described (if using BlueJ, I have no idea what this will do on other IDEs). Now this is the original program. I think I got it to work earlier using seconds and having it increment properly and what not, BUT the tricky part is our professor wants us to get rid of the whole displayString field. I have yet to figure out a way to properly display the time without that field. That field is involved in a few methods for formatting display and what not and I can't figure out a work around here. Now I'm not asking you to do my homework, I would be upset if you did actually. I love learning and I love figuring out problems, but I'm stumped here. So, if you guys/gals could at least give me some pointers and get me moving in the right direction that would be great! And thanks ahead of time for the help, and sorry for the long post, but I like to be descriptive!
Looking forward to being part of this community