Create and implement a class called clockType with the following data and methods
import java.util.Scanner;
public class clockType {
private int hours;
private int minutes;
private int seconds;
public clockType(){ //Default constructor
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
public clockType(int hours, int minutes, int seconds){ //Overloading constructor
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
// Getters
public int getHours() { //getter for hours
return hours;
}
public int getMinutes() { //getter for minutes
return minutes;
}
public int getSeconds(){ //getter for seconds
return seconds;
}
// Setters
public void setHours(int hours) { //setter for hours
this.hours = hours;
}
public void setMinutes(int minutes) { //setter for minutes
this.minutes = minutes;
}
public void setSeconds(int seconds) { //setter for seconds
this.seconds = seconds;
}
public void printTime(){ //display time in the form of hh:mm:ss
System.out.println(this.hours+":"+this.minutes+":"+this.seconds);
}
public static void main(String[] args) {
clockType obj = new clockType(); // using default constructor
Scanner sc = new Scanner(System.in);
System.out.print("Enter Hours: ");
//using setter to set hours
obj.setHours(sc.nextInt());
System.out.print("Enter Minutes: ");
//using setter to set minutes
obj.setMinutes(sc.nextInt());
System.out.print("Enter Seconds: ");
//using setter to set seconds
obj.setSeconds(sc.nextInt());
// displaying Time
obj.printTime();
}
}
Sample Output
Get Answers For Free
Most questions answered within 1 hours.