Skip to content

Commit 284fb1f

Browse files
authored
Add files via upload
1 parent 6c8b4eb commit 284fb1f

File tree

4 files changed

+396
-0
lines changed

4 files changed

+396
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Fig. 8.6: Time2Test.java
2+
// Overloaded constructors used to initialize Time2 objects.
3+
4+
public class Main
5+
{
6+
public static void main(String[] args)
7+
{
8+
Time2 t1 = new Time2(); // 00:00:00
9+
Time2 t2 = new Time2(2); // 02:00:00
10+
Time2 t3 = new Time2(21, 34); // 21:34:00
11+
Time2 t4 = new Time2(12, 25, 42); // 12:25:42
12+
Time2 t5 = new Time2(t4); // 12:25:42
13+
14+
System.out.println("Constructed with:");
15+
displayTime("t1: all default arguments", t1);
16+
displayTime("t2: hour specified; default minute and second", t2);
17+
displayTime("t3: hour and minute specified; default second", t3);
18+
displayTime("t4: hour, minute and second specified", t4);
19+
displayTime("t5: Time2 object t4 specified", t5);
20+
21+
// attempt to initialize t6 with invalid values
22+
try
23+
{
24+
Time2 t6 = new Time2(27, 74, 99); // invalid values
25+
}
26+
catch (IllegalArgumentException e)
27+
{
28+
System.out.printf("%nException while initializing t6: %s%n",
29+
e.getMessage());
30+
}
31+
}
32+
33+
// displays a Time2 object in 24-hour and 12-hour formats
34+
private static void displayTime(String header, Time2 t)
35+
{
36+
System.out.printf("%s%n %s%n %s%n",
37+
header, t.toUniversalString(), t.toString());
38+
}
39+
} // end class Time2Test
40+
41+
42+
/**************************************************************************
43+
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
44+
* Pearson Education, Inc. All Rights Reserved. *
45+
* *
46+
* DISCLAIMER: The authors and publisher of this book have used their *
47+
* best efforts in preparing the book. These efforts include the *
48+
* development, research, and testing of the theories and programs *
49+
* to determine their effectiveness. The authors and publisher make *
50+
* no warranty of any kind, expressed or implied, with regard to these *
51+
* programs or to the documentation contained in these books. The authors *
52+
* and publisher shall not be liable in any event for incidental or *
53+
* consequential damages in connection with, or arising out of, the *
54+
* furnishing, performance, or use of these programs. *
55+
*************************************************************************/
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Fig. 8.5: Time2.java
2+
// Time2 class declaration with overloaded constructors.
3+
4+
public class Time2
5+
{
6+
private int secondsAfterMidnight;
7+
8+
// Time2 no-argument constructor:
9+
// initializes each instance variable to zero
10+
public Time2()
11+
{
12+
secondsAfterMidnight = 0;
13+
}
14+
15+
// Time2 constructor: hour supplied, minute and second defaulted to 0
16+
public Time2(int hour)
17+
{
18+
setTime(hour, 0, 0); // invoke constructor with three arguments
19+
}
20+
21+
// Time2 constructor: hour and minute supplied, second defaulted to 0
22+
public Time2(int hour, int minute)
23+
{
24+
setTime(hour, minute, 0); // invoke constructor with three arguments
25+
}
26+
27+
// Time2 constructor: hour, minute and second supplied
28+
public Time2(int hour, int minute, int second)
29+
{
30+
if (hour < 0 || hour >= 24)
31+
throw new IllegalArgumentException("hour must be 0-23");
32+
33+
if (minute < 0 || minute >= 60)
34+
throw new IllegalArgumentException("minute must be 0-59");
35+
36+
if (second < 0 || second >= 60)
37+
throw new IllegalArgumentException("second must be 0-59");
38+
39+
setTime(hour, minute, second);
40+
}
41+
42+
// Time2 constructor: another Time2 object supplied
43+
public Time2(Time2 time)
44+
{
45+
// invoke constructor with three arguments
46+
setTime(time.getHour(), time.getMinute(), time.getSecond());
47+
}
48+
49+
// Set Methods
50+
// set a new time value using universal time;
51+
// validate the data
52+
public void setTime(int hour, int minute, int second)
53+
{
54+
if (hour < 0 || hour >= 24)
55+
throw new IllegalArgumentException("hour must be 0-23");
56+
57+
if (minute < 0 || minute >= 60)
58+
throw new IllegalArgumentException("minute must be 0-59");
59+
60+
if (second < 0 || second >= 60)
61+
throw new IllegalArgumentException("second must be 0-59");
62+
63+
setHour(hour);
64+
setMinute(minute);
65+
setSecond(second);
66+
}
67+
68+
// validate and set hour
69+
public void setHour(int hour)
70+
{
71+
if (hour < 0 || hour >= 24)
72+
throw new IllegalArgumentException("hour must be 0-23");
73+
74+
secondsAfterMidnight = (hour * 3600) + (getMinute() * 60) + getSecond();
75+
}
76+
77+
// validate and set minute
78+
public void setMinute(int minute)
79+
{
80+
if (minute < 0 || minute >= 60)
81+
throw new IllegalArgumentException("minute must be 0-59");
82+
83+
secondsAfterMidnight = (getHour() * 3600) + (minute * 60) + getSecond();
84+
}
85+
86+
// validate and set second
87+
public void setSecond(int second)
88+
{
89+
if (second < 0 || second >= 60)
90+
throw new IllegalArgumentException("second must be 0-59");
91+
92+
secondsAfterMidnight = (getHour() * 3600) + (getMinute() * 60) + second;
93+
}
94+
95+
// Get Methods
96+
// get hour value
97+
public int getHour()
98+
{
99+
return (secondsAfterMidnight / 3600);
100+
}
101+
102+
// get minute value
103+
public int getMinute()
104+
{
105+
return ((secondsAfterMidnight % 3600) / 60);
106+
}
107+
108+
// get second value
109+
public int getSecond()
110+
{
111+
return ((secondsAfterMidnight % 3600) % 60);
112+
}
113+
114+
// convert to String in universal-time format (HH:MM:SS)
115+
public String toUniversalString()
116+
{
117+
return String.format(
118+
"%02d:%02d:%02d", getHour(), getMinute(), getSecond());
119+
}
120+
121+
// convert to String in standard-time format (H:MM:SS AM or PM)
122+
public String toString()
123+
{
124+
return String.format("%d:%02d:%02d %s",
125+
((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
126+
getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
127+
}
128+
} // end class Time2
129+
130+
/**************************************************************************
131+
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
132+
* Pearson Education, Inc. All Rights Reserved. *
133+
* *
134+
* DISCLAIMER: The authors and publisher of this book have used their *
135+
* best efforts in preparing the book. These efforts include the *
136+
* development, research, and testing of the theories and programs *
137+
* to determine their effectiveness. The authors and publisher make *
138+
* no warranty of any kind, expressed or implied, with regard to these *
139+
* programs or to the documentation contained in these books. The authors *
140+
* and publisher shall not be liable in any event for incidental or *
141+
* consequential damages in connection with, or arising out of, the *
142+
* furnishing, performance, or use of these programs. *
143+
*************************************************************************/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Main
2+
{
3+
public static void main( String args[] )
4+
{
5+
6+
Time2 time = new Time2(22, 58, 58);
7+
8+
System.out.println("Time: ");
9+
System.out.printf(" %s\n", time.toUniversalString());
10+
System.out.printf(" %s\n", time.toString());
11+
12+
time.tick();
13+
System.out.println("add 1 second");
14+
System.out.printf(" %s\n", time.toString());
15+
16+
time.incrementMinute();
17+
System.out.println("add 1 minute");
18+
System.out.printf(" %s\n", time.toString());
19+
20+
time.incrementHour();
21+
System.out.println("add 1 hour");
22+
System.out.printf(" %s\n", time.toString());
23+
24+
for (int i = 0; i <= 1; i++)
25+
time.tick();
26+
27+
System.out.println("into the next day");
28+
System.out.printf(" %s\n", time.toString());
29+
30+
}//end method main
31+
}//end class
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Fig. 8.5: Time2.java
2+
// Time2 class declaration with overloaded constructors.
3+
4+
public class Time2
5+
{
6+
private int hour; // 0 - 23
7+
private int minute; // 0 - 59
8+
private int second; // 0 - 59
9+
10+
// Time2 no-argument constructor:
11+
// initializes each instance variable to zero
12+
public Time2()
13+
{
14+
this(0, 0, 0); // invoke constructor with three arguments
15+
}
16+
17+
// Time2 constructor: hour supplied, minute and second defaulted to 0
18+
public Time2(int hour)
19+
{
20+
this(hour, 0, 0); // invoke constructor with three arguments
21+
}
22+
23+
// Time2 constructor: hour and minute supplied, second defaulted to 0
24+
public Time2(int hour, int minute)
25+
{
26+
this(hour, minute, 0); // invoke constructor with three arguments
27+
}
28+
29+
// Time2 constructor: hour, minute and second supplied
30+
public Time2(int hour, int minute, int second)
31+
{
32+
if (hour < 0 || hour >= 24)
33+
throw new IllegalArgumentException("hour must be 0-23");
34+
35+
if (minute < 0 || minute >= 60)
36+
throw new IllegalArgumentException("minute must be 0-59");
37+
38+
if (second < 0 || second >= 60)
39+
throw new IllegalArgumentException("second must be 0-59");
40+
41+
setTime (hour, minute, second);
42+
}
43+
44+
// Time2 constructor: another Time2 object supplied
45+
public Time2(Time2 time)
46+
{
47+
// invoke constructor with three arguments
48+
this(time.getHour(), time.getMinute(), time.getSecond());
49+
}
50+
51+
// Set Methods
52+
// set a new time value using universal time;
53+
// validate the data
54+
public void setTime(int hour, int minute, int second)
55+
{
56+
if (hour < 0 || hour >= 24)
57+
throw new IllegalArgumentException("hour must be 0-23");
58+
59+
if (minute < 0 || minute >= 60)
60+
throw new IllegalArgumentException("minute must be 0-59");
61+
62+
if (second < 0 || second >= 60)
63+
throw new IllegalArgumentException("second must be 0-59");
64+
65+
setHour(hour);
66+
setMinute(minute);
67+
setSecond(second);
68+
}
69+
70+
// validate and set hour
71+
public void setHour(int hour)
72+
{
73+
if (hour < 0 || hour >= 24)
74+
throw new IllegalArgumentException("hour must be 0-23");
75+
76+
this.hour = hour;
77+
}
78+
79+
// validate and set minute
80+
public void setMinute(int minute)
81+
{
82+
if (minute < 0 || minute >= 60)
83+
throw new IllegalArgumentException("minute must be 0-59");
84+
85+
this.minute = minute;
86+
}
87+
88+
// validate and set second
89+
public void setSecond(int second)
90+
{
91+
if (second < 0 || second >= 60)
92+
throw new IllegalArgumentException("second must be 0-59");
93+
94+
this.second = second;
95+
}
96+
97+
// Get Methods
98+
// get hour value
99+
public int getHour()
100+
{
101+
return hour;
102+
}
103+
104+
// get minute value
105+
public int getMinute()
106+
{
107+
return minute;
108+
}
109+
110+
// get second value
111+
public int getSecond()
112+
{
113+
return second;
114+
}
115+
116+
// Tick time
117+
public void tick()
118+
{
119+
setSecond(second +1);
120+
121+
if(second == 0)
122+
incrementMinute();
123+
}
124+
125+
public void incrementMinute()
126+
{
127+
setMinute(minute +1);
128+
129+
if(minute == 0)
130+
incrementHour();
131+
}
132+
133+
public void incrementHour()
134+
{
135+
setHour (hour +1);
136+
}
137+
138+
// convert to String in universal-time format (HH:MM:SS)
139+
public String toUniversalString()
140+
{
141+
return String.format(
142+
"%02d:%02d:%02d", getHour(), getMinute(), getSecond());
143+
}
144+
145+
// convert to String in standard-time format (H:MM:SS AM or PM)
146+
public String toString()
147+
{
148+
return String.format("%d:%02d:%02d %s",
149+
((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
150+
getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
151+
}
152+
} // end class Time2
153+
154+
/**************************************************************************
155+
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
156+
* Pearson Education, Inc. All Rights Reserved. *
157+
* *
158+
* DISCLAIMER: The authors and publisher of this book have used their *
159+
* best efforts in preparing the book. These efforts include the *
160+
* development, research, and testing of the theories and programs *
161+
* to determine their effectiveness. The authors and publisher make *
162+
* no warranty of any kind, expressed or implied, with regard to these *
163+
* programs or to the documentation contained in these books. The authors *
164+
* and publisher shall not be liable in any event for incidental or *
165+
* consequential damages in connection with, or arising out of, the *
166+
* furnishing, performance, or use of these programs. *
167+
*************************************************************************/

0 commit comments

Comments
 (0)