Question

An abstract class doesn't have a constructor (because you cannot make an object of the abstract...

An abstract class doesn't have a constructor (because you cannot make an object of the abstract class). It should have at least one method, which then has to be overridden in all derived classes. Here is an example:

  1.   abstract void run();  
  2. class Honda4 extends Bike{  
  3. public static void main(String args[]){  
  4. obj.run();  
  5. }

You are to create an abstract class called Shape, which has an abstract method called computeArea().

Derive a Circle class from Shape. (Circle is similar to your previous Circle class, but alter the name of the area method to compute Area().

Derive a Rectangle class from Shape, changing the name of its area method to computeArea()

Derive a Diamond class from Shape. This is a new class will have attributes length and width. You will need constructors, getters and setters, and a computeArea() method, and a toString method.

In main, create an ArrayList of Shapes. Create two circles, 2 rectangles, and 2 diamonds and add them to the ArrayList.

Loop through your ArrayList outputting the area of each shape.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Please find the code below, I am also enclosing the screenshot for your reference, Here the main catch is that, as ll the classing extending the abstract class Shapes. Hence, you can create the objects of Circle, Rectangle, Diamond and assign the reference to Shapes. Therefore, based on the reference, the respective computeArea() method gets called. Please refer through the comments for understanding.

Shapes.java

abstract class Shape { // abstract class

    abstract void computeArea();
}

Rectangle.java

public class Rectangle extends Shape{ // This ckass is extending an abstract class Shapes
    @Override
    void computeArea() {

        //display the area of a rectangle
        System.out.println("The area of rectangle : ");
    }
}

Circle.java

public class Circle extends Shape { // This ckass is extending an abstract class Shapes

    @Override
    void computeArea() {
        // Compute area of a circle
        System.out.println("The area of circle : ");

    }
}

Diamond.java

import java.util.ArrayList;

public class Diamond extends Shape { // This ckass is extending an abstract class Shapes

    // Attributes as mentioned in the assignment
    int length;
    int width;

    // tostring method
    // You can modify in the way you want
    @Override
    public String toString() {
        return "Diamond{" +
                "length=" + length +
                ", width=" + width +
                '}';
    }


    // getters and setters
    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }


    // compute area methods
    @Override
    void computeArea() {

        System.out.println("The area of diamond : ");
    }

    public static void main(String [] args){

        // Create an arraylist
        ArrayList<Shape> shapes = new ArrayList<>();

        // adding 2 circles to the list
        shapes.add(new Circle());
        shapes.add(new Circle());

        // adding 2 rectangles to the list
        shapes.add(new Rectangle());
        shapes.add(new Rectangle());

        // adding 2 diamonds to the list
        shapes.add(new Diamond());
        shapes.add(new Diamond());

        // traversing a list and diaplaying the area
        for (Shape shape : shapes){
            shape.computeArea();// calling computeArea methos, wwith reference to the items present in the list
        }
    }
}

Thank you,

Good Luck!!!

Add a comment
Know the answer?
Add Answer to:
An abstract class doesn't have a constructor (because you cannot make an object of the abstract...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT