Question

In: Computer Science

(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...

(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes   
2) Triangle
3) Rectangle   
4) Circle
5) TestAllShapes (create 1 object of each type and print the Area for each of them.)

Solutions

Expert Solution

Code:
abstract class Shapes{      //creating an abstract class of shape
    abstract public double getArea();   //creating a abstract method
}

class Rectangle extends Shapes{     //Rectangle class inherites shapes Class

    private double length=0;    //defining length variable
    private double width=0;     //defining length variable
  
    public Rectangle(double l,double w){    //constructor with legth and width as parameters
        this.length = l;    //assignig the values to the variables
        this.width = w;
    }
  
    public double getArea(){        //implementing the abstract function of parent class
        double area = length * width;   // calculating area of rectangle
        return area;    //returning area
    }
}

class Circle extends Shapes{    //Circle extends the Shapes class
  
    private double radius=0;    //defining length variable
    public Circle(double r){    //constructor with radius parameter
        this.radius = r;        //assigning radius
    }
  
    public double getArea(){         //implementing the abstract function of parent class
        double area = Math.PI * radius * radius;       // calculating area of Circle
        return area;    //returning area
    }
}

class Triangle extends Shapes{
  
    private double base = 0;    //defining base variable
    private double height = 0; //defining height variable
  
    public Triangle(double b,double h){     //constructor with legth and width as parameters
        this.base = b;      //assigning base of the triangle
        this.height = h;    //assigning height of the triangle
    }
    public double getArea(){        //area calculating fuction
        double area = 0.5 * base * height;
        return area;        //returning the area
    }
}

public class TestClass{     //test class to print the areas of the objects
    public static void main (String[] args) {
      
        Rectangle rect1 = new Rectangle(2,3);   //creating a rectanle object with legth=2 and width=3
        System.out.println("Area of the Rectangle is :" + " "+rect1.getArea()); //printing the area of the rectangle
      
        Triangle tr1 = new Triangle(2,6);       //creating a Triangle object with base=2 and height=6
        System.out.println("Area of the Triangle is :" + " "+tr1.getArea());     //printing the area of the Triangle
      
        Circle c1 = new Circle(4);          //creating a Circle object with radius 4
        System.out.println("Area of the Circle is :"+ " " + c1.getArea());      //printing the area of the Circle
    }
}


Related Solutions

Using Java Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes: Consider a...
Using Java Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes: Consider a graphics system that has classes for various figures — say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and either a center point or upper-left corner point, while a box and circle might have only a center point (or upper-right corner point) and an edge length or radius, respectively. In a well-designed system, these would...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the...
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep ( i.e., as many levels ) as possible. Specify the instance variables and methods for each class. The private instances variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects...
Creat a theater booking system in java language using : 1.OOP objects -Classes 2.encapsulation 3.polymorphism 4.inheritance...
Creat a theater booking system in java language using : 1.OOP objects -Classes 2.encapsulation 3.polymorphism 4.inheritance 5.abstract class
write a java code to calculate 1+2-3+4-5 …-99+100
write a java code to calculate 1+2-3+4-5 …-99+100
Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). 1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input a. Invalid input should throw a message for the user. Example: Invalid input, please try again 2. Each options should ask users for relevant data....
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can be several ways to represent the shapes. For example, a shape can be represented by an array of side lengths counted in the clock-wise order. For example, {2, 4, 2, 4} for a rectangle of width 2 and height 4, and {4, 4, 4, 4} for a square. You need to design your representation. Each polygon should have a function area() that returns its...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length and width, a Circle constructed by a radius and a Square constructed by a side length. Both classes should have the methods that compute: - The area - The diagonal - The perimeter Use as much abstraction as you can. At the end of the file, use those classes to calculate the perimeter of a circle with radius the half of the diagonal of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT