CSC1110 Homework 7
Complete Exercise 7.3 (pp. 317)
This exercise has you trace a program with multiple objects. The program with explanations can be found on pages 277-278 (Figures 7.2 and 7.3). The code can also be found in the assignment repository. There is an Excel file in the repository to use for the tracing.
Complete the following program
Using the following Time and TimeDriver class skeletons, replace the < insert ...> lines with your own code such that the program operates properly. More specifically:
- In the
Time
class, provide a method definition for thesetHours
method such thatsetHours
can be called as part of a method-call chain. - In the
TimeDriver
class, provide a single statement that chains calls to thesetHours
,setMinutes
,setSeconds
, andprintTime
methods. Use reasonable values for your method-call arguments. If you pass 8 tosetHours
, 59 tosetMinutes
, and 0 tosetSeconds
, then your method-call-chaining statement should print08:59:00
public class Time {
private int hours;
private int minutes;
private int seconds;
//**********************************************************
<insert setHours method definition here>
public Time setMinutes(int minutes) {
this.minutes = minutes;
return this;
} // end setMinutes
public Time setSeconds(int seconds) {
this.seconds = seconds;
return this;
} // end setSeconds
//************************************************************
public void printTime() {
System.out.printf("%02d:%02d:%02d\n", hours, minutes, seconds);
} // end printTime
} // end Time class
public class TimeDriver {
public static void main(String[] args) {
Time time = new Time();
<insert chained-method-calls statement here >
}
} // end TimeDriver class
Submission Instructions:
Commit and Push your files to GitHub Classroom.