In: Computer Science
Programming Language : JAVA
Create an interface named Turner, with a single method called turn(). Then create 4 classes:
1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random.
2- Document: that implements turn(), which changes the page on the document to the next page and returns true. If the current page is the last page of the document, then it returns false. This class should have a method called returnIdDate(), which returns a String containing the ID of the Document and the date it was published.
3- Pancake: that implements turn(), which flips the pancake if it has not flipped before and returns true. If the pancake has already been flipped, then it returns false.
4- Think of one more objects that can use turn(). Create the class and implement the turn() method. Show your creativity.
Write an application, DemoTurners, which:
Interface allows us to add polymorphism into our code. An interface defines a template for a class. Then any class which implements it, need to add the implementation for the template methods, in this case it is turn().
Java Code:
Interface:
interface Turner {
public boolean turn();
}
Classes which implement this interface:
Pancake Class
class Pancake implements Turner{
//to store the status of pancake
boolean isFlipped = false;
public boolean turn(){
//do not turn if isFlipped is true
if (!isFlipped)
isFlipped = true;
else return false;
return true;
}
}
Document Class:
class Document implements Turner{
//current and last page values
int lastPage = 20;
int currentPage = 0;
public boolean turn(){
//if on last page, do not turn
if (currentPage==lastPage)
return false;
currentPage = currentPage + 1;
return true;
}
String returnIdDate(){
//returning the required string
return "Document ID: IS2165\nPublishing Date: 23rd September, 2019";
}
}
Leaf Class:
class Leaf implements Turner{
// store color list
String colorsList[] = {"White","Green","Yellow","Pink"};
String color;
public boolean turn() {
//generate random number using Math.random
// it returns double value between 0 and 1, so we convert it into integer
int randomNo = (int) (Math.random()*10);
if (randomNo<=3)
this.color = colorsList[randomNo];
else return false;
return true;
}
}
Chair Class (One extra class that needs to be created):
class Chair implements Turner{
boolean isFlipped = false;
public boolean turn(){
//set the new state opposite to current
isFlipped = !isFlipped;
return true;
}
}
DemoTurner class to test the above classes:
import java.util.ArrayList;
public class DemoTurner{
public static void main(String[] args) {
//list of generic type
ArrayList<Turner> demoTurner = new ArrayList<Turner>();
//adding the objects
demoTurner.add(new Chair());
demoTurner.add(new Pancake());
demoTurner.add(new Leaf());
demoTurner.add(new Document());
//traversing the list to invoke turn() for every object
for (int i = 0; i < demoTurner.size(); i++) {
System.out.println(demoTurner.get(i).turn());
}
//invoking special method of Document class
System.out.println(((Document)demoTurner.get(3)).returnIdDate());
}
}
Reference screenshots:
Output of the program:
In the for loop we simply write demoTurner.get(i).turn() and it automatically runs the respective method implementation of each class. This is because this method has been declared in interface.
To call the special method of Document class, we first get the object from demoList and then type cast it to Document type. After this we can call such methods.