Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The Balloon class Design, code and test a class named Balloon. The class contains: Constants -a constant named PI, set to 3.1415926 Instance variables -an
The Balloon class
Design, code and test a class named Balloon. The class contains:
Constants
-a constant named PI, set to 3.1415926
Instance variables
-an instance variable named color to hold the color of a balloon (e.g. "red", "blue", "yellow")
-an instance variable named size e.g. 12.34
Methods
constructor
-a two parameter constructor that sets a new balloons color and size
inflate()
-takes one parameter. Adds this amount to the size of the balloon
volume()
-calculates and returns the current volume of the balloon. Formula for this is 4/3size3. (HINTS: be careful to avoid rounding error as you translate the formula into Java. And you can use size * size * size for size3)
one more method, give it a clear, meaningful name
-that returns the color of a balloon
The Tester class
Write a Tester class containing only a single main() method, that implements the following steps in order:
-use Scanner to read a color from the keyboard (hint: use next())
-then read a size from the keyboard
-create a new Balloon object with these properties
-inflate this balloon by 5.0 units
-output the color of the balloon in the format following. You are required to call a Balloon method as you do this
-output the volume of the balloon, 2 decimal places required, in the format following
For example, a run of your program would look something like:
Enter color: blue
Enter size: 5.5
For the blue balloon
Volume is now: 4849.05
Hints
You are writing two new classes named Balloon and Tester.
To help you with this lab, make sure you first download, study carefully and understand the Designing a class and Data types in Java example programs.
Then follow the usual development process (covered in the Designing a class lecture):
-design your new classes on a piece of paper
-identify instance variables and data types
-identify methods, design algorithms, think about parameters and return values
-the Balloon constructor has two parameters. Remember to say the data type for each of these two formal parameters when writing this method. In Tester, to call this constructor, list the two actual parameter values
as an example, if in the class Foo the constructor has these two params:
public Foo(int size, String taste)
{
// code to initialize all Foo instance variables
// goes here
}
then in another class, to call this constructor:
Foo obj = new Foo(123, "yukky");
also, you can use variables in the method call. For example, say that int x has the value 123 and String s has value "yukky", could say:
Foo obj = new Foo(x, s);
also, in Canvas, Data types in Java Example programs, study the Two parameters example. This shows the Savings class constructor, that demonstrates writing and calling a constructor that takes 2 parameters
-now use BlueJ to write and test your methods as you go
-(create a new project in BlueJ, then click New Class to start writing your program)
-write a method, create a new object and test the new method
-(BTW, when creating a new Balloon object manually in BlueJ during testing, the first param to the constructor is a String. You have to type double quotes around the value you enter here e.g. green)
-finish one method before starting the next
-finally, have Tester run your Balloon class. Check that the output in the Terminal Window is correct, then use Options | Save to file to save your output file as output.txt
Required
Failure to meet these requirements will cost you points:
-PI must be declared as a constant in the Balloon class
-every class and method you write must have a comment
-again, you are required to call a Balloon method as you output the color of the balloon from Tester
-again, volume must be output to 2 decimal places
-when your program is tested and correct, you must run it for a red balloon of size 99.0 and save these results as your output.txt output file
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started