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...
Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class...
Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class ( super class) 2,3. Create two sub classes ( car, bus , truck train or any) for vehicle class 4 Create a subclass (sportscar or schoolbus or Goodstrain or any) for car or bus You need to use the following atleast for one class. Use a protected variable Add constructor for superclass and subclass Override a method which displays a description about the vehicle...
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...
Language: JavaScript Create a public class called Inverter with a single class method named invert. invert...
Language: JavaScript Create a public class called Inverter with a single class method named invert. invert should accept a Map<String, Integer> and return a Map<Integer, String> with all of the key-value pairs swapped. You can assert that the passed map is not null. Because values are not necessarily unique in a map, normally you would need a way to determine what the right approach is when two keys map to the same value. However, the maps passed to your function...
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...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
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...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
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...