Help with homework #2

Reply

Join Date: Jul 2009
Posts: 14
Reputation: GooeyG is an unknown quantity at this point 
Solved Threads: 0
GooeyG's Avatar
GooeyG GooeyG is offline Offline
Newbie Poster

Help with homework #2

 
0
  #1
34 Days Ago
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!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
0
  #2
34 Days Ago
Your error message is telling you what has gone wrong with this line:
  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...
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC