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

1-Write a test program that creates an array of three objects; first two objects

ID: 3702840 • Letter: 1

Question

1-Write a test program that creates an array of three objects; first two objects are created via using the default constructor while the third via using the second constructor.

2. Attributes for the first two objects should be entered via prompting the user to enter the appropriate attributes. The data should then read from the keyboard

3. Display both the area and the perimeter for each of the objects.

The code:

public class Cylinder {

   double height;

   double radious;

  

   Cylinder(){}

  

  

   public Cylinder(double height, double radious) {

          

           this.height = height;

           this.radious = radious;

          

       }

  

   public double getSurfaceArea() {

      

       return height*radious;

   }

  

   public double getVolume(){

      

       return 2*(radious+height);

      

      

   }

  

   public String toString(){

          

           return "The area of the rectangle : " +getSurfaceArea()

                   +" The perimeter of the rectangle :" +getVolume();

       }

  

  

   public double getHeight() {

           return height;

       }

       public void setHeight(double heigh) {

           this.height = height;

       }

       public double getRadious() {

           return radious;

       }

       public void setRadious(double radious) {

           this.radious = radious;

       }

      

      

}

Explanation / Answer

CylinderTest.java

import java.util.Scanner;

public class CylinderTest {

public static void main(String[] args) {

Cylinder obj[] = new Cylinder[3];

Scanner scan = new Scanner(System.in);

System.out.println("Enter the first cylinder height: ");

double height1 = scan.nextDouble();

System.out.println("Enter the first cylinder radius: ");

double radius1 = scan.nextDouble();

System.out.println("Enter the second cylinder height: ");

double height2 = scan.nextDouble();

System.out.println("Enter the second cylinder radius: ");

double radius2 = scan.nextDouble();

obj[0] = new Cylinder();

obj[0].setHeight(height1);

obj[0].setRadious(radius1);

obj[1] = new Cylinder();

obj[1].setHeight(height2);

obj[1].setRadious(radius2);

obj[2] = new Cylinder(5,4);

System.out.println("Area1: "+obj[0].getSurfaceArea());

System.out.println("Perimeter1: "+obj[0].getVolume());

System.out.println("Area2: "+obj[1].getSurfaceArea());

System.out.println("Perimeter2: "+obj[1].getVolume());

System.out.println("Area3: "+obj[2].getSurfaceArea());

System.out.println("Perimeter3: "+obj[2].getVolume());

}

}

Cylinder.java

public class Cylinder {

double height;

double radious;

  

Cylinder(){}

  

  

public Cylinder(double height, double radious) {

  

this.height = height;

this.radious = radious;

  

}

  

public double getSurfaceArea() {

  

return height*radious;

}

  

public double getVolume(){

  

return 2*(radious+height);

  

  

}

  

public String toString(){

  

return "The area of the rectangle : " +getSurfaceArea()

+" The perimeter of the rectangle :" +getVolume();

}

  

  

public double getHeight() {

return height;

}

public void setHeight(double heigh) {

this.height = heigh;

}

public double getRadious() {

return radious;

}

public void setRadious(double radious) {

this.radious = radious;

}

  

  

}

Output:

Enter the first cylinder height:
2
Enter the first cylinder radius:
3
Enter the second cylinder height:
4
Enter the second cylinder radius:
5
Area1: 6.0
Perimeter1: 10.0
Area2: 20.0
Perimeter2: 18.0
Area3: 20.0
Perimeter3: 18.0