473,748 Members | 3,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Dököll
2,364 Recognized Expert Top Contributor
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 2965
JosAH
11,448 Recognized Expert MVP
Why not make the hours a NumberDisplay(2 4)? 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 Recognized Expert Top Contributor
Why not make the hours a NumberDisplay(2 4)? 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 Recognized Expert MVP
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 Recognized Expert Top Contributor
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 Recognized Expert MVP
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 Recognized Expert Top Contributor
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 Recognized Expert MVP
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 Recognized Expert Top Contributor
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
2912
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 calculates and displays elapsed wall clock seconds since initialization. class ExampleTimer: def __init__(self): self.start = time.clock()
13
3288
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 time.time() doesn't appear to be suitable, since time might jump forwards or backwards at the user's whim, if the system clock is reset, or when a daylight savings time change occurs. Using time.clock() doesn't appear to be suitable on Linux,...
34
4174
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 "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
8
3407
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 benchmark some function calls for Zope project and when I use t0=time.clock(); foo(); print time.clock()-t0 I get much smaller values than when I use time.clock() (most of them 0.0 but some 0.01) When I use time.time() I get values like...
18
3370
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 boils down to the clock() function. Here is the code: #include <iostream> #include <cstdlib> int main(void)
30
17314
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() well-implemented for the major compilers g++, VC++, and Macintosh compilers and other C++ compilers on Linux/Unix, Windows, and MacOS?
8
35982
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
6936
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
4305
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
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.