Question

Create an abstract class called GeometricFigure. Each figure includes a height, a width. a figure type,...

Create an abstract class called GeometricFigure. Each figure includes a height, a width. a figure type, and an area. Include an abstract method to determine the area of the figure. Create two subclasses called Square and Triangle. Create an application that demonstrates creating objects of both subclasses, and store them in an array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
abstract class GeometricFigure {

    protected double height;
    protected double width;
    protected String type;
    protected double area;

    public GeometricFigure(double height, double width, String type) {
        this.height = height;
        this.width = width;
        this.type = type;
    }

    public abstract double getArea();

    @Override
    public String toString() {
        return "height=" + height + ", width=" + width + ", type='" + type + '\'' + ", area=" + getArea();
    }
}

class Square extends GeometricFigure {

    public Square(double height, double width) {
        super(height, width, "Square");
    }

    @Override
    public double getArea() {
        area = height * width;
        return area;
    }
}

class Triangle extends GeometricFigure {

    public Triangle(double height, double base) {
        super(height, base, "Triangle");
    }

    @Override
    public double getArea() {
        area = 0.5 * height * width;
        return area;
    }
}

class GeometricFigureTest {

    public static void main(String[] args) {
        GeometricFigure[] figures = {new Triangle(3, 10), new Square(4, 9), new Triangle(5, 5)};
        for (int i = 0; i < figures.length; i++) {
            System.out.println(figures[i]);
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
Create an abstract class called GeometricFigure. Each figure includes a height, a width. a figure type,...
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
  • I've seen this one done as a console application, however I need some help setting it...

    I've seen this one done as a console application, however I need some help setting it up as a windows form app using micrsoft visual c#. C# project - Create an application named ShapesDemo that creates several objects that descend from an abstract class called GeometricFigure. Each GeometricFigure includes a height, a width, and an area. Provide get and set accessors for each field except area; the area is computed and is read-only. Include an abstract method calledComputeArea() that computes...

  • C# VISUAL STUDIO CONSOLE App(.NET Framework) Create an application named ShapesDemo that creates several objects that...

    C# VISUAL STUDIO CONSOLE App(.NET Framework) Create an application named ShapesDemo that creates several objects that descend from an abstract class called GeometricFigure. Each GeometricFigure includes a height, a width, and an area. Provide get and set accessors for each field except area; the area is computed and is read-only. Include a method called ComputeArea() that computes the area of the GeometricFigure. Next you will create two additional classes derived from the GeometricFigure class Rectangle and Square.

  •           TASK 1: Create class diagram for abstract class Shape. TASK 2: Create class diagrams for abstract subclasses...

              TASK 1: Create class diagram for abstract class Shape. TASK 2: Create class diagrams for abstract subclasses TwoDimensionalShape and ThreeDimensionalShape which extend abstract superclassShape. TwoDimensionalShape contains method getArea, and ThreeDimensionalShape contains methods getArea and getVolume. TASK 3: Create class diagrams for concrete classes Circle, Square, Triangle, Sphere, Cube and Tetrahedron.

  • In Java Create an interface called Amount that includes a method called setPrice (). Create an a...

    In Java Create an interface called Amount that includes a method called setPrice (). Create an abstract class named Book that inherits from the interface Amount. Include a String field for the book’s title and a double field for the book’s Price . Within the class, include a constructor that requires the book title and add two getter methods — one that returns the title and one that returns the price. Include an abstract method named setPrice (). Create a...

  • 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...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • Create a class called Retangle.java that contains two double-precision instance variables named width and height. The...

    Create a class called Retangle.java that contains two double-precision instance variables named width and height. The class should include all kinds of overloaded constructors. Additionally, there should be two accessor methods, mutator methods, class method named area() that returns the area of a Rectangle object. Inside main(),fully test all methods.

  • 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...

  • This assignment involves writing a program that uses an abstract class and concrete subclasses. An abstract...

    This assignment involves writing a program that uses an abstract class and concrete subclasses. An abstract Vehicle class declares an abstract method named display() that is used to display vehicles. Vehicle has two subclasses: Boat and Car. A Boat has a length attribute, and a draft attribute (a draft is how far below the surface the bottom of the boat is). A Car has a make attribute and a year attribute. Appropriate getter, setter, and constructor methods should be defined...

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