Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The starting point for this assignment is the clock-display project, which is presented in Sections 3.1-3.11 of Objects First with Java. Part 1 - A

The starting point for this assignment is the clock-display project, which is presented in Sections 3.1-3.11 of Objects First with Java.

Part 1 - A 12-hour Clock (Loosely based on Exercises 3.31 and 3.32)

Note that you may not change class NumberDisplay in the assignment.

Rename ClockDisplay to ClockDisplay12: this will be a 12 hour clock.

Except for the required constructor name changes, the methods provided by ClockDisplay12 will be the same ones provided by ClockDisplay.

There are now only 12 possibilities for the hour (not 24), so make the appropriate changes.

o As NumberDisplay only works with ranges starting from 0, and we want 1 to 12, we must use a range of 0 to 11 in NumberDisplay and then change the number 0 to the number 12 in ClockDisplay12, when we display the time (i.e. in UpdateDisplay).

o Note also that the user will enter 12 (not 0), if he/she wants 12 o'clock.

o As before the default constructor should set the time to midnight (i.e. 12:00a.m.).

We need an extra field representing "a.m." or "p.m.". Make this a String.

o To make life easier, create two constants called AM and PM, to avoid typing "a.m." and "p.m." a lot.

o A String must be added as a parameter to the constructor with parameters, and to setTime, as the user must specify whether the time he/she wants is a.m. or p.m.:

We must do some error checking when the user enters this String to ensure that it is valid.

If you tested the original clockDisplay class, you will see that invalid inputs default to 0 for both the hour and the minute.

Let's have an invalid input default to "a.m.".

Note that you cannot use "==" to see if two Strings are the same. You must use the equals method, e.g. if (amPm.equals(AM)) ... // String amPm equals String AM.

Method timeTick needs to be updated to change a.m. to p.m. and p.m. to a.m. when appropriate.

Method updateDisplay needs to be updated to convert an hour of 0 to an hour of 12, as mentioned above, and to include a.m. or p.m. at the end of the displayString.

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 12:00. */ public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); 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(24); minutes = new NumberDisplay(60); 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 of this display in the format HH:MM. */ public String getTime() { return displayString; } /** * Update the internal string that represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } }

Please dont edit the NumberDisplay Class. Thanks.

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; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions