Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

( Java ) Object Oriented Programming 1. Design and implement the class Circle th

ID: 3600171 • Letter: #

Question

( Java ) Object Oriented Programming

1.     Design and implement the class Circle that contains the double private data field radius and the following member methods:

2.     getRadius that returns the radius of Circle object.

3.     setRadius that sets the radius of Circle object.

4.     area that calculates and return the area of a Circle object  (area = *r²).

5.     toString that returns the radius of a Circle object as a string.

6.     Two constructors; the first one without arguments. The second constructor with one parameter r. It initializes the radius to r.

1.     Design and implement the class Cylinder that inherits the class Circle and contains one double private data field height and the following member methods:

2.     getHeight that returns the height of Cylinder object.

3.     setHeight that sets the height of Cylinder object.

4.     area that calculates and return the area of a Cylinder object

(area = 2**r²+2**r*h).

1.     volume that calculates and return the volume of a Cylinder object

(volume = *r²*h).

1.     toString that returns the radius and height of a Cylinder object as string.

2.     Two constructors; the first one without arguments. The second constructor with two parameters r and h. It initializes the radius to r and the heightto h.

1.     Using sentinel controlled loop, write Test_Driver program to read radius and height of 20 Cylinder objects and store them in an array. Your program must be able to output, for Cylinder object, its radius, height, area and volume.

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

_____________Could you rate me well.Plz .Thank You