473,414 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

This one I am Proud: But adding AM PM to clock!

Dököll
2,364 Expert 2GB
All it is, is a simple program that asks to enter hour and minute and makes the clock tick, after ticking, when clock is viewed, it should read a minute ahead. Very proud of the results. The only issue is getting PM to fire when time ticks beyond 24 hours. Just a matter of time, but do tell me what you see happening, I am sure it is pretty simple...

Expand|Select|Wrap|Line Numbers
  1.  
  2. /** 
  3.  * @author Michael Kölling and David J. Barnes
  4.  * @version 2007.10.03
  5.  * @professor The Nice One
  6.  * @programmer Just Me
  7.  * @Lab 5
  8.  */
  9.  
  10.  
  11. public class ClockDisplay
  12. {
  13.     private NumberDisplay hours;
  14.     private NumberDisplay minutes;
  15.     private String displayString;    // simulates the actual display
  16.  
  17.     /**
  18.      * Constructor for ClockDisplay objects. This constructor 
  19.      * creates a new clock set at 00:00.
  20.      */
  21.     public ClockDisplay()
  22.     {
  23.         hours = new NumberDisplay(12);
  24.         minutes = new NumberDisplay(59);
  25.         updateDisplay();
  26.     }
  27.  
  28.     /**
  29.      * Constructor for ClockDisplay objects. This constructor
  30.      * creates a new clock set at the time specified by the 
  31.      * parameters.
  32.      */
  33.     public ClockDisplay(int hour, int minute)
  34.     {
  35.         hours = new NumberDisplay(12);
  36.         minutes = new NumberDisplay(59);
  37.         setTime(hour, minute);
  38.     }
  39.  
  40.     /**
  41.      * This method should get called once every minute - it makes
  42.      * the clock display go one minute forward.
  43.      */
  44.     public void timeTick()
  45.     {
  46.         minutes.increment();
  47.         if(minutes.getValue() == 0) {  // it just rolled over!
  48.             hours.increment();
  49.         }
  50.         updateDisplay();
  51.     }
  52.  
  53.     /**
  54.      * Set the time of the display to the specified hour and
  55.      * minute.
  56.      */
  57.     public void setTime(int hour, int minute)
  58.     {
  59.         hours.setValue(hour);
  60.         minutes.setValue(minute);
  61.         updateDisplay();
  62.     }
  63.  
  64.     /**
  65.      * Return the current time...
  66.      */
  67.     public String getTime()
  68.     {
  69.         return displayString;
  70.     }
  71.  
  72.     /**
  73.      * Update the internal string that represents the display.
  74.      */
  75.     private String updateDisplay()
  76.     {
  77.         if(hours.getValue() < 12){
  78.  
  79.        return displayString = hours.getValue() + ": " + 
  80.          minutes.getValue() + " AM"; 
  81.  
  82.       }
  83.  
  84.         else { 
  85.  
  86.          return displayString = hours.getValue() + ": " + 
  87.          minutes.getValue() + " PM";
  88.      }
  89. }
  90.  
  91. }
  92.  
  93.  
Thanks for reading!
Oct 13 '07 #1
8 2949
JosAH
11,448 Expert 8TB
Why not make the hours a NumberDisplay(24)? A value < 12 indicates AM,
the other values indicate PM. When the value of the display > 12 subtract 12
from the value before displaying it.

kind regards,

Jos
Oct 13 '07 #2
Dököll
2,364 Expert 2GB
Why not make the hours a NumberDisplay(24)? A value < 12 indicates AM,
the other values indicate PM. When the value of the display > 12 subtract 12
from the value before displaying it.

kind regards,

Jos
I'll try it JosAH, this might work. I was also coming over because I forgot to add additional code, specific to the NumberDisplay class, sorry about that:

Expand|Select|Wrap|Line Numbers
  1.  
  2. /**
  3.  * @author Michael Kölling and David J. Barnes
  4.  * @version 2007.10.03
  5.  * @professor Still Nice One
  6.  * @programmer Only Me
  7.  * Lab 5
  8.  */
  9. public class NumberDisplay
  10. {
  11.     private int limit;
  12.     private int value;
  13.  
  14.     /**
  15.      * Constructor for objects of class NumberDisplay.
  16.      * Set the limit at which the display rolls over.
  17.      */
  18.     public NumberDisplay(int rollOverLimit)
  19.     {
  20.         limit = rollOverLimit;
  21.         value = 0;
  22.     }
  23.  
  24.     /**
  25.      * Return the current value.
  26.      */
  27.     public int getValue()
  28.     {
  29.         return value;
  30.     }
  31.  
  32.     /**
  33.      * Return the display value (that is, the current value as a two-digit
  34.      * String. If the value is less than ten, it will be padded with a leading
  35.      * zero).
  36.      */
  37.     public String getDisplayValue()
  38.     {
  39.         if(value < 10) {
  40.             return "0" + value;
  41.         }
  42.         else {
  43.             return "" + value;
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * Set the value of the display to the new specified value. If the new
  49.      * value is less than zero or over the limit, do nothing.
  50.      */
  51.     public void setValue(int replacementValue)
  52.     {
  53.         if((replacementValue >= 0) && (replacementValue < limit)) {
  54.             value = replacementValue;
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * Increment the display value by one, rolling over to zero if the
  60.      * limit is reached.
  61.      */
  62.     public void increment()
  63.     {
  64.         value = (value + 1) % limit;
  65.     }
  66. }
  67.  
Will give it another whirl like I said. Wish me luck for Lab 6, looks easier, will post after submitting, only if I need to tweak it a bit, I may not like what it is doing:-)
Oct 14 '07 #3
JosAH
11,448 Expert 8TB
I'll try it JosAH, this might work. I was also coming over because I forgot to add additional code, specific to the NumberDisplay class, sorry about that
No need to apologize; I'd put the AM/PM logic in a class derived from your
NumberDisplay class; that'd keep your main/driver logic clean.

kind regards,

Jos
Oct 14 '07 #4
Dököll
2,364 Expert 2GB
No need to apologize; I'd put the AM/PM logic in a class derived from your
NumberDisplay class; that'd keep your main/driver logic clean.

kind regards,

Jos
Thanks!

funny thing, I actually did start off by copying a chunk of code from the NumberDisplay class and made up Strings using the ClockDisplay class objects to attempt a completed task. I guess when AM popped up I was overjoyed and ignored minor details...

I think I see what you're leading at:-)
Oct 15 '07 #5
JosAH
11,448 Expert 8TB
Thanks!

funny thing, I actually did start off by copying a chunk of code from the NumberDisplay class and made up Strings using the ClockDisplay class objects to attempt a completed task. I guess when AM popped up I was overjoyed and ignored minor details...

I think I see what you're leading at:-)
What you wrote above indicates an enthousiasm for actual coding instead of
designing the darn thing first. Never, ever touch that keyboard before you've
finished your design. A design doesn't imply all sorts of fancy UML pictures,
i.e. a few pieces of scribbling paper do fine too as long as you have thought
over all corners of your program to be so that you won't face surprises when
you finally do have to type in all that silly source code.

kind regards,

Jos
Oct 15 '07 #6
Dököll
2,364 Expert 2GB
What you wrote above indicates an enthousiasm for actual coding instead of
designing the darn thing first. Never, ever touch that keyboard before you've
finished your design. A design doesn't imply all sorts of fancy UML pictures,
i.e. a few pieces of scribbling paper do fine too as long as you have thought
over all corners of your program to be so that you won't face surprises when
you finally do have to type in all that silly source code.

kind regards,

Jos
The very idea, JosAH...Design first is key...I am going with my guts thus far, and it looks like I am winning. You're very kind to amicably add your two cents in, I appreciate it...keep'em comin';-)

And about silly coding! Lab6 is in the bag, the first time I ever studied each piece before looking at what to modify...just submitted it. The only one thus far with no issues. Perhaps somehow you were talking "sense" into me before I logged on to report job, seemingly, well-done (Prof will be the Judge but...)...

Have a look:

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.ArrayList;
  3.  
  4. /**
  5.  * A class to maintain an arbitrarily long list of notes.
  6.  * Notes are numbered for external reference by user.
  7.  * In this version, note numbers start at 1.
  8.  * 
  9.  * @author David J. Barnes and Michael Kölling.
  10.  * @version 2007.10.15
  11.  * @programmer Me 
  12.  * @professor Nice
  13.  * @Lab 6
  14.  */
  15. public class Notebook
  16. {
  17.     // Storage for an arbitrary number of notes.
  18.     private ArrayList<String> notes;
  19.  
  20.     /**
  21.      * Perform any initialization that is required for the
  22.      * notebook.
  23.      */
  24.     public Notebook()
  25.     {
  26.         notes = new ArrayList<String>();
  27.     }
  28.  
  29.     /**
  30.      * Store a new note into the notebook.
  31.      * @param note The note to be stored.
  32.      */
  33.     public void storeNote(String note)
  34.     {
  35.         notes.add(note);
  36.     }
  37.  
  38.     /**
  39.      * @return The number of notes currently in the notebook.
  40.      */
  41.     public int numberOfNotes()
  42.     {
  43.         return notes.size();
  44.     }   
  45.  
  46.  
  47.     /**
  48.      * Decided to use the search mechanism here to do the searching and printing for us:-)
  49.      * Judging by the index information I thought it fiting to use the index number to figure out
  50.      * how to make a comparison.  Hence, comparing the index number against whatever is found=true
  51.      */
  52.  
  53.  
  54.  
  55.     /**
  56.      * Modified the index portion to reflect 1 as starting point.
  57.      * Modified removeNote, showNote as well to reflect 1 as starting point...
  58.      */
  59.  
  60.  
  61.     public void listNotes(String searchString)
  62.     {
  63.         int index = 1;
  64.         boolean found = false;
  65.         while(index < notes.size() && !found)
  66.         {
  67.             String note = notes.get(index);
  68.             if (note.contains(searchString))
  69.             {
  70.                 found=true;
  71.             }
  72.             else
  73.             {
  74.                 index++;
  75.             }
  76.         }
  77.  
  78.         /**
  79.      * Modified to reflect a 1, 2, 3 for 1st, 2nd, 3rd note.
  80.      */
  81.  
  82.  
  83.         if ((found) && index ==1)
  84.         {
  85.              System.out.println("1" + ":" + notes.get(index));
  86.         }
  87.  
  88.  
  89.         else if ((found) && index ==2)
  90.         {
  91.              System.out.println("2" + ":" + notes.get(index));
  92.         }
  93.  
  94.         else if ((found) && index ==3)
  95.         {
  96.              System.out.println("3" + ":" + notes.get(index));
  97.         }
  98.  
  99.  
  100.         else
  101.         {
  102.             System.out.println("Search term not Found, please try again!");
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Modified the index portion to reflect 1 as starting point.
  108.      */
  109.  
  110.  
  111.     public void removeNote(int noteNumber)
  112.     {
  113.         if(noteNumber < 1) {
  114.             // This is not a valid note number, so do nothing.
  115.         }
  116.         else if(noteNumber < numberOfNotes()) {
  117.             // This is a valid note number.
  118.             notes.remove(noteNumber);
  119.         }
  120.         else {
  121.             System.out.println("Note number was not Found, please try again");
  122.         }
  123.     }
  124.  
  125.  
  126.  
  127.  
  128.     /**
  129.      * Show a note.
  130.      * @param noteNumber The number of the note to be shown.
  131.      * Modified the index portion to reflect 1 as starting point.
  132.      */
  133.  
  134.     public void showNote(int noteNumber)
  135.     {
  136.         if(noteNumber < 1) {
  137.             // This is not a valid note number, so do nothing.
  138.         }
  139.         else if(noteNumber < numberOfNotes()) {
  140.             // This is a valid note number, so we can print it.
  141.             System.out.println(notes.get(noteNumber));
  142.         }
  143.         else {
  144.              System.out.println("Note number was not Found");
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150.  
No problems it does what I want, no gimmicks. I will revisit this one to see what else it can do, perhpas I can make it work for other previous labs. There's an idea, yeah, That's it!

Do indexes work with time? Or a better way to ask! Does each time slot have an index number, like 12:00 index 0, does this even make sense?
Oct 16 '07 #7
JosAH
11,448 Expert 8TB
Erm, indexes in arrays and all sorts of Lists start at 0, not 1.

kind regards,

Jos
Oct 16 '07 #8
Dököll
2,364 Expert 2GB
Erm, indexes in arrays and all sorts of Lists start at 0, not 1.

kind regards,

Jos
Now this makes sense, thanks Jos!
Jan 10 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jacob H | last post by:
Hello all, I'm close to being a novice programmer and I never was good at math. So I'm curious to know ways in which this following code can be made more efficient, more elegant. The code just...
13
by: Peter Hansen | last post by:
I would like to determine the "actual" elapsed time of an operation which could take place during a time change, in a platform-independent manner (at least across Linux/Windows machines). Using...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
8
by: peterbe | last post by:
What's the difference between time.clock() and time.time() (and please don't say clock() is the CPU clock and time() is the actual time because that doesn't help me at all :) I'm trying to...
18
by: roberts.noah | last post by:
Ok, so I am trying to establish how boost.timer works and I try a few things and afaict it doesn't. So I open the header to see why and run some tests to establish what is going on and it all...
30
by: Matt | last post by:
Does clock_t clock() measure real time or process time? Unless I have missed something, Stroustrup's treatment in TC++PL (section D.4.4.1) is none too clear on that question. Is clock()...
8
by: David | last post by:
I'm trying to add a date and time to an html form, but I'm having a bit of trouble getting it working. Any suggestions? Thanks! ~ David (merlin001_at_gmail_dot_com)
5
by: none | last post by:
Hello, can you help? if I include: #include <time.h> and run the code: clock_t t = clock(); cout << "t is " << t << endl; I get (seemingly randomly) "t is 0" or "t is 10000"
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.