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:
In java Promgramming
Java Code:
import java.util.ArrayList;
//interface Turner
interface Turner {
public boolean turn();
}
// class leaf which implements Turner interface
class Leaf implements Turner{
//color of leaf
String color;
//list of colors
String colors[] = {"Green","Orange","Blue","Red"};
//implementation of turn() method
public boolean turn() {
//generating random number
int random = (int) (Math.random()*10);
//turn only if random number generated is <=3
if (random<=3)
this.color = colors[random];
else return false;
return true;
}
}
// class document which implements Turner interface
class Document implements Turner{
//current page of document
int page = 0;
//last page of document
int lastpage = 100;
//implementation of turn() method
public boolean turn(){
//turn only if not on the last page
if (page==lastpage)
return false;
page = page + 1;
return true;
}
//method to return the id and publishing date of document
String returnIdDate(){
return "Document ID: DM2547\nPublishing Date: 29 October";
}
}
// class pancake which implements Turner interface
class Pancake implements Turner{
//to store that whether panacake has been flipped or not
boolean flipped = false;
//implementation of turn() method
public boolean turn(){
//only turn if not already done
if (!flipped)
flipped = true;
else return false;
return true;
}
}
// class pancake which implements Turner interface
class Smartphone implements Turner{
//to store the current state of phone
boolean flipped = false;
//implementation of turn() method
public boolean turn(){
// toggle between flipped states
flipped = !flipped;
return true;
}
}
//test class
public class DemoTurner{
public static void main(String[] args) {
//arrayList of type turner
ArrayList<Turner> list = new ArrayList<Turner>();
//adding all the objects in the array list
list.add(new Leaf());
list.add(new Document());
list.add(new Pancake());
list.add(new Smartphone());
//using for each loop to call turn() for every object
list.forEach(turnerObject -> {System.out.println(turnerObject.turn());});
// invoking returnIdDate() for Document object
System.out.println(((Document)list.get(1)).returnIdDate());
}
}
Output:
Screenshots for better clarity:
Interface is used to provide polymorphism in the code. When we invoke the turn method in loop, the respective method of each object gets called. To invoke the returnIdDate() function for document object, we first get that object from the arrayList using get method. It returns us a generic object of turner type. After type casting it using (Document), we can call the respective method/
Get Answers For Free
Most questions answered within 1 hours.