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... -
-
/**
-
* @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 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
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: -
-
/**
-
* @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:-)
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
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:-)
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
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: -
-
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?
JosAH 11,448
Recognized Expert MVP
Erm, indexes in arrays and all sorts of Lists start at 0, not 1.
kind regards,
Jos
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!
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 calculates and displays
elapsed wall clock seconds since initialization.
class ExampleTimer:
def __init__(self):
self.start = time.clock()
|
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,...
|
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
|
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...
|
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)
| |
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?
|
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: 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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |