Answered step by step
Verified Expert Solution
Question
1 Approved Answer
TASK 8 - Software construction and documentation Aim: To demonstrate advanced features of Java interfaces. Title: TestTimer. Summary: Using interfaces to hide implementation of a
TASK 8 - Software construction and documentation Aim: To demonstrate advanced features of Java interfaces. 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 haaNext() public Object next () 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 Iteratort default void remove() // Return true if there are more elements // to be iterated through and false otherwise. public boolean has ext() ) 1 Return the next element in the iteration sequence. 11 Precondition: hasNext(): public object next() 1/ Remove the object most recently returned. // Brecondition: next has been called. IT It is an optional method;i.c, body can be empty public void remove () { } other methods } Notice that default inethods can be left empty. TASK 8 - Software construction and documentation 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 findleaning (String word) { // Pind word in storage and return - definition ) public class Student static String what It DoesMean it device) return device.findMeaning (word"); 1 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 and Dictionary classes. Code becomes as follows: TASK 8 - Software construction and documentation public interface Meaning i public String findMeaning (String wordLookup): 1 public class Tablet implements Meaning ! public String findleaning (string word) { // Search Internet to find word meaning ) ) public class Dictionary implements Meaning i public String CindMeaning (String word) // Find word in storage and return a definition 1 public class Student static String what Does It Mean (Meaning device) return device.findMeaning ("simultaneous"); 1 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: TASK 8 - Software construction and documentation 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 TOS 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