Untitled

 avatar
unknown
java
2 years ago
1.6 kB
0
Indexable
import java.io.*;
abstract class Shape
{
	double r,h;
	Shape(double r, double h)
	{
		this.r = r;
		this.h = h;
	}
	abstract double calcArea();
	abstract double calcVolume();
	
}

class Cone extends Shape{
	Cone(double r, double h)
	{
		super(r,h);
	}
	double calcArea()
	{
		return 3.14*r*(r+Math.sqrt(r*r+h*h));
	}
	double calcVolume()
	{
		return 3.14*r*r*h/3;
	}
}
class Cylinder extends Shape
{
	Cylinder(double r, double h)
	{
		super(r,h);
	}
	double calcArea()
	{
		return 2*3.14*r*(h+r);
	}
	double calcVolume()
	{
		return 3.14*r*r*h;
	}
}
class Calculate_Area_Volume
{
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader( new 
		InputStreamReader(System.in));
		Shape shape = null;
		double r= 0;
		double h = 0;
		int ch;
		do
		{
			System.out.print("\n1.Cone"+"\n2.Cylinder"+"\n3.Exit"+"\nEnter your choice:");
			ch = Integer.parseInt(br.readLine());
			switch(ch)
			{
				case 1:
					System.out.print("enter radius of cone:");
					r= Double.parseDouble(br.readLine());
					System.out.print("enter height of cone:");
					h = Double.parseDouble(br.readLine());
					shape = new Cone(r,h);
					break;
				case 2:
					System.out.print("enter radius of cylinder:");
					r= Double.parseDouble(br.readLine());
					System.out.print("enter height of cylinder:");
					h = Double.parseDouble(br.readLine());
					shape = new Cylinder(r,h);
					break;
				case 3:
					System.exit(0);
				}
				System.out.println("area = " +shape.calcArea()+"\nVolume ="+shape.calcVolume());
			}
			while(ch!=4);
		}
	}
Editor is loading...