Home>

Here is the problem.

Implement the interface IAlarm, IClock, and Problemx4_2 in the main method so that the following program will run as expected.

The expected answer is below.

Set the alarm
Sound an alarm
Stop the alarm
Correct the time
Know the time

In the code I implemented below, nothing is displayed when I run it.
Where should I fix it? I want you to teach me.

<AlarmClock>

public class AlarmClock implements IAlarm, IClock {
    public void alarm () {
        System.out.println ("Sound alarm");
    }
    public void setAlarm () {
        System.out.println ("Set alarm");
    }
    public void stopAlarm () {
        System.out.println ("Stop alarm");
    }
    public void showTime () {
        System.out.println ("Know the time");
    }
    public void adjustTime () {
        System.out.println ("Correct the time");
    }
}

<Problemex4_2>

public class Problemex4_2 {
    / **
     * @param args
     * /
    public static void main (String [] args) {
        AlarmClock ac = new AlarmClock ();// Create an instance of the alarm clock class
    }
    // processing as an alarm
    public static void funcAlarm (IAlarm alarm) {
        IAlarm.setAlarm ();// Set alarm
        IAlarm.alarm ();// Sound an alarm
        IAlarm.stopAlarm ();// Stop the alarm
    }
    // Processing as a clock
    public static void funcClock (IClock clock) {
        IClock.adjustTime ();// Know the time
        IClock.showTime ();// Correct the time
    }
}

<IClock>

public interface IClock {
    static void adjustTime () {
    }
    static void showTime () {
    }
}

<IAlarm>

public interface IAlarm {
    public static void setAlarm () {
    }
    public static void alarm () {
    }
    public static void stopAlarm () {
    }
}


`` `

  • Answer # 1

    There are various points of concern, but when you run the program, do you know which part of the process is executed first?

    If you know that, you will understand why nothing is displayed.

  • Answer # 2

    It wasn't clear which part was given the problem and which part was filled in, but it was an example of using interace so that the execution result in the question sentence can be obtained I tried to write it.

    public class Problemex4_2 {
        public static void main (String [] args) {
            AlarmClock ac = new AlarmClock ();// Create an instance of the alarm clock class
            funcAlarm (ac);
            funcClock (ac);
        }
        // processing as an alarm
        public static void funcAlarm (IAlarm alarm) {
            alarm.setAlarm ();// Set alarm
            alarm.alarm ();// Sound an alarm
            alarm.stopAlarm ();// Stop the alarm
        }
        // Processing as a clock
        public static void funcClock (IClock clock) {
            clock.adjustTime ();// know the time
            clock.showTime ();// correct the time
        }
    }
    interface IClock {
        public void adjustTime ();
        public void showTime ();
    }
    interface IAlarm {
        public void setAlarm ();
        public void alarm ();
        public void stopAlarm ();
    }
    class AlarmClock implements IAlarm, IClock {
        public void alarm () {
            System.out.println ("Sound alarm");
        }
        public void setAlarm () {
            System.out.println ("Set alarm");
        }
        public void stopAlarm () {
            System.out.println ("Stop alarm");
        }
        public void showTime () {
            System.out.println ("Know the time");
        }
        public void adjustTime () {
            System.out.println ("Correct the time");
        }
    }


    Execution example:

    public void setAlarm ();where
    public static void setAlarm ();
    Or
    public default void setAlarm ();
    The intention of the design changes depending on.
    Naturally, the way of writing in the AlarcClock class and main () will also change.

  • Answer # 3

    Reference question: What are the benefits of an interface?

    Addition: I noticed an error by looking at @ katoy's answer and corrected it.

    Public static void will not work unless you understand the overall flow, such as changing to public void.
    Finally, it was a copy of @katoy.


    Since there is no answer, I am not confident so I wrote it for confirmation.
    I would be grateful if you could correct it.

    When you search the interface, there seem to be many explanations of nuances such as implements = "contract" and "promise".
    This seems to be useful when changes or modifications are minimized, even if they are replaced with something that is no longer present.

    The role of the interface in this example may have the following implications:
    IAlarm "promises" to implement alarm-related functions = methods! "Contract".
    setAlarm () alarm () stopAlarm ()

    IClock "promises" to implement clock-related functions = methods! "Contract".
    adjustTime () showTime ()

    AlarmClock with implements IAlarm, IClock
    Since the alarm and clock functions are "contracted" there is always a "promised" method.

    I think it will be possible to implement on the main () side.

    public class SampleMain {
        public static void main (String [] args) {
            // Create an instance of the clock class with alarm
            SampleAlarmClock ac = new SampleAlarmClock ();
            funcAlarm (ac);
            funcClock (ac);
        }
        // processing as an alarm
        public static void funcAlarm (SampleIAlarm ac) {
            SampleIAlarm.setAlarm ();
            SampleIAlarm.alarm ();
            SampleIAlarm.stopAlarm ();
        }
        // Processing as a clock
        public static void funcClock (SampleIClock ac) {
            SampleIClock.adjustTime ();
            SampleIClock.showTime ();
        }
    }
    // /// * I think the following is wrong, so I correct it and leave it as a bad example
    // If // AlarmClock is changed, main () also needs to be modified
    // // processing as an alarm
    // public static void funcAlarm (SampleAlarmClock ac) {
    // ac.setAlarm ();
    // ac.alarm ();
    // ac.stopAlarm ();
    //}
    //
    // // Processing as a clock
    // public static void funcClock (SampleAlarmClock ac) {
    // ac.adjustTime ();
    // ac.showTime ();
    //}
    public class SampleAlarmClock implements SampleIAlarm, SampleIClock {
        public void alarm () {
            System.out.println ("Sound alarm");
        }
        public void setAlarm () {
            System.out.println ("Set alarm");
        }
        public void stopAlarm () {
            System.out.println ("Stop alarm");
        }
        public void showTime () {
            System.out.println ("Know the time");
        }
        public void adjustTime () {
            System.out.println ("Correct the time");
        }
    }
    public interface SampleIAlarm {
        public void setAlarm ();
        public void alarm ();
        public void stopAlarm ();
    }
    public interface SampleIClock {
        public void adjustTime ();
        public void showTime ();
    }