Programming Assignment #2
(Using an Existing Class: Creating Objects
and Calling Accessor and Mutator Methods)
I. The Assignment
This assignment is to write a “test” class (aka: a “driver” class or “client code”) that uses the class Balloon.java, available on the class web page.
To use the Balloon class, download it and store it in the src folder of your NetBeans project. Make sure you save it as Balloon.java.
The best way to learn how to use the Balloon class – or any other Java class, for that matter - is to consult the documentation, Balloon.html (online). You can also read the javadoc comments that appear just above the class declaration and above each method declaration, which explain what each method does, what the method’s parameters are, and what value – if any - is returned by the method. The html “help” pages are generated from these comments.
Don’t worry if you don’t understand the code. It will all be covered later. It is not necessary to know how a method works as long as you know what it does and how to call it.
Review declaring variables, creating objects, calling methods that return a value vs. “void” methods, and accessor and mutator methods before beginning.
To receive credit for this assignment, you must not modify the Balloon class in any way!
II. Your BalloonTester Class
Your BalloonTester class will have only a single method – main – and will perform each of the following operations, in the exact order listed below. Each operation may be done in one or two statements. Make sure you follow directions faithfully, and note that once you have done step 3, you can copy and paste it to do steps 6, 9, and 12.
1. Create a Balloon object with a name of your own choosing and an altitude of 100 meters.
2. Create a second Balloon object with a name of your own choosing, and specify an initial altitude of -100 meters.
3. Call the accessor methods of the Balloon class to get the name and altitude of each Balloon object. Print the data, one object per line.
4. Make the object you created in step 1 ascend to an altitude of 250 meters.
5. Call the adjustAltitude method to increase the altitude of the object you created in step 2 by 150 meters.
6. Call the accessor methods of the Balloon class to get the name and altitude of each object. Print the data, one object per line.
7. Call the adjustAltitude method to decrease the altitude of the object you created in step 1 by 150 meters.
8. Make the object you created in step 2 descend to the same altitude as the other object. You may assume that the other object is at a lower altitude.
To get credit for step 8., the statement(s) you write must always work, regardless of the actual altitude of the second object. It cannot depend on you knowing the altitude of the second object, but must utilize the fact that the object knows its own altitude. In other words, if you use a literal in any way to set the altitude, it is not correct.
9. Call the accessor methods to get the name and altitude of each object. Print the data, one object per line.
10. Move the object you created in step 1 to an altitude that is four times its current altitude. As in step 8, the statement(s) you write must work for any altitude and may not depend on you “figuring out” the new altitude beforehand.
11. Attempt to move the object you created in step 2 to an altitude that is 150 meters below its current altitude.
12. Call the accessor methods to get the name and altitude of each object. Print the data, one object per line.
--- Update ---
and this is the Balloon.java given:
// File: Balloon2.java // Modified Balloon class has overloaded constructors /** * A class to represent a hot-air balloon. Balloon objects have a name and an * altitude. */ public class Balloon2 { // instance variables private String name ; // name of the balloon private int altitude; // altitude (height) of balloon in meters // "class" variable private static int count = 1 ; // to create default name /** * Create a ballon object with a given name at a given altitude. * @param theName the name of the ballon object * @param theAltitude the altitude */ public Balloon2(String theName, int theAltitude) { name = theName ; // make sure altitude is not negative! altitude = Math.max(theAltitude,0) ; } /** * Create a ballon object with a given name at defaule altitude of 0. * @param theName the name of the ballon object */ public Balloon2(String theName) { name = theName ; altitude = 0 ; } /** * Create a ballon object with a default name at a given altitude. * @param theAltitude the altitude */ public Balloon2(int theAltitude) { name = "Balloon" + count ; count = count + 1 ; // make sure altitude is not negative! altitude = Math.max(theAltitude,0) ; } /** * Create a ballon object with a default name and default altitude of 0. */ public Balloon2() { name = "Balloon" + count ; count = count + 1 ; altitude = 0 ; } /** * Ascend to a particular altitude. * @param newAlt the altitude to which to ascend, in meters. */ public void ascendTo(int newAlt) { // ascend to new altitude only if it is greater than current altitude, if (newAlt > altitude) { altitude = newAlt ; } } /** * Descend to a particular altitude. * @param newAlt the altitude to which to descend, in meters. */ public void descendTo(int newAlt) { // prevent possible crash into ground if (newAlt < 0) // if desired altitude is below ground level! { altitude = 0 ; // ...descend only to ground level } // otherwise, descend only if new altitude is less than current altitude else if (newAlt < altitude) { altitude = newAlt ; } } /** * Modify altitude by a given number of meters, up or down. * @param change number of meters to add to current altitude */ public void adjustAltitude(int change) { // if change is negative (i.e. descending), can't go below 0 altitude if (change + altitude < 0) // change < 0 && abs(change) > altitude { altitude = 0 ; // ...descend only to ground level } else // safe to modify current alt by "change" meters { altitude = altitude + change ; } } /** * Get ballon name. * @return name the name of the balloon */ public String getName() { return name ; } /** * Get current altitude. * @return altitude the altitude of the balloon */ public int getAltitude() { return altitude ; } } // end of Balloon class definition