Programming Lab 1 Object-Oriented Programming in Java

Coin Purse

Objectives / 1. Practice designing and implementing an object-oriented program.
2. Learn how to write and compile a Java program using BlueJ.
3. Learn how to inspect and use objects interactively in BlueJ.
Entry Criteria (what you need) / 1. Java Development Kit (JDK 6.0).
2. BlueJ development environment.
3. (recommended) Java SE documentation and bookmark in your browser.
Download these from:
4. Sample code for this lab. Download from class homepage.
Tasks / 1. Read and understand the requirements.
2. Download and install the sample code. Open it as a BlueJ project.
3. Complete the exercises.
4. Test your program in BlueJ. Test using both valid and invalid data.
Evaluation / 1. After you have tested your own program, ask the TA or Instructor to evaluate it.
2. Upload a RAR file of your project to MaxLearn (course.ku.ac.th).
Exit Criteria / 1. A tested, running program, evaluated by TA or instructor.
2. Completed lab sheet.
Requirements

1. Write an application to simulate a coin purse that a person can insert coins and remove coins.

2. For this iteration, we will only allow one-baht, five-baht, and ten-baht coins.

3. The purse has a fixed capacity. The capacity is the number of coinsthat you can put in the purse. Capacity limits the number of coins, not the value of the coins.

4. You set the capacity of a Purse when you create it.

5. When a client (object) asks to withdraw money, the purse decides which coins to withdraw and return them to the client. Just like a machine that gives change.

Design

We want to put coins in the Purse, or withdraw coins from the Purse. So we need objects to represent a Coin and a Purse. When you design objects (design a class) you need to ask two questions:

(a) what should this object do? (behavior)

(b) what does this object need to know? (state)

Design of a Coin class: a coin has a value. You can examine the value, but you can't change it.

Behavior (doing): Coin needs a way to tell us its value. This is called an accessor method. In Java accessormethods have names beginning with "get". For example, getValue.

State (knowing): A coin needs to remember its own value.

Exercise 1: Complete the Coin Class

1. Write a Java class that implements the Coin class diagram. You should always include Javadoc comments in your source code for (a) description of class, (b) description of methods.

2. Test your Coin class interactively in BlueJ.

package oop.coinpurse;

/**

* Coin class represents a coin with a fixed value.

* @author Bill Gates 50540000

*/

public class Coin {

private int value; // value of this coin

/** Create a Coin object and set its value.

* @param avalue is the value of this coin.

*/

public Coin( int avalue ) {

this.value = avalue;

}

//TODO write accessor method for value

/**

* Return a string description of this coin

*/

public String toString() {

return value+"-Baht";

}

}

Design of the Purse

A Purse holds Coins. We want to insert and withdraw coins, and to know how much money or how many coinsare in the Purse.

Behavior: insert coin(s), withdraw money, compute the balance, count number of coins in Purse, and check whether Purse is full,

Purse( capacity ) / a constructor that creates an empty purse with a given capacity. new Purse( 6 ) creates a Purse with capacity 6 coins.
count( ) / returns the number of coins in the Purse
getBalance( ) / returns the value of all the coins in the Purse. If Purse has two 10-Baht and three 1-Baht coins then getBalance() is 23.
getCapacity( ) / returns the capacity of the Purse
isFull( ) / return true if the purse is full
insert( Coin ) / try to insert a coin in Purse. Returns true if insert OK, false if the Purse is full or the Coin is not valid (not 1, 5, or 10 Baht coin).
withdraw( amount ) / try to withdraw some money. Returns an array of Coin if we can withdraw, and returns null if cannot withdrawamount.
toString( ) / return a String describing what is in the purse.

State: a Purse needs to know its capacity. A Purse needs to know what coins is has. We could use a Collection to hold the coins but we haven't studied collections yet. Since all 10-Baht coins are exactly alike, the Purse only needs to know how many 10-Baht, 5-Baht, and 1-Baht coins it has.

Example: A Purse with capacity 10 contains 2 10-Baht coins, 4 5-Baht coins, and no 1-Baht ones.

purse.getBalance( ) returns 40

purse.count( )returns 6

purse.isFull( )returns false

purse.toString()returns "2 10-Baht, 4 5-Baht, 0 1-Baht coins" (you can be creative here)

purse.withdraw(11)returns null. The purse doesn't have any 1-Baht coins, so can't withdraw 11.

purse.withdraw(25)returns an array such as [ Coin(10),. Coin(10), Coin(5) ].

purse.insert( new Coin(2) ) returns false. We don't accept 2-Baht coins.

Exercise 2: Complete a UML class diagram of the Purse.

Exercise 3: Complete the Purse class

The sample code for this lab contains a partial implementation.

1. Complete all the methods, including Javadoc comments.

2. Thoroughly test the program in BlueJ. Try both valid and invalid inputs.

User Interface

In software design, each class should have just one purpose. All its methods are related to that purpose.

You should separate the user interface from the logic(or model)of your application.

These parts formlayers. For the Coin Purse application we have 2 layers:

The upper layer calls methods of the lower case. The lower layer provides services that the upper layer uses.

Boring Console User Interface

We will use a boring Console interface. The basic operation is:

loop until the user chooses to quit

print purse balance and status

ask user to input an action (deposit, withdraw, or quit)

read the user's choice

perform the user's choice

deposit

withdraw

or quit

end loop

For this lab, you can use the UserDialog in the sample code (no coding necessary).

Exercise 4: Write a Main class to Connect Parts of the Program

Write a small Main class to create objects and connectthe objects together. It does this:

1. create a Purse object with some capacity

2. create a UserDialog and give it a reference to the purse (this is calleddependency injection)

3. call run( ) to start the UserDialog object

package coinpurse;

publicclass Main {

private static int CAPACITY = 20;

public static void main( String [] args ) {

//TODO complete this code. Use the diagram above

}

}

Run Your Code. Test everything. Test using invalid data, too.
Java Language used in this lab
Create an object / Coin fivebaht = new Coin( 5 );
Purse purse = new Purse( 10 ); // 10 is capacity
Invoke a method:
object.method( ... ) / int amount = fivebaht.getValue( );
System.out.println(
"You have " + purse.getBalance() + " Baht");
purse.insert( new Coin(10) );
Use "this" to refer to attributes of object / if ( this.value == 1 ) print "one baht";
"this" is required only if a parameter or localvariable has same name as an attribute
public Purse( int capacity )
{ this.capacity = capacity; }
UsingtoString() / System.out.print and System.out.println will call toString when a string is needed for object
System.out.println( "Purse contains "+purse );
// println called purse.toString
Declare an array var / Coin [] coins; // declare name 'coins'
Create an array / coins = new Coin[6]; // array of 6 elements
Declare and create / Coin [] coins = new Coin[6];
Put objects in array / coins[0] = new Coin(5);
coins[1] = new Coin(10);
for(int k=2; k<6; k++) coins[k] = new Coin(1);

- 1 -