It would be perfectly reasonable for the Time2 class of Fig. 8.5 to represent the time internally
Question:
It would be perfectly reasonable for the Time2 class of Fig. 8.5 to represent the time internally as the number of seconds since midnight rather than the three integer values hour, minute and second. Clients could use the same public methods and get the same results. Modify the Time2 class of Fig. 8.5 to implement the time as the number of seconds since midnight and show that no change is visible to the clients of the class.
Fig. 8.5
Transcribed Image Text:
I 2 3 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 // 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 < 0 11 hour >= 24) { throw new IllegalArgumentException ("hour must be 0-23"); } if (minute < 0 || minute >=60) { throw new IllegalArgumentException ("minute must be 0-59"); } if (second < 0 || 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 < 0 11 hour >= 24) { throw new Illegal ArgumentException ("hour must be 0-23"); } if (minute < 0 || minute >=60) { throw new IllegalArgumentException ("minute must be 0-59"); } if (second < 0 || 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 0 11 hour >= 24) { throw new Illegal ArgumentException ("hour must be 0-23"); } this.hour hour; } // validate and set minute public void setMinute(int minute) { if (minute <0 || minute >= 60) { throw new IllegalArgumentException ("minute must be 0-59"); } 112 113 114 115 116 117 118 119 } 120 } this.minute = minute; } // validate and set second public void set Second(int second) { if (second < 0 || 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 toUniversal String() { 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 11 getHour() == 12) ? 12: getHour () % 12), getMinute(), getSecond(), (getHour() < 12 ? "AM": "PM"));
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (2 reviews)
To modify the Time2 class to represent the time internally as the number of seconds since midnight while making no visible changes to the clients of the class you will need to perform the following st...View the full answer
Answered By
Muhammad Haroon
More than 3 years experience in teaching undergraduate and graduate level courses which includes Object Oriented Programming, Data Structures, Algorithms, Database Systems, Theory of Automata, Theory of Computation, Database Administration, Web Technologies etc.
5.00+
3+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
Managing Scope Changes Case Study Scope changes on a project can occur regardless of how well the project is planned or executed. Scope changes can be the result of something that was omitted during...
-
The Crazy Eddie fraud may appear smaller and gentler than the massive billion-dollar frauds exposed in recent times, such as Bernie Madoffs Ponzi scheme, frauds in the subprime mortgage market, the...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Ruby works in the treasury department of a large airline company. Over the coming months the airline company expects to increase flights as countries continue to ease travel restrictions. Ruby and...
-
Consider an airfoil in a Mach 0.5 free stream. At a given point on the airfoil, the local Mach number is 0.86. Using the compressible flow tables at the back of this book, calculate the pressure...
-
On 1 January, Bob Earl set up Earl's Gyms Ltd to manufacture and sell children's outdoor play gyms. He was an engineer by profession but he understood the importance of accounting information and...
-
In connection with your examination of the financial statements of Olars Manufacturing Corporation for the year ended December 31, 19X0, your post-balance-sheet-date review disclosed the following...
-
One of our best customers has had a major plant breakdown and wants us to make as many widgets for him as possible during the next few days, until he gets the necessary repairs done. With our...
-
On the following graph, use the blue points (circle symbol) to plot Simone's initial production possibilities frontier (PPF). EMBROIDERED SWEATERS 30 25 8 15 10 5 0 0 1 2 3 4 TEA TOWELS 5 00 7 8...
-
Create class SavingsAccount. Use a static variable annualInterestRateto store the annual interest rate for all account holders. Each object of the class contains a private instance variable...
-
Create a class Cylinder with attributes radius and height, each of which has a default value of 1. Provide a method that calculates the cylinders volume, which is pi multiplied by the square of the...
-
Calculate y" and y"'. y = 5t 3 + 7t 8/3
-
What is financial management? How does financial management differ in the sport industry as compared to other industries?
-
Executives found guilty in huge financial frauds do not consider their families as stakeholders in their decision to commit financial crimes. True/False
-
Allowing fraudulent financial statements to be published after corrupt audits does not decrease confidence in the capital markets. True/False
-
What legislative actions currently being considered in Congress might affect the financial management of sport?
-
If an organizations current ratio value is below 1.00, what might that suggest about the organization?
-
The part shown is formed from a 1/8 -in diameter steel wire, with R = 5 in and l = 4 in. A force is applied with P = 1 lbf. Use Castiglianos method to estimate the horizontal deflection at point A....
-
A handrail, which weighs 120 N and is 1.8 m long. was mounted to a wall adjacent to a small set of steps (Figure P4.26). The support at A has broken, and the rail has fallen about the loose bolt at 8...
-
What is a private network address? Should a data-gram with a private network address ever be present in the larger public Internet? Explain.
-
Compare and contrast the lPv4 and the 1Pv6 header fields. Do they have any fields in common?
-
It has been said that when IPv6 tunnels through IPv4 routers, lPv6 treats the IPv4 tunnels as link-layer protocols. Do you agree with this statement? Why or why not?
-
Your client wants to design to design a deodorant package that will appeal to women, yet is afraid that the wrong design will be offensive. Name and explain two research methodologies or techniques...
-
What is a "disaster check" in qualitative research? Why is it used?
-
What is (are) the marketing problem(s) You should identify and describe the key marketing problem(s) you will address. Then, identify and present examples of the problem (e.g. photographs,...
Study smarter with the SolutionInn App