| | |
Help with homework #2
![]() |
Here is the problem:There are (at least) two ways in which you can make a 12 hour clock. One possibility is to just store hour values from 1 to 12. On the other hand, you can just leave the clock to work internally as a 24 hour clock, but change the display string of the clock display to show 4:23 or 4.23pm when the internal value is 16:23. Implement both versions.
The way i went to try to solve this problem was to modify the updateDisplay(). I came up with an error, which it has to do with the fields that i'm trying to use are unknown within that function. I tried to put some formal parameters in the function signature to solve this problem, but came up with another error. So, i took the formal parameters out. I'm not sure what I'm doing wrong or how to approach this.
Thanks!
The way i went to try to solve this problem was to modify the updateDisplay(). I came up with an error, which it has to do with the fields that i'm trying to use are unknown within that function. I tried to put some formal parameters in the function signature to solve this problem, but came up with another error. So, i took the formal parameters out. I'm not sure what I'm doing wrong or how to approach this.
Thanks!
Java Syntax (Toggle Plain Text)
public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; // simulates the actual display //clockdisplay constuctor public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); updateDisplay(); } //clockdisplay constuctor 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); if (hour == 12) { hours.setValue(00); minutes.setValue(30); hours.increment(); minutes.increment(); updateDisplay(); } //ex 3.31 i have to do an if statement //i have to modify it this method. 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. * * accommodate the updateDisplay to print 4:23 when the internal * value is 16:23 date 10-21-09 */ private void updateDisplay() //int hour, int minute { //hours.setValue(hour); //minutes.setValue(minute); //computer made displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); //programmer made //error:Incomparable types:NumberDisplay and int if (hours == 16 && minutes == 23) { hour.setValue(4); mintues.setValue(23); hours.increment(); minutes.increment(); } } }
0
#2 Oct 22nd, 2009
Your error message is telling you what has gone wrong with this line:
Here you are trying to compare your variable hours which is a NumberDisplay object with the number 16, which is an int. That's like saying "If this apple equals this orange" - how do you compare an apple to an orange? What you need to do is compare the hours to the NumberDisplay object with the value 16 (if that's what you really want to do?) Same goes for minutes variable...
java Syntax (Toggle Plain Text)
if (hours == 16 && minutes == 23)
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. ![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- Need help with Computer Science homework (Computer Science)
- I am having homework isues. Please Help (Java)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the Java Forum
- Previous Thread: Help me
- Next Thread: first ever program using more than one method
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





