Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Design a class named Timer that contains the following three private instance data fields: hours: an integer that holds the number of hours. minutes: an

Design a class named Timer that contains the following three private instance data fields:
hours: an integer that holds the number of hours.
minutes: an integer that holds the number of minutes.
seconds: an integer that holds the number of seconds.
In addition, the class should have the following constructors and methods:
1. A no-arg constructor that creates a default Timer object (The data fields hours, minutes, seconds are defaulted to 0).
2. A constructor that constructs a Timer object with three specified values for hours, minutes, and seconds.
3. Three public setter methods for the data fields hours, minutes and seconds.
4. Three public getter methods for the data fields hours, minutes and seconds.
In the second constructor and three setter methods above, make sure that the three data fields are greater than or equal to 0. e.g., if the value used to set the data field hours is negative, you should then set hours to 0 instead.
Write a test program to do the following tasks:
1. Prompt the user to enter three groups of values of hours, minutes and seconds and use these values to create an array of three Timer objects.
2. Calculate and print the total number of hours, minutes, and seconds in these three objects.
A sample dialog is:
Enter the first Timer object: 123
Enter the second Timer object: 456
Enter the third Timer object: 78-1[The object created should have 7,8, and 0 for the data fields hours, minutes, and seconds since -1 is invalid]
The total number of hours is 12(sum of 1,4 and 7)
The total number of minutes is 15(sum of 2,5, and 8)
The total number of seconds is 9(sum of 3,6 and 0)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions