Please help me with the java. 1. Define a derived class, AlarmClock , to represe
ID: 3823283 • Letter: P
Question
Please help me with the java.
1.Define a derived class, AlarmClock, to represent an alarm clock, with the following Clock class as its base class.
public class Clock {
private int hour;
private int minute;
private int second;
//default constructor
public Clock() {
//initialize to default values
hour = 0;
minute = 0;
second = 0;
}
//constructor with arguments
public Clock(int h, int m, int s) //set current time
{
hour = h;
minute = m;
second = s;
}
…
}
a. In your AlarmClock subclass, you only need to consider the following two aspects:
The AlarmClock should have three instance variables: alarmHour, alarmMinute, and alarmSecond.
b. Please write two constructors of the derived class AlarmClock -- one is the default constructor that assigns all member/instance variables to zero. The other takes 6 arguments time (ie., hour, minute, second, alarmHour, alarmMinute, alarmMinute.) that sets current time and alarm
(no need to write any other methods)
Explanation / Answer
class Clock
{
private int hour;
private int minute;
private int second;
//default constructor
public Clock()
{
//initialize to default values
hour = 0;
minute = 0;
second = 0;
}
//constructor with arguments
public Clock(int h, int m, int s) //set current time
{
hour = h;
minute = m;
second = s;
}
/*
//Method for printing Clock class members values
public void print()
{
System.out.println(hour+" "+minute+" "+second);
}*/
}
public class AlarmClock extends Clock
{
private int alarmHour;
private int alarmMinute;
private int alarmSecond;
public AlarmClock()
{
alarmSecond=0;
alarmMinute=0;
alarmHour=0;
}
public AlarmClock(int hour,int minute,int second,int alarmHour,int alarmMinute,int alarmSecond)
{
super(hour,minute,second);
this.alarmHour=alarmHour;
this.alarmSecond=alarmSecond;
this.alarmMinute=alarmMinute;
}
/*
//Method for printing AlarmClock hours, minutes and seconds
public void print()
{
super.print();
System.out.println(alarmHour+" "+alarmMinute+" "+alarmSecond);
}*/
public static void main(String args[])
{
AlarmClock a=new AlarmClock(11,23,45,9,12,45);
// a.print();
}
}