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... -
-
/**
-
* @author Michael Kölling and David J. Barnes
-
* @version 2007.10.03
-
* @professor The Nice One
-
* @programmer Just Me
-
* @Lab 5
-
*/
-
-
-
public class ClockDisplay
-
{
-
private NumberDisplay hours;
-
private NumberDisplay minutes;
-
private String displayString; // simulates the actual display
-
-
/**
-
* Constructor for ClockDisplay objects. This constructor
-
* creates a new clock set at 00:00.
-
*/
-
public ClockDisplay()
-
{
-
hours = new NumberDisplay(12);
-
minutes = new NumberDisplay(59);
-
updateDisplay();
-
}
-
-
/**
-
* Constructor for ClockDisplay objects. This constructor
-
* creates a new clock set at the time specified by the
-
* parameters.
-
*/
-
public ClockDisplay(int hour, int minute)
-
{
-
hours = new NumberDisplay(12);
-
minutes = new NumberDisplay(59);
-
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);
-
updateDisplay();
-
}
-
-
/**
-
* Return the current time...
-
*/
-
public String getTime()
-
{
-
return displayString;
-
}
-
-
/**
-
* Update the internal string that represents the display.
-
*/
-
private String updateDisplay()
-
{
-
if(hours.getValue() < 12){
-
-
return displayString = hours.getValue() + ": " +
-
minutes.getValue() + " AM";
-
-
}
-
-
else {
-
-
return displayString = hours.getValue() + ": " +
-
minutes.getValue() + " PM";
-
}
-
}
-
-
}
-
-
Thanks for reading!
8 2949
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
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: -
-
/**
-
* @author Michael Kölling and David J. Barnes
-
* @version 2007.10.03
-
* @professor Still Nice One
-
* @programmer Only Me
-
* Lab 5
-
*/
-
public class NumberDisplay
-
{
-
private int limit;
-
private int value;
-
-
/**
-
* Constructor for objects of class NumberDisplay.
-
* Set the limit at which the display rolls over.
-
*/
-
public NumberDisplay(int rollOverLimit)
-
{
-
limit = rollOverLimit;
-
value = 0;
-
}
-
-
/**
-
* Return the current value.
-
*/
-
public int getValue()
-
{
-
return value;
-
}
-
-
/**
-
* Return the display value (that is, the current value as a two-digit
-
* String. If the value is less than ten, it will be padded with a leading
-
* zero).
-
*/
-
public String getDisplayValue()
-
{
-
if(value < 10) {
-
return "0" + value;
-
}
-
else {
-
return "" + value;
-
}
-
}
-
-
/**
-
* Set the value of the display to the new specified value. If the new
-
* value is less than zero or over the limit, do nothing.
-
*/
-
public void setValue(int replacementValue)
-
{
-
if((replacementValue >= 0) && (replacementValue < limit)) {
-
value = replacementValue;
-
}
-
}
-
-
/**
-
* Increment the display value by one, rolling over to zero if the
-
* limit is reached.
-
*/
-
public void increment()
-
{
-
value = (value + 1) % limit;
-
}
-
}
-
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:-)
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
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:-)
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
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: -
-
import java.util.ArrayList;
-
-
/**
-
* A class to maintain an arbitrarily long list of notes.
-
* Notes are numbered for external reference by user.
-
* In this version, note numbers start at 1.
-
*
-
* @author David J. Barnes and Michael Kölling.
-
* @version 2007.10.15
-
* @programmer Me
-
* @professor Nice
-
* @Lab 6
-
*/
-
public class Notebook
-
{
-
// Storage for an arbitrary number of notes.
-
private ArrayList<String> notes;
-
-
/**
-
* Perform any initialization that is required for the
-
* notebook.
-
*/
-
public Notebook()
-
{
-
notes = new ArrayList<String>();
-
}
-
-
/**
-
* Store a new note into the notebook.
-
* @param note The note to be stored.
-
*/
-
public void storeNote(String note)
-
{
-
notes.add(note);
-
}
-
-
/**
-
* @return The number of notes currently in the notebook.
-
*/
-
public int numberOfNotes()
-
{
-
return notes.size();
-
}
-
-
-
/**
-
* Decided to use the search mechanism here to do the searching and printing for us:-)
-
* Judging by the index information I thought it fiting to use the index number to figure out
-
* how to make a comparison. Hence, comparing the index number against whatever is found=true
-
*/
-
-
-
-
/**
-
* Modified the index portion to reflect 1 as starting point.
-
* Modified removeNote, showNote as well to reflect 1 as starting point...
-
*/
-
-
-
public void listNotes(String searchString)
-
{
-
int index = 1;
-
boolean found = false;
-
while(index < notes.size() && !found)
-
{
-
String note = notes.get(index);
-
if (note.contains(searchString))
-
{
-
found=true;
-
}
-
else
-
{
-
index++;
-
}
-
}
-
-
/**
-
* Modified to reflect a 1, 2, 3 for 1st, 2nd, 3rd note.
-
*/
-
-
-
if ((found) && index ==1)
-
{
-
System.out.println("1" + ":" + notes.get(index));
-
}
-
-
-
else if ((found) && index ==2)
-
{
-
System.out.println("2" + ":" + notes.get(index));
-
}
-
-
else if ((found) && index ==3)
-
{
-
System.out.println("3" + ":" + notes.get(index));
-
}
-
-
-
else
-
{
-
System.out.println("Search term not Found, please try again!");
-
}
-
}
-
-
/**
-
* Modified the index portion to reflect 1 as starting point.
-
*/
-
-
-
public void removeNote(int noteNumber)
-
{
-
if(noteNumber < 1) {
-
// This is not a valid note number, so do nothing.
-
}
-
else if(noteNumber < numberOfNotes()) {
-
// This is a valid note number.
-
notes.remove(noteNumber);
-
}
-
else {
-
System.out.println("Note number was not Found, please try again");
-
}
-
}
-
-
-
-
-
/**
-
* Show a note.
-
* @param noteNumber The number of the note to be shown.
-
* Modified the index portion to reflect 1 as starting point.
-
*/
-
-
public void showNote(int noteNumber)
-
{
-
if(noteNumber < 1) {
-
// This is not a valid note number, so do nothing.
-
}
-
else if(noteNumber < numberOfNotes()) {
-
// This is a valid note number, so we can print it.
-
System.out.println(notes.get(noteNumber));
-
}
-
else {
-
System.out.println("Note number was not Found");
-
}
-
}
-
}
-
-
-
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?
Erm, indexes in arrays and all sorts of Lists start at 0, not 1.
kind regards,
Jos
Erm, indexes in arrays and all sorts of Lists start at 0, not 1.
kind regards,
Jos
Now this makes sense, thanks Jos!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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()...
|
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)
|
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"
|
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
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |