Friday 10 March 2017

Java Slip 1 :- Abstract Class Shape TYBSc Computer Science Pune University

Download Solved JAVA Slips for T.Y.BSc Comp Sci 2017 (Pune University) https://goo.gl/y8RlQ8

/* Create an abstract class shape. Derive three classes sphere, cone and cylinder from it. Calculate area
and volume of all (use method overriding)*/

import java.io.*;
abstract class Shape
{
double r,h;
Shape(double r,double h)
{
this.r=r;
T his.h=h;
}
abstract void area();
abstract void volume();
}
class Cyclinder extends Shape
{
Cyclinder(double x,double y)
{
super(x,y);
}
void area()
{
double a;
a=(2*3.14*r*h)+(2*3.14*r*r);
System.out.println("Area of Cyclinder:"+a);
}
void volume()
{
double v;
v=3.14*r*r*h;
System.out.println("Volume of Cyclinder:"+v);
}
}

class Cone extends Shape
{
Cone(double x,double y)
{
super(x,y);
}
void area()
{
double x,a;
x=(h*h)+(r*r);
a=3.14*r*(r+Math.sqrt(x));
System.out.println("Area of Cone:"+a);
}
void volume()
{
double v;
v=3.14*r*r*(h/3);
System.out.println("Volume of Cone:"+v);
}
}
class Box extends Shape
{
double b;
Box(double x,double y,double z)
{
super(x,y);
b=z;
}
void area()
{
double x1,y1,z1,a;
x1=h*b;
y1=h*r;
z1=b*r;
a=(2*x1)+(2*y1)+(2*z1);
System.out.println("Area of Box:"+a);
}
void volume()
{
double v;
v=h*b*r;
System.out.println("Volume of Box:"+v);
}
}
class Sphere1 extends Shape
{
Sphere1(double x,double y)
{
super(x,y);
}
void area()
{
double a;
a=4*3.14*r*r;
System.out.println("Area of Sphere:"+a);
}
void volume()
{
double v;
v=(4/3)*3.14*r*r*r;
System.out.println("Volume of Sphere:"+v);
}
public static void main(String g[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter radius:");
double p=Double.parseDouble(br.readLine());
System.out.println("Enter height:");
double q=Double.parseDouble(br.readLine());
System.out.println("Enter breadth:");
double r=Double.parseDouble(br.readLine());
System.out.println("Enter length:");
double w=Double.parseDouble(br.readLine());
Cyclinder c1=new Cyclinder(p,q);
c1.area();
c1.volume();
Cone c2=new Cone(p,q);
c2.area();
c2.volume();
Box b1=new Box(w,r,q);
b1.area();
b1.volume();
Sphere1 s1=new Sphere1(p,0);
s1.area();
s1.volume();
}
}
Download Solved JAVA Slips for T.Y.BSc Comp Sci 2017 (Pune University) https://goo.gl/y8RlQ8

No comments:

Post a Comment