this is JAVA. Every cylinder has a base and height, wherein the base is a circle
ID: 3818725 • Letter: T
Question
this is JAVA. Every cylinder has a base and height, wherein the base is a circle. Design a class, cylinderType, that can capture the properties of a cylinder and perform the usual operations on the cylinder. Derive this class from the class circleType design. Some of the operations that can be performed on a cylinder are as follows: calculate and print the volume, calculate and print the surface area, set the height, set the radius of the base, and set the center of the base. Also, write a program to test various operations on a cylinder.
Explanation / Answer
1. CircleType.java
public class CircleType {
double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
2. CylinderType.java
public class CylinderType extends CircleType {
double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double volume(){
return (3.14)*(this.radius * this.radius)*(this.height);
}
public double surfaceArea(){
double> double two = 2*3.14*this.radius*this.radius;
return one+two;
}
}
3. Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
CylinderType cylinder = new CylinderType();
Scanner sc = new Scanner(System.in);
System.out.println("Enter Radius: ");
double r = Double.valueOf(sc.nextLine());
System.out.println("Enter Height: ");
double h = Double.valueOf(sc.nextLine());
cylinder.setRadius(r);
cylinder.setHeight(h);
System.out.println(cylinder.volume());
System.out.println(cylinder.surfaceArea());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.