CSC108 Assignment 2


Bigger and better ...

A2 requires you to write more complex code than in A1, and we expect (on average) that you'll need 10--15 hours to complete it. Plan to spend as much as two hours (!) reading this handout, before you write a single line of code, so that you thoroughly understand what we're asking you to do. This is a good activity to do with a partner (registered a week ahead of the due date, of course)before you begin programming.

We strongly encourage you and your partner to work together at all times, and alternate driving and navigating. You'll both benefit, even if it feels like it is taking longer.

Purpose

A2 is meant to give you practice designing and writing classes, writing constructors, writing and using static methods, writing and using boolean methods, and writing classes that interact with each other.

Overview

The World Trade Organization (WTO) has decided that member countries are no longer allowed to protect their air supplies from international market forces. Consequently Grasping Air Supply Procurers (GASP) has attained monopoly control of 97% of the globe's air, and the remaining 3% is far above sea level (hence inaccessible to most lungs). GASP franchises air fill shops where users may buy air. Breathers live in airtight chambers and buy air from their nearest GASP franchise. Breathers earn money, which they can spend buying air, and each breather has a particular lung capacity (in litres of air per day) that must be filled to avoid dire consequences. The total money earned by all GASP franchises taken together is kept track of.

The user of the program you will write will be run from DrJava's Interactions Pane. It will create air fill shops, chambers, breathers, and will cause these objects to interact. We'll more completely specify the program below, but first some rules:

Vitally important (and extremely strict) rules

Here is a list of rules that you must follow in A2. If you break any of these, you will automatically get a zero on all or part of this assignment. Most of these have been discussed and used in class all term.
  1. Put instance variables at the beginning of the class and methods at the end. Put all constructor methods right after the instance variables, before the other methods.
  2. All the methods must be public, and all instance variables private.
  3. Every variable you declare must be used. Delete the declarations of any variables that break this rule.
  4. Every class, method, and instance variable must have a javadoc comment describing what it represents. Do not write comments describing the "part of speech." Here's what you shouldn't do:
    /**
     * This declares i as an instance variable, and i represents
     * the number of widgits.
     */
    private int i;
    
    Instead, here's what you should do (use the descriptive part):
    /**
     * The number of widgits.
     */
    private int i;
    
  5. Don't assign to a variable, and then replace the value without ever using the original one:
    public class X {
      JFrame theWindow= new JFrame();
    
      /** Make a new X containing j. */
      public X(JFrame j) {
        this.theWindow= j;
      }
    }
    
  6. Use the brace placement that we demonstrate in class: opening braces go at the end of a line, and the corresponding closing brace is outdented. Do not put opening braces on their own line. (UTM students should read a separate announcement concerning this rule).
  7. Place a blank line above every comment (except the class comment). For example:
    /** class comment here */
    public class X {
    
      /** Instance variable comment here. Notice the preceding blank line */
      private int i;
    
      /** Method comment here.  Notice the preceding blank line. */
      public void m() {
    
        // Put a blank line before comments in here. */
        i= 4;
      }
    }
    
  8. Don't define any methods other than the ones specified.
  9. All methods must have an appropriate comment (see the API for comment examples).
  10. For all methods, the order of parameters is important. Declare parameters in the order they are described in this handout.
  11. Don't worry about whether adjusting the air results in a negative amount (we can't actually enforce this rule by checking whether you worry or not).
  12. Use the capitalization we specify.
  13. All variables must begin with a lowercase letter, and have internal words begin with an initial capital (then lower case).
  14. All instance variables must have meaningful names. Local variables and parameters should be short.
  15. You may not use loops or if statements.
  16. All input and output must be done in the test programs. None should be done in AirFillShop, etc.

Program specification

Write classes AirFillShop, Chamber, and Breather, as well as AirFillShopTester, ChamberTester, and BreatherTester, with the following (complete, although informal) specification.

AirFillShop

An air fill shop has a name (a String, an income (a double), and a price (in dollars) per litre of air (a double which is never less than 1.00 --- monopolies have their advantages). There are two ways to construct an AirFillShop:
  1. Provide price per litre (a double) and the shop's name (a String).
  2. With a single String containing both pieces of information, separated by a comma followed by a space; first the price per litre and then the name. You'll need to use String methods such as indexOf and substring, and also Double.parseDouble. Here's an example for a shop called Last Gasp charging $13 per litre:
    13.00, Last Gasp
    
Air fill shops have a sellAir method, which when given an amount of money (a double) returns the number of litres of air the money buys, rounded down to the nearest int. Notice that this means that an air fill shop routinely overcharges. Air fill shops have a getName method, which returns the shop's name, and a getMLPerDollar method, which returns the number (a double) of millilitres that this shop sells for a dollar (useful to make the shop's price appear less expensive). They also have a toString method, which returns a String of this form (no newline):
Store name: x.yz millilitres per dollar.
    
(of course "Store name", and "x.yz" are replaced by the appropriate data for this air fill shop). Here's an example using Last Gasp and it $13 per litre price:
Last Gasp: 76.92307692307692 millilitres per dollar.
    
Note the colon (:) and the final period (.). Don't worry about the number of decimal places to the right of the decimal. You must use this exact format, including spaces. The total income for all air fill shops is maintained in a double static variable, which can be retrieved by calling static method getTotalIncome

AirFillShopTester

Write a class called AirFillShopTester that has a single method test with no parameters. This method should prompt the user for air fill shop information (all on one line, as specified by the second AirFillShop constructor). Your test method should do this for two air fill shops, and then your test method should call each AirFillShop method to make sure they work as specified.

Chamber

A chamber has an address (a String). Its method getAddress returns this address (as a String).

A chamber has an integer number of litres of air (at standard temperature and pressure). This amount starts at 0 litres. Whenever some air is bought or breathed, you call the chamber's adjustAirBy method with the number of litres of air (an int, postive if some air has been bought, negative if some has been breathed). The chamber's getAvailableAir reports the number of litres of air (an int) in the chamber.

There are two ways to construct a chamber:

  1. With the address (a String) and the nearest AirFillShop.
  2. With the address (String) only.
A chamber has methods to set and get the nearest air fill shop: setNearestAirFillShop, which is given an AirFillShop, and getNearestAirFillShop, which returns this chamber's nearest AirFillShop.

Chambers also have a toString method, which returns a String (all on one line, no newline character) in this form:

Chamber: address, Air (in litres): xxx, Air fill shop: name.
    
...where you fill in "address", "xxx", and "name" with the appropriate information for this chamber. For example:
Chamber: 19 Drury Lane, Air (in litres): 97, Air fill shop: Wheezers.
    
Note the commas, spacing, and the final period. You must use this exact format, including spaces.

ChamberTester

Write a class called ChamberTester that has a single method, test, with no parameters. This method should prompt the user for two Chamber addresses and information for two AirFillShops. Your test method should create the four objects, and then call each Chamber method to make sure they work.

Breather

Breathers have a Chamber, a name (a String), a capacity (amount of air breathed per day in litres --- an int), and money in dollars (a double).

You can tell a breather to buy air for its chamber, using the breather's buyAir method. The breather will buy as much air as they can afford from their chamber's nearest air fill shop, and put that air in their chamber, leaving their supply of money at zero.

You can tell a breather to breathe for a day by calling its breathe method. Assume that when breathe is called there's enough air in the chamber.

You can ask a breather if there's enough air for them to breathe for a day, by calling canBreathe, which returns true or false as appropriate.

You can give a breather more money by callings its earnMoney method, which is given the amount of money earned (a double).

You can move a breather to a different chamber by calling the breather's move method, which is given the Chamber it should move to. They leave behind the air supply at their old chamber, and use the air supply at their new chamber.

There are several ways to construct Breathers:

  1. With their name (a String), and capacity (an int) (no chamber or money).
  2. With their name (a String), capacity (an int), and money (a double) (no chamber).
  3. As in the previous constructor, but with the three pieces of information all together in a single String, each piece separated by a comma and a single space:
    Adam, 5, 99.95
    	
  4. With the name (a String), capacity (an int), money (a double), and a chamber.
Breathers also have a toString, which returns a String in this form:
Adam, breathes at Paradise Chambers, has $1050.0, can breathe: true.
Note the commas, spacing, punctuation, and the final period. You must this exact format, including spaces, and no newline.

This should appear on a single line, with no newline characters.

Again, don't worry about getting exactly two digits after the period. Just use whatever value is in the double.

BreatherTester

Write a class called BreatherTester that has a single method, test, with no parameters. This method should prompt the user for information for two breathers, two chambers, and information for two air fill shops. Create the six objects. After that, your test method should call each Breather method to make sure they work.

What to submit

Submit the .java files containing your Chamber, Breather, AirFillShop, ChamberTester, BreatherTester, and AirFillShopTester classes.
Danny Heap
Last modified: Thu Feb 20 16:15:50 EST 2003