Question

Purpose: To write an Object-Oriented application using abstraction, inheritance, encapsulation, and polymorphism to handle an inheritance...

Purpose: To write an Object-Oriented application using abstraction, inheritance, encapsulation, and polymorphism to handle an inheritance structure of various shapes.

Details:

Implement the following Shape hierarchy: a box around each shape name and arrows pointing to the shape from each (Circle, Square, Triangle)

Shape

Circle Square Triangle

  

Use the following as a guide:

  • The Shape class should be abstract and contain two abstract methods:
    • getArea – Which calculates the area of the shape and returns that value as a double.
    • getPerimeter – Which calculates the perimeter of the shape and return that value as a double.

And one concrete method:

    • getName – that returns the shape’s name as a String.
  • The Circle class’s constructor should take a single parameter of type double representing its diameter. No error checking is required.
  • The Square class’s constructor should take a single parameter of type double representing the length of its sides. No error checking is required.
  • The Triangle class’s constructor should take a single parameter of type double representing the length of its sides. No error checking is required.
  • No set and get methods are needed. Do not include them in the any of the classes.

Create a second class called TestShapes that contains the main method. This test class does not need to ask users for input. The class should do the following:

  1. Create an array of size three, that uses Shape references. Fill the array with one of each concrete class in the hierarchy.
  2. Iterate through the array of shapes and doing the following:
    1. Display the shape’s name.
    2. Display the shape’s area.
    3. Display the shape’s perimeter.

*******Ensure that your program is properly formatted and it follows all Java naming conventions.

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

Thanks for the question.


Below is the code you will be needing Let me know if you have any doubts or if you need anything to change.


Also, designed the UML diagram.

If you are satisfied with the solution, please rate the answer. Thanks

===========================================================================

public abstract class Shape {


    public abstract double getArea();

    public abstract double getPerimeter();

    public String getName() {
        return "Abtract Shape";
    }

}

====================================================================

public class Circle extends Shape {

    private double diameter;

    public Circle(double diameter) {
        this.diameter = diameter;
    }

    @Override
    public double getArea() {
        return Math.PI * (diameter * diameter) / 4;
    }

    @Override
    public double getPerimeter() {
        return Math.PI * diameter;
    }

    @Override
    public String getName() {
        return "Circle";
    }


}

====================================================================

public class Square extends Shape {

    private double length;

    public Square(double length) {
        this.length = length;
    }

    @Override
    public double getArea() {
        return length * length;
    }

    @Override
    public double getPerimeter() {
        return 4 * length;
    }

    @Override
    public String getName() {
        return "Square";
    }
}

====================================================================

public class Triangle extends Shape {

    private double side1;
    private double side2;
    private double side3;

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    @Override
    public double getArea() {

        double s = (getPerimeter()) / 2;
        return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

    }


    @Override
    public double getPerimeter() {
        return side1 + side2 + side3;
    }

    @Override
    public String getName() {
        return "Triangle";
    }
}

====================================================================

public class TestShapes {


    public static void main(String[] args) {

        //Create an array of size three, that uses Shape references
        Shape shapes[] = new Shape[3];

        shapes[0] = new Circle(7.0);
        shapes[1] = new Square(5.5);
        shapes[2] = new Triangle(5, 6, 7);

        //Iterate through the array of shapes and doing the following:
        for (int i = 0; i < shapes.length; i++) {
            System.out.println("Shapes Name: " + shapes[i].getName());
            System.out.println("Shapes Area: " + shapes[i].getArea());
            System.out.println("Shapes Perimeter: " + shapes[i].getPerimeter());
            System.out.println();
        }
    }
}

====================================================================

Add a comment
Know the answer?
Add Answer to:
Purpose: To write an Object-Oriented application using abstraction, inheritance, encapsulation, and polymorphism to handle an inheritance...
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
  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

  • Please use C++ and Please do all file separate separately. And also I need output too....

    Please use C++ and Please do all file separate separately. And also I need output too. thanks. Please do it complete work. ShapeNodePoly.cpp Avaliable from: Wednesday, July 27, 2016, 10:20 AM Requested files: Point.h, Point.cpp, Shape.h, Shape.cpp, Polygon.h, Polygon.cpp, Ellipse.h, Ellipse.cpp, ShapeNodePoly.cpp, ShapeNodePoly_test.cpp (Download) Type of work: Individual work In this assignment, you will create a class that can behave as any of the shapes listed below. You will use object inheritance to enable all shapes to be managed using...

  • Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve...

    Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...

  • Using the IST Linux system create the following Java command line inheritance application Lab4. Create the...

    Using the IST Linux system create the following Java command line inheritance application Lab4. Create the following project Java files: Point.java, Shape.java, Circle.java, Triangle.java, Rectangle.java, and Lab4.java Point.java will contain two coordinates x and y. This will be reused to create point objects to draw all the shapes. Shape.java will be the parent class and will contain a Point type. Circle.java, Triangle.java, Rectangle.java will all be children of Shapes.java class Each class (Circle, Triangle, Rectangle) will contain the private class...

  • Create a Bitbucket private repository. Name it “CUS1156_Lab2”. Clone it in your local java Eclipse workspace....

    Create a Bitbucket private repository. Name it “CUS1156_Lab2”. Clone it in your local java Eclipse workspace. You will add the java files after you have completed them in Part 1 and Part 2 to this location. Part 1: Abstract classes From class, an abstract class represents some generic concept. Subclasses then provide their own specific implementations of any abstract methods of the abstract class. 1. Implement an abstract class called Shape it was discussed in the lecture. Include an abstract...

  • Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...

    Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...

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