CS 1054: Introduction to Programming in JAVA

CS 1054 Introduction to Programming in Java (Spring 2008)

Lab 3: Implementing Classes

Lab Objectives

Instructions

For this lab, you will need to produce two BlueJ projects called Lab3A and Lab3B. For the Lab3A project, part of the code has been written for you. For the Lab3B project, you have to create the files from scratch.

Lab3A project

  1. Download Lab3A.zip , save the file, and then extract it in a folder you can locate. This is a BlueJ project that contains code for two classes: PiggyBank and PiggyBankTester.
  2. Try out both classes and understand what PiggyBank objects are and how they behave. In particular, notice the methods you can invoke on a PiggyBank object:
    • public void putNickels( int nickelCount ): adds nickels to the piggy bank
    • public int getCoinCount(): returns the number of coins currently inside the piggy bank
    • public double getDollarAmount(): returns the total dollar amount inside the piggy bank
  3. Complete the PiggyBank class by adding three more methods to the PiggyBank class: putDimes , putQuarters , and emptyOut . The first two methods are methods similar to putNickels, except that dimes and quarters are added to the piggy bank, respectively:
    • public void putDimes( int dimeCount ): adds dimes to the piggy bank
    • public void putQuarters( int quarterCount ): adds quarters to the piggy bank
    • public void emptyOut(): removes all the coins from the piggy bank
    When done, test these three additional methods by "uncommenting" the second part of the code for PiggyBankTester.
  4. Compile both classes and then execute PiggyBankTester . When you get the expected output as indicated, you are done with this portion of the lab.
  5. Submit through Webcat. Consult your TA for instructions.
Lab3B project
  1. Create an EggTrader class. An egg trader starts off with 10.00 dollars and no eggs, buys eggs by the dozen, and sells them by the piece. Buying eggs reduces cash and adds eggs; selling eggs reduces eggs on hand and causes an increase in cash. The methods of the EggTrader class are as follows:
    • public void buyOneDozen( double pricePerDozen ): buys one dozen eggs at the given price
    • public void sellEggs( int numEggs, double unitPrice ): sells numEggs eggs at the given per-egg price
    • public int getEggCount(): returns the number of eggs the trader has on hand
    • public double getCash(): returns the cash amount the trader has on hand
  2. Create a tester for the EggTrader class. Make sure you call all the methods you developed. (A different tester will be used when grading your program so make sure the method names for EggTrader are spelled properly).
  3. Submit through Webcat.