wait po
Expert Answer
Explanation
Here I have created the interface Alarm with 2 function declarations.
Next, I have created a class named Weekday of the abstract type that implements the interface and is an empty class.
Then, I have created another class named Monday that extends the class Weekday and defines the method declared in the interface.
In the setAlarm() function, I have set the value to the local variable named time of type string.
Next, in the showAlarm() function after creating the object of LocalTime, I have called the function isBefore() and isAfter() to compare the dates and print the result to the console.
In the main method, I have taken input from the user and then called the function to set and display the status of the alarm.
Answer
Java code:
import java.time.LocalTime;
import java.util.Scanner;
// interface Alarm
interface Alarm{
// abstract methods
void setAlarm(String time);
void showAlarm();
}
// empty abstract class Weekday
abstract class Weekday implements Alarm{
}
// class that extends Weekday and define the abstract methods
public class Monday extends Weekday{
// to store the time
String time;
// to the set the time entered by user
Override
public void setAlarm(String time) {
this.time = time;
}
// to compare time and print the result
Override
public void showAlarm() {
LocalTime alarm = LocalTime.parse(time);
LocalTime now = LocalTime.now();
// comparing the times
if(alarm.isAfter(now))
System.out.println("Alarm is set for tomorrow!");
else if (alarm.isBefore(now))
System.out.println("I'll wake you up later!");
}
// main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// user input
System.out.print("Enter time for alarm in this format (HH:MM): ");
String inputTime = sc.nextLine();
// creating the object
Monday obj = new Monday();
// calling the setAlarm method
obj.setAlarm(inputTime);
// calling the showAlarm method
obj.showAlarm();
}
}
Screenshot of the output:
Hope it helps!!