intro to java 11) Write the definition for a child class of Circle named Cylinde
ID: 3736269 • Letter: I
Question
intro to java
11) Write the definition for a child class of Circle named Cylinder. The cylinder should have a double attribute named height. The constructor of the child class should take parameters to initialize the attributes. The class Cylinder should also include getter and setter method as well as an equals method that returns true if two cylinder objects have same dimensions and false otherwise. Remember to override method where needed. public class Circle protected double radius; public Circle(double r) f radius=r; public void setRadius(double r) radius r; public double getRadius() f return radius; public double area() return Math.PI radius radius; public String toString) f return "Circle:Radius- " + radius;Explanation / Answer
Circle.java
public class Circle {
// Declaring instance variables
private double radius;
public Circle() {
}
// parameterized constructor
public Circle(double radius) {
this.radius = radius;
}
// Setter and getter
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
// calculating the area of the circle
public double calArea() {
return Math.PI * getRadius() * getRadius();
}
// calculating the perimeter of the circle
public double calPerimeter() {
return 2 * Math.PI * getRadius();
}
@Override
public String toString() {
return " Radius=" + radius;
}
}
_________________
Cylinder.java
public class Cylinder extends Circle {
//Declaring instance variables
private double height;
public Cylinder() {}
//parameterized constructor
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
//Getter and setter
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//calculating the area of the cylinder
public double calArea() {
return (2 * Math.PI * getRadius()) * (getRadius() + getHeight());
}
//calculating the perimeter of the cylinder
public double calVolume() {
return Math.PI * getRadius() * getRadius() * getHeight();
}
@Override
public String toString() {
return "Height=" + height + super.toString();
}
}
__________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
double r, h;
int count = 0;
//Creating an Array of 20 Cylinder class objects
Cylinder c[] = new Cylinder[20];
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the radius and Height of the Cylinder
System.out.print("Enter the Radius of Cylinder#" + (count + 1) + " (-1 to exit):");
r = sc.nextDouble();
/* This while loop continues to execute
* until the user enters -1
*/
while (r != -1) {
System.out.print("Enter the Height of Cylinder#" + (count + 1) + ":");
h = sc.nextDouble();
c[count] = new Cylinder(r, h);
count++;
System.out.print("Enter the Radius of Cylinder#" + (count + 1) + "(-1 to exit) :");
r = sc.nextDouble();
}
//Displaying the output
for (int i = 0; i < count; i++) {
System.out.println("** Cylinder#" + (i + 1) + " **");
System.out.println(c[i]);
System.out.println("Area :" + c[i].calArea());
System.out.println("Volume :" + c[i].calVolume());
}
}
}
_______________
Output:
Enter the Radius of Cylinder#1 (-1 to exit):5.5
Enter the Height of Cylinder#1:6.0
Enter the Radius of Cylinder#2(-1 to exit) :4.5
Enter the Height of Cylinder#2:5.0
Enter the Radius of Cylinder#3(-1 to exit) :3.5
Enter the Height of Cylinder#3:4.0
Enter the Radius of Cylinder#4(-1 to exit) :-1
** Cylinder#1 **
Height=6.0 Radius=5.5
Area :397.4114706791088
Volume :570.1990666265474
** Cylinder#2 **
Height=5.0 Radius=4.5
Area :268.6061718819273
Volume :318.0862561759665
** Cylinder#3 **
Height=4.0 Radius=3.5
Area :164.93361431346415
Volume :153.93804002589985
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.