Create a class Circle, it should have only one method i.e. area, and then use inheritance

0

Assignment: Module 04.1

Create a class Circle, it should have only one method i.e. area, and then use inheritance to create a different class Cylinder, that extends from class circle. it should have only one method i.e volume.

Create a class Circle, it should have only one method i.e. area, and then use inheritance to create a different class Cylinder, that extends from class circle. it should have only one method i.e volume. 

You need to print area and volume, take radius and height from input. (use Math.PI property for pi value).





class Circle {
    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double area() {
        return Math.PI * Math.pow(radius, 2);
    }
}

class Cylinder extends Circle {
    double height;

    public Cylinder(double radius, double height) {
        super(radius);
        this.height = height;
    }

    public double volume() {
        return area() * height;
    }
}

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter radius:");
        double r = sc.nextDouble();
        System.out.println("Enter height:");
        double h = sc.nextDouble();

        Cylinder c = new Cylinder(r, h);
        System.out.println("Area: " + c.area());
        System.out.println("Volume: " + c.volume());
    }
}   

You can enter the radius and height values, the program will give you the area and volume of cylinder.



👉Java Fundamentals Module 04👈

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
✨ Updates