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 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....
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,...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without using methods from the Rectangle class, return true if the point is inside the rectangle and false otherwise. 2. Write a second method with the same functionality as Exercise 1. However, use a method from the Rectangle class this time. 3. Normally, the == operator cannot be used to compare two strings for equality. There are 2 main exceptions we talked about. The first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT