175k views
3 votes
Implement a class Balloon that models a spherical balloon that is being filled with air. The constructor constructs an empty balloon. Supply these methods:

void addAir(double amount) adds the given amount of air.
double getVolume() gets the current volume.
double getSurfaceArea() gets the current surface area.
double getRadius() gets the current radius.

Supply a BalloonTester class that constructs a balloon, adds 100cm^3 of air, tests the three accessor methods, adds another 100cm3 of air, and tests the accessor methods again.

1 Answer

1 vote

Answer:

Here is the Balloon class:

public class Balloon { //class name

private double volume; //private member variable of type double of class Balloon to store the volume

public Balloon() { //constructor of Balloon

volume = 0; } //constructs an empty balloon by setting value of volume to 0 initially

void addAir(double amount) { //method addAir that takes double type variable amount as parameter and adds given amount of air

volume = volume + amount; } //adds amount to volume

double getVolume() { //accessor method to get volume

return volume; } //returns the current volume

double getSurfaceArea() { //accessor method to get current surface area

return volume * 3 / this.getRadius(); } //computes and returns the surface area

double getRadius() { //accessor method to get the current radius

return Math.pow(volume / ((4 * Math.PI) / 3), 1.0/3); } } //formula to compute the radius

Step-by-step explanation:

Here is the BalloonTester class

public class BalloonTester { //class name

public static void main(String[] args) { //start of main method

Balloon balloon = new Balloon(); //creates an object of Balloon class

balloon.addAir(100); //calls addAir method using object balloon and passes value of 100 to it

System.out.println("Volume "+balloon.getVolume()); //displays the value of volume by getting value of volume by calling getVolume method using the balloon object

System.out.println("Surface area "+balloon.getSurfaceArea()); //displays the surface area by calling getSurfaceArea method using the balloon object

System.out.println("Radius "+balloon.getRadius()); //displays the value of radius by calling getRadius method using the balloon object

balloon.addAir(100); //adds another 100 cm3 of air using addAir method

System.out.println("Volume "+balloon.getVolume()); //displays the value of volume by getting value of volume by calling getVolume method using the balloon object

System.out.println("Surface area "+balloon.getSurfaceArea()); //displays the surface area by calling getSurfaceArea method using the balloon object

System.out.println("Radius "+balloon.getRadius()); } } //displays the value of radius by calling getRadius method using the balloon object

//The screenshot of the output is attached.

Implement a class Balloon that models a spherical balloon that is being filled with-example-1
User Hrant Nurijanyan
by
4.3k points