Test: Computer Science

Consider the following code:

public class Clock {

     private int seconds;

 

     public Clock(int s) {

          seconds = s;

     }

 

     public void setTime(int s) {

          seconds = s;

     }

 

     public void setSeconds(int s) {

          int hoursMinutes = seconds - seconds % 60;

          seconds = hoursMinutes + s;

     }

 

     public void setMinutes(int min) {

          int hours = seconds / 3600;

          int currentSeconds = seconds % 60;

          seconds = hours + min * 60 + currentSeconds;

     }

}

1.

Which of the following defines a toString method that will output the time in 24-hour format in the following form:

1:51:03

(Notice that you need to pad the minutes and seconds. You can call 12 midnight "0".)

public String toString() {

     int hours = seconds / 3600;

     int mins =  seconds - (hours * 3600) / 60;

     String minString = ""+mins;

     if(mins < 10) {

          minString = "0" + minString;

     }

     int secs = seconds * 60;

     String secString = "" + secs;

     if(secs < 10) {

          secString = "0" + secString;

     }

     return hours + ":"+minString+":"+secString;

}

public String toString() {

     int hours = seconds / 60;

     int mins =  seconds - (hours * 3600) / 60;

     String minString = ""+mins;

     if(mins < 10) {

          minString = "0" + minString;

     }

     int secs = seconds * 60;

     String secString = "" + secs;

     if(secs < 10) {

          secString = "0" + secString;

     }

     return hours + ":"+minString+":"+secString;

}

public String toString() {

     int hours = seconds / 3600;

     int mins =  (seconds - hours * 3600) / 60;

     String minString = ""+mins;

     if(mins < 10) {

          minString += "0";

     }

     int secs = seconds % 60;

     String secString = "" + secs;

     if(secs < 10) {

          secString += "0";

     }

     return hours + ":"+minString+":"+secString;

}

public String toString() {

     int hours = seconds / 3600;

     int mins =  (seconds - hours * 3600);

     String minString = ""+mins;

     if(mins < 10) {

          minString = "0" + minString;

     }

     int secs = seconds % 60;

     String secString = "" + secs;

     if(secs < 10) {

          secString = "0" + secString;

     }

     return hours + ":"+minString+":"+secString;

}

public String toString() {

     int hours = seconds / 3600;

     int mins =  (seconds - hours * 3600) / 60;

     String minString = ""+mins;

     if(mins < 10) {

          minString = "0" + minString;

     }

     int secs = seconds % 60;

     String secString = "" + secs;

     if(secs < 10) {

          secString = "0" + secString;

     }

     return hours + ":"+minString+":"+secString;

}

1/3 questions

0%

Access results and powerful study features!

Take 15 seconds to create an account.
Start now! Create your free account and get access to features like:
  • Full length diagnostic tests
  • Invite your friends
  • Access hundreds of practice tests
  • Monitor your progress over time
  • Manage your tests and results
  • Monitor the progress of your class & students
By clicking Create Account you agree that you are at least 13 years old and you agree to the Varsity Tutors LLC Terms of Use and Privacy Policy.
Learning Tools by Varsity Tutors