110k views
2 votes
The purpose of this first assignment is to review some concepts from Computer Science I. Create a project called Assignment 1. You will then write two classes: Clock.java - Write a class named Clock that contains the following variables, methods, and constructors: (a) Private instance variables that store hours (0-23), minutes (0-59), and seconds (0-59). (b) A constructor that initializes the hours, minutes, and seconds to values specified by parameters. (c) A constructor that initializes the hours, minutes to values specified by parameters (defaulting the seconds to 0) (d) A constructor that initializes the hours to a value specified by a parameter (defaulting the minutes and seconds to 0 ) (e) A no-arg constructor that sets the hours, minutes, and seconds to 0 . (f) A method that resets the hours, minutes, and seconds to 0 . (g) A method that advances the clock by one second. (h) Getters and setters for hours, minutes, and seconds. (i) A toString method that returns the hours, minutes, and seconds as a string of the form "hh:mm:ss". TestClock.java - A driver class that contains only a main method that testing all constructors and methods in

User Kevinmm
by
7.7k points

1 Answer

5 votes

Answer:

Clock Java:

public class Clock {

private int hours;

private int minutes;

private int seconds;

// Constructor to initialize hours, minutes, and seconds

public Clock(int hours, int minutes, int seconds) {

this.hours = hours;

this.minutes = minutes;

this.seconds = seconds;

}

// Constructor to initialize hours and minutes (seconds default to 0)

public Clock(int hours, int minutes) {

this(hours, minutes, 0);

}

// Constructor to initialize hours (minutes and seconds default to 0)

public Clock(int hours) {

this(hours, 0, 0);

}

// No-arg constructor (initializes all fields to 0)

public Clock() {

this(0, 0, 0);

}

// Method to reset the clock to 0

public void reset() {

this.hours = 0;

this.minutes = 0;

this.seconds = 0;

}

// Method to advance the clock by one second

public void tick() {

seconds++;

if (seconds == 60) {

seconds = 0;

minutes++;

if (minutes == 60) {

minutes = 0;

hours++;

if (hours == 24) {

hours = 0;

}

}

}

}

// Getters and setters for hours, minutes, and seconds

public int getHours() {

return hours;

}

public void setHours(int hours) {

this.hours = hours;

}

public int getMinutes() {

return minutes;

}

public void setMinutes(int minutes) {

this.minutes = minutes;

}

public int getSeconds() {

return seconds;

}

public void setSeconds(int seconds) {

this.seconds = seconds;

}

// toString method to display the time in "hh:mm:ss" format

public String toString() {

return String.format("%02d:%02d:%02d", hours, minutes, seconds);

}

}

Test.Clock Java

public class TestClock {

public static void main(String[] args) {

// Testing Clock class

Clock clock1 = new Clock(10, 30, 45);

Clock clock2 = new Clock(8, 15);

Clock clock3 = new Clock(4);

Clock clock4 = new Clock();

System.out.println("Clock 1: " + clock1.toString());

System.out.println("Clock 2: " + clock2.toString());

System.out.println("Clock 3: " + clock3.toString());

System.out.println("Clock 4: " + clock4.toString());

clock1.reset();

System.out.println("Clock 1 after reset: " + clock1.toString());

clock2.tick();

System.out.println("Clock 2 after ticking: " + clock2.toString());

// Testing getters and setters

clock3.setHours(12);

clock3.setMinutes(45);

clock3.setSeconds(20);

System.out.println("Clock 3 after setting values: " + clock3.toString());

}

}

Step-by-step explanation:

User Dath
by
8.0k points

No related questions found