Latest

6/recent/ticker-posts

Write a program to create a class shape with functions to find the area of the shapes. Create derived classes circle, rectangle and trapezoid each having overridden functions area and display. Base class:

 Write a program to create a class shape with functions to find the area of the shapes. Create

derived classes circle, rectangle and trapezoid each having overridden functions area and display.

Base class:


● Member Variables: (protected) length, breadth

● Member Function: (public) virtual function area

CODE:


import java.util.*;

public abstract class Shape {

	public abstract double area();
	public abstract void display();
}

public class Circle extends Shape{
    public void display() {
        System.out.println(area());
    }
    public Circle(int radius) {
        this.radius = radius;
    }
    protected int radius;
    public double area() {
        return (3.14*radius*radius);
    }
}

public class Rectangle extends Shape {
	   
    public void display() {
        System.out.println(area());        
    }

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
    protected double length;
    protected double width;
    
    public double area() {
        return length*width;
    }

}

public class Trapezoid extends Shape{
    public void display() {
        System.out.println(area());
    }
    public Trapezoid(double lenght, double breadth, double height) {
        this.lenght = lenght;
        this.breadth = breadth;
        this.height = height;
    }
    protected double lenght;
    protected double breadth;
    protected double height;
   
    public double area() {
        return ((lenght+breadth)/2)*height;
    }
}
public class Main2 {
	 public static void main (String[] args) {
	
	        Rectangle rectangle = new Rectangle(4,4);
	        rectangle.display();
	        Circle circle= new Circle(6);
	        circle.display();
	        Trapezoid trapezoid=new Trapezoid(5,7,4);
	        trapezoid.display();
	        System.out.println();
	    }
}



Post a Comment

0 Comments