Question

Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1-...

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:

  1. Creates an array of 4 Turner objects and adds one object for each of the 4 class types to it.
  2. Demonstrates polymorphism by calling the turn() method on each object in a loop.
  3. Prints the Document object’s returnIdDate method using the Turner object (how can this be done?

In java Promgramming

Homework Answers

Answer #1

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/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the previous problem. In addition, we will add a new class called Mechanic. We will also create a new TestMechanic class that will contain the main method. Create a class called Mechanic with following instance variable and constants: private int cost; public static int TEST_PRICE = 10; public static int REPAIR_PRICE = 50; Have a default constructor that will set...
no need to explain. True or False 1. A constructor is a special Class member method....
no need to explain. True or False 1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList that stores Song objects.. TODO 2: Implement the isEmpty method. Takes no parameters. Returns true if there are no songs on the list, false otherwise. TODO 3: Implement the addSong method. This method takes a Song object as a parameter and does not return a value. It adds the song to the songList. TODO 4: Implement the getSongListAsString method. This method takes no parameters....
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments,...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. Create a Java class named HeadPhone to represent a headphone set. The class contains:  Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.  A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.  A private...
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...