IN NEED OF JAVASCRIPT HOMEWORK

profilemaigc
Lab9_2019S.pdf

ISM3230 In-class lab Module 9 – Classes Spring 2019

 Constructors

 Getter/ Setter methods

TASK

Your task is to implement a simple Clock class to keep track of the current time using 24-hour time

representation. Your clock should allow the user of the Clock class to do the following:

 to create a clock with a given time. If no time is specified at the clock instantiation, the clock

should set itself to midnight and print a warning message to the screen.

 to set an existing clock to a specific time with setter methods

 to use a getter method to obtain an integer that represents the current number of hours on the

clock

 to use a getter method to obtain an integer that represents the current number of minutes on

the clock

 to receive a string that is formatted for the screen output and represents the current time on

the clock. The required format is the time in a four-digit format, with leading zeros where

needed for hours and minutes, and the ':' character separating the hours and minutes (for

example: "05:30", "15:04", "23:59").

 to advance an existing clock by an integer number of minutes forward

To test the implementation of the Clock class, you should create a driver program that creates a clock

object and uses its methods to perform some tests. The driver should:

 prompt the user for initial hours and minutes for the clock and instantiate a clock object

 print the "Initial time" as a formatted output and the hours and minutes separately as

integers

 in a user-driven loop, ask the user to enter the number of minutes to advance the clock forward

and print the formatted current time. Depending on the number of minutes entered, the code

should:

o ensure that the number of minutes to go forward does not exceed 720 (12 hours). Print

an error message if it does.

o terminate the loop whenever the user enters a negative number, printing

"Exiting..."

o set the current time to 12:00 with Clock's setter methods and print a message

"Resetting clock to noon!" whenever the user enters 0

INSTRUCTIONS

There are many ways to approach the architecture of the code. For this lab, use the following class

design. Use this logic only, do not deviate from these instructions:

1) A Clock class should include:

a) Private integer data members for current hours and minutes.

b) Constructors:

i) a constructor that takes 2 parameters, hours_in and minutes_in, and sets the clock's data

members to the passed hours and minutes, respectively

ii) a default constructor that uses the above constructor to set the time to midnight (with the

keyword this) and prints a message "Warning: No parameters; clock set to midnight!" to

the screen

c) Public methods:

i) setter that accepts an integer and sets the clock's hours to the parameter

ii) setter that accepts an integer and sets the clock's minutes to the parameter

iii) getter that returns an integer with the clock's current hours

iv) getter that returns an integer with the clock's current minutes

v) String getCurrentTime() method that has no input parameters, and returns the

current time as a 5 character String that includes leading zeros for hours and minutes when

needed, and separating the values with a ':'.

Hint: if your hour and/or minute variable is less than 10, you need to insert a "0" (zero)

character before the hour and/or minute to produce an output with leading zeros

Hint: you are working with a String, so you can let Java automatically convert the integers to

Strings by concatenation to an existing String, or you convert them explicitly with

Integer.toString(int i)

Hint: you should start with an empty String "" and build it up by adding the parts: hours, ':'

and minutes

vi) a void advanceTime(int advanceMinutes) method with a single input

parameter for the number of minutes to advance the clock time, and no return value

Hint: if your new minute count exceeds 59, you need to calculate the correct minute value

for the new time.

Hint: if your new hour count exceeds 23, you need to calculate the correct hour value for

the next day.

2) A driver class (with main), called Lab9, should provide the following logic:

a) Prompt user for input for the current time hours and minutes. You may assume that these

values are valid (i.e., you do not need to do any input validation for these values).

b) Call the Clock constructor with user input hours and minutes to create a new clock object.

c) Print the initial user input time; use the clock's method getCurrentTime() to format the

output before printing. Use the label "Initial time: "

d) Print the value of hours and the value of minutes by calling the clock's getter methods. Use the

labels "Hours getter:" and "Minutes getter: ", respectively.

e) In a user-input driven loop:

i) Prompt the user for a number of minutes to advance the clock with "Enter number of

minutes to advance the clock (up to 720 minutes) or -1 to

exit:"

ii) Read the number of minutes to advance into a variable

(1) If the user entered a positive number greater than 720, print an error message: "Too

many minutes: can't advance by more than 12 hours!".

(2) If the user entered 0, print the message "Resetting clock to noon!" , use the

setter methods to set the clock to 12 hours and 0 minutes and print the new time.

(3) If the user entered an appropriate value for minutes, ask the Clock to advance the

clock time, and then print the new time.

iii) If the user entered any negative number, exit the loop and print "Exiting..." when

terminating the program

SUBMITTING: You will need to submit multiple files for this Lab.

 Either submit each file separately, using the normal lab submission method, submitting the

driver and Clock files

 Or, Use File>Export Project>To Zip... from the main menu bar to submit the whole project zip

file.

SAMPLE OUTPUT