Question

in java Implement a class Balloon. A balloon starts out with radius 0. Supply a method...

in java

Implement a class Balloon. A balloon starts out with radius 0. Supply a method

public void inflate(double amount)
that increases the radius by the given amount. Supply a method

public double getVolume()
that returns the current volume of the balloon; volume of a balloon is represented as:

V = (4/3) * PI * r^3

Use Math.PI for the value of π. To compute the cube of a value r, you can use Math.pow (https://www.tutorialspoint.com/java/lang/math_pow.htm) or r*r*r

Write a tester program that inflates the balloon to 10, print the volume, then inflate it to 15 and print the volume.

Radius: 10, volume: 4188.79

Radius: 15, volume: 14137.17

Homework Answers

Answer #1
//Balloon.java
public class Balloon {
    private double radius;

    public Balloon() {
        radius = 0;
    }

    public void inflate(double amount){
        radius += amount;
    }

    public double getVolume(){
        return (4.0/3)*Math.PI*Math.pow(radius,3);
    }

    public static void main(String[] args) {
        Balloon balloon1 = new Balloon();
        balloon1.inflate(10);
        System.out.printf("Volume = %.2f\n",balloon1.getVolume());

        Balloon balloon2 = new Balloon();
        balloon2.inflate(15);
        System.out.printf("Volume = %.2f\n",balloon2.getVolume());
    }
}

Volume = 4188.79
Volume = 14137.17

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
java Consider the following class definition: public class Circle { private double radius; public Circle (double...
java Consider the following class definition: public class Circle { private double radius; public Circle (double r) { radius = r ; } public double getArea(){ return Math.PI * radius * radius; } public double getRadius(){ return radius; } } a) Write a toString method for this class. The method should return a string containing the radius and area of the circle; For example “The area of a circle with radius 2.0 is 12.1.” b) Writeaequalsmethodforthisclass.ThemethodshouldacceptaCircleobjectasan argument. It should return...
in java Write a class Battery that models a rechargeable battery. A battery has a constructor...
in java Write a class Battery that models a rechargeable battery. A battery has a constructor public Battery(double capacity) where capacity is a value measured in milliampere hours. A typical AA battery has a capacity of 2000 to 3000 mAh. The method public void drain(double amount) drains the capacity of the battery by the given amount. The method public void charge() charges the battery to its original capacity. The method public double getRemainingCapacity() gets the remaining capacity of the battery....
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...