Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Fig. 8.5: Time2.java // Time2 class declaration with overloaded constructors. public class Time2 { private int hour; // 0 - 23 private int minute;

image text in transcribed

// Fig. 8.5: Time2.java // Time2 class declaration with overloaded constructors.

public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59

// Time2 no-argument constructor: // initializes each instance variable to zero public Time2() { this(0, 0, 0); // invoke constructor with three arguments }

// Time2 constructor: hour supplied, minute and second defaulted to 0 public Time2(int hour) { this(hour, 0, 0); // invoke constructor with three arguments }

// Time2 constructor: hour and minute supplied, second defaulted to 0 public Time2(int hour, int minute) { this(hour, minute, 0); // invoke constructor with three arguments }

// Time2 constructor: hour, minute and second supplied public Time2(int hour, int minute, int second) { if (hour = 24) { throw new IllegalArgumentException("hour must be 0-23"); }

if (minute = 60) { throw new IllegalArgumentException("minute must be 0-59"); }

if (second = 60) { throw new IllegalArgumentException("second must be 0-59"); }

this.hour = hour; this.minute = minute; this.second = second; }

// Time2 constructor: another Time2 object supplied public Time2(Time2 time) { // invoke constructor with three arguments this(time.hour, time.minute, time.second); }

// Set Methods // set a new time value using universal time; // validate the data public void setTime(int hour, int minute, int second) { if (hour = 24) { throw new IllegalArgumentException("hour must be 0-23"); }

if (minute = 60) { throw new IllegalArgumentException("minute must be 0-59"); }

if (second = 60) { throw new IllegalArgumentException("second must be 0-59"); }

this.hour = hour; this.minute = minute; this.second = second; }

// validate and set hour public void setHour(int hour) { if (hour = 24) { throw new IllegalArgumentException("hour must be 0-23"); }

this.hour = hour; }

// validate and set minute public void setMinute(int minute) { if (minute = 60) { throw new IllegalArgumentException("minute must be 0-59"); }

this.minute = minute; }

// validate and set second public void setSecond(int second) { if (second = 60) { throw new IllegalArgumentException("second must be 0-59"); }

this.second = second; }

// Get Methods // get hour value public int getHour() {return hour;}

// get minute value public int getMinute() {return minute;}

// get second value public int getSecond() {return second;}

// convert to String in universal-time format (HH:MM:SS) public String toUniversalString() { return String.format( "%02d:%02d:%02d", getHour(), getMinute(), getSecond()); }

// convert to String in standard-time format (H:MM:SS AM or PM) public String toString() { return String.format("%d:%02d:%02d %s", ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour()

_____________________________________________________________________________________________________________________

// Fig. 8.6: Time2Test.java // Overloaded constructors used to initialize Time2 objects.

public class Time2Test { public static void main(String[] args) { Time2 t1 = new Time2(); // 00:00:00 Time2 t2 = new Time2(2); // 02:00:00 Time2 t3 = new Time2(21, 34); // 21:34:00 Time2 t4 = new Time2(12, 25, 42); // 12:25:42 Time2 t5 = new Time2(t4); // 12:25:42

System.out.println("Constructed with:"); displayTime("t1: all default arguments", t1); displayTime("t2: hour specified; default minute and second", t2); displayTime("t3: hour and minute specified; default second", t3); displayTime("t4: hour, minute and second specified", t4); displayTime("t5: Time2 object t4 specified", t5);

// attempt to initialize t6 with invalid values try { Time2 t6 = new Time2(27, 74, 99); // invalid values } catch (IllegalArgumentException e) { System.out.printf("%nException while initializing t6: %s%n", e.getMessage()); } }

// displays a Time2 object in 24-hour and 12-hour formats private static void displayTime(String header, Time2 t) { System.out.printf("%s%n %s%n %s%n", header, t.toUniversalString(), t.toString()); } }

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions