Questions 13 refer to the Time class declared below public c

Questions 1-3 refer to the Time class declared below: public class Time {private int hrs; private int mins; private int secs; public Time() {/* implementation not shown */} public Time(int h, int m, int s) {/* implementation not shown */}/** Resets time to hrs = h, mins = m, secs = s. */public void resetTime(int h, int m, int s) {/* implementation not shown */}/** Advances time by one second. */public void increment() {/* implementation not shown */}/** return true if this time equals t, false otherwise */public boolean equals(Time t) {/* implementation not shown */}/** return true if this time is earlier than t, false otherwise */public boolean lessThan(Time t) {/* implementation not shown */}/** return a String with the time in the form hrs:mins:secs */public String toString() {/* implementation not shown */}} Which of the following represents correct implementation code for the constructor with parameters? hrs = 0; mins -= 0; secs = 0; hrs = h; mins = m; secs = s; resetTime(hrs, mins, secs); h = hrs; m = mins; s = secs; Time = new Time(h, m, s);

Solution

(B)

We are looking for the implementation code for the constructor with parameters.

CONSTRUCTOR : An instance method that has the same name as the class which can be used to set the default or the user-defined values of the members of the object created

Here user-defined values are considered.

Constructor has 3 parameters : h, m, a
Class has 3 variables : hrs, mins, secs

We need to give the values came from parameters to the existing class variables. So, following is the correct way of doing it.

hrs = h;
mins = m;
secs = s;

 Questions 1-3 refer to the Time class declared below: public class Time {private int hrs; private int mins; private int secs; public Time() {/* implementation

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site