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

Create an object class named circle that contains data fields for the radius and

ID: 3571395 • Letter: C

Question

Create an object class named circle that contains data fields for the radius and PI and constructor that reads radius. Include a toString method to show this circle object. Next create a subclass Cylinder, which contains additional field height that hold the height of the Cylinder. Override toString method in the subclass. Then write a CylinderDemo class create an array of Cylinder objects of random radius and height (random numbers between 10 and 100). The length of array should be entered by the user. Then use a loop structure to display all Cylinder objects in the array.

Note 1: for creating random numbers between 10 and 100 you can use the following statement:

Math.random()*90 + 10;

Create an object class named circle that contains data fields for the radius and PI and constructor that reads radius. Include a toString method to show this circle object. Next create a subclass Cylinder, which contains additional field height that hold the height of the Cylinder. Override toString method in the subclass. Then write a CylinderDemo class create an array of Cylinder objects of random radius and height (random numbers between 10 and 100). The length of array should be entered by the user. Then use a loop structure to display all Cylinder objects in the array.

Note 1: for creating random numbers between 10 and 100 you can use the following statement:

Math.random()*90 + 10;

Explanation / Answer

1. Circle.java

public class Circle {
   private final double PI = 3.14159;
   private double radius;

   public Circle(double r) {
       radius = r;
   }

   public double getRadius() {
       return radius;
   }

   public void setRadius(double radius) {
       this.radius = radius;
   }

   public double getPI() {
       return PI;
   }

   @Override
   public String toString() {
       return "Circle [PI=" + PI + ", radius=" + radius + "]";
   }

}

-----------------------------

2. Cylinder.java

public class Cylinder extends Circle {

   public Cylinder(double r, double height) {
       super(r);
       this.height = height;
   }

   private double height;

   public double getHeight() {
       return height;
   }

   public void setHeight(double height) {
       this.height = height;
   }

   @Override
   public String toString() {
       return "Cylinder [height=" + height + ", "
               + super.toString() + "]";
   }

}

-------------------------------------------------

3. CylinderDemo.java

import java.util.Scanner;

public class CylinderDemo {

   public static void main(String[] args) {
       System.out.println("Please enter the size of the array: ");
       Scanner s = new Scanner(System.in);
       int size = s.nextInt();
       Cylinder c = null;
       Cylinder[] cylinderArray = new Cylinder[size];
       for (int i = 0; i < size; i++) {
           c = new Cylinder(Math.random() * 90 + 10, Math.random() * 90 + 10);
           cylinderArray[i] = c;
       }
       for (int i = 0; i < size; i++) {
           System.out.println(cylinderArray[i].toString());
       }
   }

}

------------------------------------------------------------

output:

Please enter the size of the array:
4
Cylinder [height=32.58483483204751, Circle [PI=3.14159, radius=72.21730400945276]]
Cylinder [height=13.901300591713298, Circle [PI=3.14159, radius=86.5705181825492]]
Cylinder [height=87.29345216049099, Circle [PI=3.14159, radius=28.844575032183858]]
Cylinder [height=65.0760949736825, Circle [PI=3.14159, radius=15.051792765862093]]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote