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

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
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...
4. (a) Suppose that τσ=(1 5 2 3)(4) and στ=(1 2 4 5)(3) in S5. If...
4. (a) Suppose that τσ=(1 5 2 3)(4) and στ=(1 2 4 5)(3) in S5. If σ1 = 2, find σ and τ. (b) In Sn, show that σ = τ if and only if σ(τ)^(−1) = ε. ε is the identity permutation. Must be written as a proof. (c) Let σ=(1 2 3) and τ=(1 2) in S3. Show that S3={ε,σ,σ^2,τ,τσ,τ(σ)^2} and that σ^3=ε=τ^2 and στ=τ(σ)^2, then fill out the multiplication table for S3.
coffee tea juice 3 4 5 5 4 3 4 4 4 5 1 2 4...
coffee tea juice 3 4 5 5 4 3 4 4 4 5 1 2 4 2 2 Do a One-way ANOVA by hand (at least once in your life!) …Is there a difference in attention for those who drink coffee, tea, or juice during an 8 a.m. class? Utilize the five steps of hypothesis testing to analyze the following data (p<.01). Attention Ratings (1=no attention- 5=full attention)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT