944,052 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 368
  • Java RSS
Oct 22nd, 2009
0

Help with homework #2

Expand Post »
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!

Java Syntax (Toggle Plain Text)
  1. public class ClockDisplay
  2. {
  3. private NumberDisplay hours;
  4. private NumberDisplay minutes;
  5. private String displayString; // simulates the actual display
  6.  
  7. //clockdisplay constuctor
  8. public ClockDisplay()
  9. {
  10. hours = new NumberDisplay(24);
  11. minutes = new NumberDisplay(60);
  12. updateDisplay();
  13. }
  14. //clockdisplay constuctor
  15. public ClockDisplay(int hour, int minute)
  16. {
  17. hours = new NumberDisplay(24);
  18. minutes = new NumberDisplay(60);
  19. setTime(hour, minute);
  20. }
  21.  
  22. /**
  23.   * This method should get called once every minute - it makes
  24.   * the clock display go one minute forward.
  25.   */
  26. public void timeTick()
  27. {
  28. minutes.increment();
  29. if(minutes.getValue() == 0) { // it just rolled over!
  30. hours.increment();
  31. }
  32. updateDisplay();
  33. }
  34.  
  35. /**
  36.   * Set the time of the display to the specified hour and
  37.   * minute.
  38.   */
  39. public void setTime(int hour, int minute)
  40. {
  41. hours.setValue(hour);
  42. minutes.setValue(minute);
  43.  
  44. if (hour == 12)
  45. {
  46. hours.setValue(00);
  47. minutes.setValue(30);
  48. hours.increment();
  49. minutes.increment();
  50. updateDisplay();
  51. }
  52.  
  53. //ex 3.31 i have to do an if statement
  54. //i have to modify it this method.
  55. updateDisplay();
  56. }
  57.  
  58. /**
  59.   * Return the current time of this display in the format HH:MM.
  60.   */
  61. public String getTime()
  62. {
  63. return displayString;
  64. }
  65.  
  66. /**
  67.   * Update the internal string that represents the display.
  68.   *
  69.   * accommodate the updateDisplay to print 4:23 when the internal
  70.   * value is 16:23 date 10-21-09
  71.   */
  72. private void updateDisplay() //int hour, int minute
  73. {
  74. //hours.setValue(hour);
  75. //minutes.setValue(minute);
  76.  
  77. //computer made
  78. displayString = hours.getDisplayValue() + ":" +
  79. minutes.getDisplayValue();
  80.  
  81. //programmer made
  82. //error:Incomparable types:NumberDisplay and int
  83. if (hours == 16 && minutes == 23)
  84. {
  85. hour.setValue(4);
  86. mintues.setValue(23);
  87. hours.increment();
  88. minutes.increment();
  89. }
  90. }
  91. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
GooeyG is offline Offline
30 posts
since Jul 2009
Oct 22nd, 2009
0
Re: Help with homework #2
Your error message is telling you what has gone wrong with this line:
java Syntax (Toggle Plain Text)
  1. if (hours == 16 && minutes == 23)
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...
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help me
Next Thread in Java Forum Timeline: first ever program using more than one method





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC