Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 dont worry if you dont 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

// File: AssignmentDemo.java // Shows the effect of the assignment operator "=" when applied to object // variables. Remember that an object variable stores a "reference to" an // object (i.e. the address of the object). /** * A class to represent a car of a specific make and color. */ class Car { // instance variables private String make ; // the make of the car private String color ; // the color /** * Creates a car object. * @param theMake the make of the car * @param theColor the color of the car */ public Car(String theMake, String theColor) { make = theMake ; color = theColor ; } /** * Paint car object a new color. * @param newColor the new color */ public void paint(String newColor) { color = newColor ; } /** * Converts a Car object to a String. * @return a String containing the make and color of the car object */ public String toString() { return color + " " + make ; } } /** * Test class shows effect of assignment statement in Java. */ public class AssignmentDemo { public static void main(String args[]) { Car myCar = new Car("Tesla", "Teal") ; Car yourCar = new Car("Maserati", "Magenta") ; System.out.println( "myCar is a " + myCar.toString() ) ; System.out.println( "yourCar is a " + yourCar.toString() ) ; yourCar = myCar ; // Note: now both obj var's point to same car object System.out.println(" After assigning \"yourCar = myCar\"... ") ; System.out.println( "myCar is a " + myCar.toString() ) ; System.out.println( "yourCar is a " + yourCar.toString() ) ; // paint my car purple myCar.paint("Teaberry") ; System.out.println(" After painting ONLY my car Teaberry... ") ; System.out.println( "myCar is a " + myCar.toString() ) ; System.out.println( "yourCar is a " + yourCar.toString() ) ; } } /* program output: myCar is a Teal Tesla yourCar is a Magenta Maserati After assigning "yourCar = myCar"... myCar is a Teal Tesla yourCar is a Teal Tesla After painting ONLY my car Teaberry... myCar is a Teaberry Tesla yourCar is a Teaberry Tesla */ 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions