Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Title: TestTimer. Summary: Using interfaces to hide implementation of a class using Java 8 new features. Question: An interface has public abstract methods and public
Title: TestTimer. Summary: Using interfaces to hide implementation of a class using Java 8 new features. Question: An interface has public abstract methods and public default methods. An interface cannot have instance variables, except for public static final fields. An interface is a contract expressing what services that should be provided. For example, the Iterator interface in java.util provide four method headers: public boolean hasNext () public Object next () default void remove ( ) default void forEachRemaining (Consumer action) A class can implement an interface by supplying definitions for all the interface's methods. Implementing interfaces by a class is a decision on how the contract is implemented. For example, a class ABC implements the interface Iterator as follows: public class ABC implements Iterator { // Return true if there are more elements // to be iterated through and false otherwise. public boolean hasNext () { } // Return the next element in the iteration sequence. // Precondition: hasNext (); public Object next() { } 1/ Remove the object most recently returned. // Precondition: next has been called. // It is an optional method;i.e, body can be empty public void remove () { ) // other methods Notice that default methods can be left empty. Advantages of interfaces is that a class can implement many of them and an interface can extend another interface. The usefulness of interfaces can be demonstrated by the following example. Let's assume that class Student needs to look for meanings of English words. A Student can use class Tablet or Dictionary. The class Tablet searches Internet for word meaning and a Dictionary looks up a word in collection of words stored in its memory. When writing Java code for Student, we do not know what way (t) Student would use to find the meaning of a given word. public class Tablet ! public String findMeaning (String word) { // Search Internet to find word meaning } ) public class Dictionary { public String findMeaning (String word) { // Find word in storage and return a definition } ) public class Student { static String what It DoesMean (t device) { return device. findMeaning ("word"); } The type of wordLookup a student should a student be used to enable student to use all available means (Tablet and Dictionary) to find meanings of English words. Replacing the type t by Tablet or Dictionary would restrict Student to search for words' meanings either using a Tablet or a Dictionary. To enable Student searching via both ways, an interface that provides lookupMeaning method is defined, and implemented by both Tablet ablet and Dictionary classes. Code becomes as follows: public interface Meaning ! public String findMeaning (String wordLookup); ) public class Tablet implements Meaning { public String findMeaning (String word) { // Search Internet to find word meaning } } public class Dictionary implements Meaning public String findMeaning (String word) { // Find word in storage and return a definition } } public class Student ! static String what Does ItMean (Meaning device) { return device. findMeaning("simultaneous"); } } In this task, you are required to apply some Java provided interfaces. Let's use the ActionListener interface provided in java.awt.event in order to get notified every 5 seconds (5000 milliseconds). 1. Create a Beeper class that implements the ActionListener interface. From Java documentation available on your Desktop, find out what non-default method(s) of ActionListener should be implemented in Beeper. The method should print to the console messages in the following format: Alert, the time is Wed Jan 02 09:36:39 EET 2019 Alert, the time is Wed Jan 02 09:36:44 EET 2019 Alert, the time is Wed Jan 02 09:36:49 EET 2019 Notice the 5 seconds difference. 2. Write a new class TestTimer with a main method to perform the following steps: a. Create a listener instance of type ActionListener of the class Beeper. b. Create a timer object of the Timer type provided in javax.swing package, and set its interval to 5 seconds and link it with your listener object. Read Java documentation on Timer construction. c. Show a dialog with the message Exit Program using the following two lines of code: JOptionPane.showMessageDialog(null, "Exit program?"); System.exit(0); Read Java documentation about JOptionPane and the method showMessageDialog. 3. Compile, run and take snapshots of your successful attempt. Store in folder 108 all documents including screenshot, java & class files, compress and upload to the course website. Title: TestTimer. Summary: Using interfaces to hide implementation of a class using Java 8 new features. Question: An interface has public abstract methods and public default methods. An interface cannot have instance variables, except for public static final fields. An interface is a contract expressing what services that should be provided. For example, the Iterator interface in java.util provide four method headers: public boolean hasNext () public Object next () default void remove ( ) default void forEachRemaining (Consumer action) A class can implement an interface by supplying definitions for all the interface's methods. Implementing interfaces by a class is a decision on how the contract is implemented. For example, a class ABC implements the interface Iterator as follows: public class ABC implements Iterator { // Return true if there are more elements // to be iterated through and false otherwise. public boolean hasNext () { } // Return the next element in the iteration sequence. // Precondition: hasNext (); public Object next() { } 1/ Remove the object most recently returned. // Precondition: next has been called. // It is an optional method;i.e, body can be empty public void remove () { ) // other methods Notice that default methods can be left empty. Advantages of interfaces is that a class can implement many of them and an interface can extend another interface. The usefulness of interfaces can be demonstrated by the following example. Let's assume that class Student needs to look for meanings of English words. A Student can use class Tablet or Dictionary. The class Tablet searches Internet for word meaning and a Dictionary looks up a word in collection of words stored in its memory. When writing Java code for Student, we do not know what way (t) Student would use to find the meaning of a given word. public class Tablet ! public String findMeaning (String word) { // Search Internet to find word meaning } ) public class Dictionary { public String findMeaning (String word) { // Find word in storage and return a definition } ) public class Student { static String what It DoesMean (t device) { return device. findMeaning ("word"); } The type of wordLookup a student should a student be used to enable student to use all available means (Tablet and Dictionary) to find meanings of English words. Replacing the type t by Tablet or Dictionary would restrict Student to search for words' meanings either using a Tablet or a Dictionary. To enable Student searching via both ways, an interface that provides lookupMeaning method is defined, and implemented by both Tablet ablet and Dictionary classes. Code becomes as follows: public interface Meaning ! public String findMeaning (String wordLookup); ) public class Tablet implements Meaning { public String findMeaning (String word) { // Search Internet to find word meaning } } public class Dictionary implements Meaning public String findMeaning (String word) { // Find word in storage and return a definition } } public class Student ! static String what Does ItMean (Meaning device) { return device. findMeaning("simultaneous"); } } In this task, you are required to apply some Java provided interfaces. Let's use the ActionListener interface provided in java.awt.event in order to get notified every 5 seconds (5000 milliseconds). 1. Create a Beeper class that implements the ActionListener interface. From Java documentation available on your Desktop, find out what non-default method(s) of ActionListener should be implemented in Beeper. The method should print to the console messages in the following format: Alert, the time is Wed Jan 02 09:36:39 EET 2019 Alert, the time is Wed Jan 02 09:36:44 EET 2019 Alert, the time is Wed Jan 02 09:36:49 EET 2019 Notice the 5 seconds difference. 2. Write a new class TestTimer with a main method to perform the following steps: a. Create a listener instance of type ActionListener of the class Beeper. b. Create a timer object of the Timer type provided in javax.swing package, and set its interval to 5 seconds and link it with your listener object. Read Java documentation on Timer construction. c. Show a dialog with the message Exit Program using the following two lines of code: JOptionPane.showMessageDialog(null, "Exit program?"); System.exit(0); Read Java documentation about JOptionPane and the method showMessageDialog. 3. Compile, run and take snapshots of your successful attempt. Store in folder 108 all documents including screenshot, java & class files, compress and upload to the course website
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started