Video 52(ch10)Ps10

Yash
0

 



Program:-


//Q1.Create a class Circle and use inheritance to create another class Cylinder from it.
//Q2.Create a class Rectangle and use inheritance to create another class cuboid. try to keep it as close to real
// world scenario as possible.
//Q3.Create methods for area and volume in Q1.
//Q4.Create method for area and volume in Q2. also create getter and setter.
//Q5.what is the area of constructor execution for the following inheritance hierarchy.
/*
Base
|
Derived1
|
Derived2

Derived2 obj = new Derived();
Which Constructors will be executed and in what order.
*/

//Q1,Q3
class Circle1{
    public int radius;

    Circle1(int r){
        this.radius = r;
        System.out.println("I am a constructor form Circle1");
    }

    public double area(){
        return Math.PI*this.radius*this.radius;
    }
}

class Cylinder1 extends Circle1{
    int height;

    Cylinder1(int r, int h){
        super(r);
        this.height = h;
        System.out.println("I am a constructor from Cylinder1");
    }
    public double area(){
        return 2*Math.PI*this.radius*(this.radius=this.height);
    }

    public double volume(){
        return Math.PI*this.radius*this.radius*this.height;
    }
}


//Q2.Q4.
class Rectangle01{
    public int length;
    public int width;

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    Rectangle01(){
        System.out.println("I am a constructor from Rectangle01");
    }
    public double area(){
        return this.length*this.width;
    }
}

class Cuboid extends Rectangle01{
    public int length;
    public int width;
    public int height;

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

   public double SurfaceArea(){
        return 2*this.length*this.width + 2*this.width*this.height + 2*this.length*this.height;
    }

    public double Volume(){
        return this.length* this.width* this.height;
    }

    Cuboid(){
        System.out.println("I am a constructor form Cuboid");
    }
}

public class PracticeSet10V53 {
    public static void main(String[] args) {
        //Q1,Q3.
        Cylinder1 obj = new Cylinder1(12,4);
        System.out.println("Area of a circle is : "+obj.area());
        System.out.println("Area of a cylinder is :"+obj.area());
        System.out.println("volume of a cylinder is : "+obj.volume());
        System.out.println();

        //Q2,Q4.
        Cuboid obj1 = new Cuboid();
        obj1.setLength(4);
        obj1.setWidth(6);
        obj1.setHeight(3);
        System.out.println("The area of cuboid is : "+ obj1.SurfaceArea());
        System.out.println("The volume of cuboid is : "+ obj1.Volume());
    }
}

Post a Comment

0 Comments
Post a Comment (0)
To Top