0% found this document useful (0 votes)
18 views

Shape Java

Java Code for abstraction

Uploaded by

tempmailfail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Shape Java

Java Code for abstraction

Uploaded by

tempmailfail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;

abstract class Shape{


abstract double surfaceArea();
abstract double volume();
}

class Sphere extends Shape{


double r;

Sphere(double r){
this.r = r;
}

@Override
double surfaceArea(){
return 4 * Math.PI * (r*r);
}
@Override
double volume(){
return (4/3)*Math.PI * (r*r*r);
}
}

class Cube extends Shape{


double s;

Cube(double s){
this.s = s;
}

@Override
double surfaceArea(){
return 6 * (s * s);
}
@Override
double volume(){
return s*s*s;
}
}

class rectangularPrism extends Shape{


double l;
double w;
double h;

rectangularPrism(double l, double w, double h){


this.l = l;
this.w = w;
this.h = h;
}

@Override
double surfaceArea(){
return 2*(l*w+l*h+w*h);
}
@Override
double volume(){
return l*w*h;
}
}

class rightCircularCylinder extends Shape{


double r;
double h;

rightCircularCylinder(double r,double h){


this.r = r;
this.h = h;
}

@Override
double surfaceArea(){
return 2*(Math.PI*(r*r) + Math.PI*r*h);
}
@Override
double volume(){
return Math.PI*(r*r)*h;
}
}

public class Main{


public static void main(String[] arg){
Scanner sc = new Scanner(System.in);

//sphere
System.out.println("Enter the values for Sphere : ");
System.out.println("Enter the Radius: ");
double sphereR = sc.nextDouble();
Shape Sphere = new Sphere(sphereR);
System.out.printf("Sphere Surface Area = %.2f\n",Sphere.surfaceArea());
System.out.printf("Sphere Volume = %.2f\n",Sphere.volume());
System.out.println();

//cube
System.out.println("Enter the values for Cube : ");
System.out.println("Enter the Side: ");
double cubeS = sc.nextDouble();
Shape Cube = new Cube(cubeS);
System.out.printf("Cube Surface Area = %.2f\n",Cube.surfaceArea());
System.out.printf("Cube Volume = %.2f\n",Cube.volume());
System.out.println();

//Rectangular Prism
System.out.println("Enter the values for Rectangular Prism : ");
System.out.println("Enter the length: ");
double rectangularPrismL = sc.nextDouble();
System.out.println("Enter the width: ");
double rectangularPrismW = sc.nextDouble();
System.out.println("Enter the height: ");
double rectangularPrismH = sc.nextDouble();

Shape RectangularPrism = new


rectangularPrism(rectangularPrismL,rectangularPrismW,rectangularPrismH);
System.out.printf("Rectangular Prism Surface Area =
%.2f\n",RectangularPrism.surfaceArea());
System.out.printf("Rectangular Prism Volume =
%.2f\n",RectangularPrism.volume());
System.out.println();

//Right Circular Cylinder


System.out.println("Enter the values for Right Circular Cylinder : ");
System.out.println("Enter the Radius: ");
System.out.println("Enter the Height: ");
double rightCircularCylinderR = sc.nextDouble();
double rightCircularCylinderH = sc.nextDouble();
Shape RightCircularCylinder = new
rightCircularCylinder(rightCircularCylinderR,rightCircularCylinderH);
System.out.printf("Right Circular Cylinder Surface Area :
%.2f\n",RightCircularCylinder.surfaceArea());
System.out.printf("Right Circular Cylinder Volume :
%.2f\n",RightCircularCylinder.surfaceArea());
System.out.println();

}
}

You might also like