0

I couldn't get how to finish this program that was tasked to us and it's about setting an alarm by creating and implementing an interface.

This is the instructions that was given to us.

Procedure/Instructions

enter image description here

Now this is what I finished so far on my code:

import java.util.Scanner;
import java.time.LocalTime;

public class Monday {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);      
        System.out.println("Enter time for alarm in this format (HH:MM): ");
        int enteredAlarm = poli.nextInt();
            
        if{
        }
        else if{
        }
    }
}

interface Alarm {
    public void setAlarm(String time) {
    }
    public void showAlarm() {
        LocalTime alarm = LocalTime.parse(time);
        LocalTime now = LocalTime.now();
    }
}

abstract class Weekday implements Alarm {
}

class Monday extends Weekday {
}

Now this is what the expected output is, which I cannot finish because I don't know how to display it by using user input, and the instructions that was given to us is confusing (Which is on the image above).

Enter time for alarm in this format (HH:MM): 05:30

Alarm is set for tomorrow!

Stewart
  • 17,616
  • 8
  • 52
  • 80
  • I would start by noticing that you're trying to _implement_ methods inside an _interface_. While that can be done in principle (using the `default` keyword), the goal of this task is clearly to have you _define_ a method in the `Alarm` interface and implement it (that is, `@Override` it) inside `Weekday` or `Monday`. – OLEGSHA Dec 04 '20 at 08:41
  • What method will work on the Alarm interface? – Mark Dizon Dec 04 '20 at 10:32
  • It's in your instructions. setAlarm(time) and showAlarm() – Gilbert Le Blanc Dec 04 '20 at 11:28

1 Answers1

0

Create a public class named Monday. This class shall contain the main method:

public class Monday {
    public static void main(String[] args) {
    }
}

Define an interface named Alarm ...

This interface is declaring two methods

public interface Alarm {
    void showAlarm();
    void setAlarm(String time);
}

Add two (2) classes ... For the Weekday class, implement the Alarm interface. Leave this class empty.

public abstract class Weekday
    implements Alarm {
}

Set Weekday as the parent of Monday.

Because of interface Alarm on Weekday class, which Monday now extends, we have to define the two methods which Alarm has declared.

public class Monday extends Weekday {

    public static void main(String[] args) {
    }

    @Override
    public void showAlarm() {
    }

    @Override
    public void setAlarm(String time) {
    }
}

Import Scanner and LocalTime ... add these two (2) statements.

import java.time.LocalTime;
import java.util.Scanner;

public class Monday extends Weekday {

    public static void main(String[] args) {
    }

    @Override
    public void showAlarm() {
        LocalTime alarm = LocalTime.parse(time); // This gives a compile error at this point
        LocalTime now = LocalTime.now();
    }

    @Override
    public void setAlarm(String time) {
    }
}

The output shall ask the user to enter the time for the alarm.

This instruction is ambiguous. Output is output. Perhaps the instruction means use Scanner in the main() method, and then use setAlarm()?

See sample output below.

So we have missing context in the posted question. Anyway I'll make a best guess. Here's the final Monday class with its strange alarm logic ...

import java.time.LocalTime;
import java.util.Scanner;

public class Monday extends Weekday {

    public static void main(String[] args) {
        Weekday weekday = new Monday();
        
        System.out.println("Enter time for alarm in this format (HH:MM):");
        try(Scanner input = new Scanner(System.in)) {
            weekday.setAlarm(input.next());
            weekday.showAlarm();
        }
    }

    @Override
    public void showAlarm() {
        LocalTime alarm = LocalTime.parse(this.time); // This no longer gives a compile error - it refers to private field "time"
        LocalTime now = LocalTime.now();
        if(alarm.isAfter(now)) {
            System.out.println("Alarm is set for tomorrow!");
        } else {
            System.out.println("I'll wake you up later!");
        }
    }

    @Override
    public void setAlarm(String time) {
        this.time = time;
    }

    private String time;
}
Stewart
  • 17,616
  • 8
  • 52
  • 80