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

Programming Assignment #2 Using an Existing Class: Creating Objects and Calling

ID: 3746861 • Letter: P

Question

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.

To use the Balloon class, download it and store it in the src folder of your NetBeans project as Balloon.java.

The best way to learn how to use the Balloon class – or any other Java class - is to consult the documentation for the class, Balloon.html (online). These web pages tell you how to create objects (i.e. the number and types of parameters required by the constructor), what methods are available, what type of value (if any) is returned by each method, and the number and types of parameters required by each method.

Feel free to examine the Balloon class code but don’t worry if you don’t understand it. It will all be covered later (in Unit 3) and there is nothing in there that can be used for this assignment. Remember that it is not necessary to know how a method does what it does - you just have to know how 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.

Create a Balloon object with a name of your own choosing and an altitude of 150 meters.

Create a second Balloon object with a name of your own choosing, and specify an initial altitude of -50 meters.

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.

Call the ascendTo method to move the object you created in step 1 to 200 meters.

Call the adjustAltitude method to increase the altitude of the object you created in step 2 by 100 meters.

Call the accessor methods of the Balloon class to get the name and altitude of each object. Print the data, one object per line.

Call the adjustAltitude method to decrease the altitude of the object you created in step 1 by 50 meters.

Make the object you created in step 2 ascend to the same altitude as the other object. You may assume that the other object is at a higher 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 to set the altitude, it is not correct.

Call the accessor methods to get the name and altitude of each object. Print the data, one object per line.

Move the object you created in step 1 to an altitude that is three 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.

Attempt to move the object you created in step 2 to an altitude that is 200 meters below its current altitude.

Call the accessor methods to get the name and altitude of each object. Print the data, one object per line.

III. What to Upload to Moodle

Upload a zip file containing your Project Folder and the output generated. To make sure you receive credit for this assignment make sure you follow the directions on the “Submitting Your Assignments” and “Creating a Zip File” documents online.

IV. Due Date: Thursday, September 13th

Explanation / Answer

Though you have not attached the provided Balloon.java code file so i looked up on the internet and found one. You can change the solution according to your Balloon.java -

public class BalloonTester

{

private static int altitude;

private static String name;

public static void main(String[] args)

{

int h100 = 100;

BalloonTester myBalloon = new BalloonTester ();

//creating a second object

BalloonTester secondBalloon = new BalloonTester(); //creates the object

//calling accesor methods

name = myBalloon.getname();

altitude = myBalloon.getaltitude();

altitude = secondBalloon.getaltitude();

//print the info obtained

System.out.println( " MyBalloon has an altitude of " + myBalloon.getaltitude()

+ "," + " and secondBalloon has an altitude of " + secondBalloon.getaltitude());

//Ascend myBalloon to an altitude of 200 meters

myBalloon.Ascend(200);

//Adjust the altitude of secondBalloon to 250 meters

secondBalloon.adjustAltitude(250);  

System.out.println( " MyBalloon has an altitude of " + myBalloon.getaltitude());

System.out.println( " SecondBalloon has an altitude of " + secondBalloon.getaltitude());

  

myBalloon.adjustAltitude(50);

}

private BalloonTester() {

}

private BalloonTester(String myBalloon) {

}

private int getaltitude() {

return altitude;

}

private void Ascend(int i) {

}

private void adjustAltitude(int i) {

}

private String getname() {

return name;

  

}

private void adjustAltitude() {

//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

}

}