Computer Science 210
Data Structures

Fall 2016, Siena College

PocketChange BlueJ Project

Click here to download a BlueJ project for PocketChange.


PocketChange Source Code

The Java source code for PocketChange is below. Click on a file name to download it.


PocketChange.java

/*   $Id: PocketChange.java 893 2009-09-14 02:58:54Z terescoj $  */
/**
   Pocket change simulator
 
   @author Jim Teresco, terescoj@cs.williams.edu
*/

import structure.*;

public class PocketChange {

    /** vector of coins in our pocket */
    protected Vector coins;

    /**
       Create an empty pocket
    */
    public PocketChange() {

	coins = new Vector();
    }

    /**
       add a coin to the pocket
       
       @param coin value of coin to add
    */
    public void add(int coin) {

	coins.add(new Integer(coin));
    }

    /**
       remove a coin from the pocket

       @param coin value of coin to remove
    */
    public void remove(int coin) {

	// why won't this alone work?
	// coins.remove(new Integer(coin));

	for (int i=0; i<coins.size(); i++) {
	    int value = ((Integer)coins.get(i)).intValue();
	    if (value == coin) {
		// we found the right kind of coin
		coins.remove(i);
		// don't keep looking!
		return;
	    }
	}
	// if we get here, it's a problem -- we didn't have the coin we wanted
	System.out.println("Had no coin of value " + coin);
    }

    /**
       determine the number of coins in the pocket

       @return the number of coins in the pocket
    */
    public int numCoins() {

	return coins.size();
    }

    /**
       determing the total value of coins in the pocket

       @return sum of the values of all coins in the pocket
    */
    public int value() {
	int total = 0;
	for (int i=0; i<coins.size(); i++) {
	    total += ((Integer)coins.get(i)).intValue();
	}
	return total;
    }

    /**
       try out our pocket change class

       @param args command-line arguments
    */
    public static void main(String[] args) {

	// construct the pocket
	PocketChange pocket = new PocketChange();

	// make some transactions
	pocket.add(1);
	pocket.add(10);
	pocket.add(5);
	pocket.add(5);
	pocket.add(25);
	pocket.add(1);
	pocket.add(10);
	pocket.add(10);
	
	System.out.println("Pocket with " + pocket.numCoins() +
			   " coins, value " + pocket.value());

	pocket.remove(25);
	pocket.remove(10);
	pocket.remove(10);
	pocket.add(1);
	pocket.add(1);

	System.out.println("Pocket with " + pocket.numCoins() + 
			   " coins, value " + pocket.value());
    }
}

PocketChangeAutobox.java

/*   $Id: PocketChangeAutobox.java 893 2009-09-14 02:58:54Z terescoj $  */
/**
   Pocket change simulator demonstrating autoboxing
 
   @author Jim Teresco, terescoj@cs.williams.edu
*/

import structure.*;

public class PocketChangeAutobox {

    /** vector of coins in our pocket */
    protected Vector coins;

    /**
       Create an empty pocket
    */
    public PocketChangeAutobox() {

	coins = new Vector();
    }

    /**
       add a coin to the pocket
       
       @param coin value of coin to add
    */
    public void add(int coin) {

	// autoboxing - the int is automatically packed up in an Integer
	coins.add(coin);
    }

    /**
       remove a coin from the pocket

       @param coin value of coin to remove
    */
    public void remove(int coin) {

	// why won't this alone work?
	// coins.remove(new Integer(coin));

	for (int i=0; i<coins.size(); i++) {
	    // we have to cast Object to Integer, but Java will autounbox it
	    int value = (Integer)coins.get(i);
	    if (value == coin) {
		// we found the right kind of coin
		coins.remove(i);
		// don't keep looking!
		return;
	    }
	}
	// if we get here, it's a problem -- we didn't have the coin we wanted
	System.out.println("Had no coin of value " + coin);
    }

    /**
       determine the number of coins in the pocket

       @return the number of coins in the pocket
    */
    public int numCoins() {

	return coins.size();
    }

    /**
       determing the total value of coins in the pocket

       @return sum of the values of all coins in the pocket
    */
    public int value() {
	int total = 0;
	for (int i=0; i<coins.size(); i++) {
	    // again, autounboxing after the cast to Integer
	    total += (Integer)coins.get(i);
	}
	return total;
    }

    /**
       try out our pocket change class

       @param args command-line arguments
    */
    public static void main(String[] args) {

	// construct the pocket
	PocketChange pocket = new PocketChange();

	// make some transactions
	pocket.add(1);
	pocket.add(10);
	pocket.add(5);
	pocket.add(5);
	pocket.add(25);
	pocket.add(1);
	pocket.add(10);
	pocket.add(10);
	
	System.out.println("Pocket with " + pocket.numCoins() +
			   " coins, value " + pocket.value());

	pocket.remove(25);
	pocket.remove(10);
	pocket.remove(10);
	pocket.add(1);
	pocket.add(1);

	System.out.println("Pocket with " + pocket.numCoins() + 
			   " coins, value " + pocket.value());
    }
}

PocketChangeT.java

/*   $Id: PocketChangeT.java 909 2009-09-20 18:33:20Z terescoj $  */
/**
   Pocket change simulator demonstrating autoboxing, generic Vectors
 
   @author Jim Teresco, terescoj@cs.williams.edu
*/

import structure5.*;

public class PocketChangeT {

    /** vector of coins in our pocket */
    protected Vector<Integer> coins;

    /**
       Create an empty pocket
    */
    public PocketChangeT() {

	coins = new Vector<Integer>();
    }

    /**
       add a coin to the pocket
       
       @param coin value of coin to add
    */
    public void add(int coin) {

	// autoboxing - the int is automatically packed up in an Integer
	coins.add(coin);
    }

    /**
       remove a coin from the pocket

       @param coin value of coin to remove
    */
    public void remove(int coin) {

	// why won't this alone work?
	// coins.remove(new Integer(coin));

	for (int i=0; i<coins.size(); i++) {
	    // no cast needed since Java knows the Vector contains
	    // Integers, plus Java will autounbox it to an int
	    int value = coins.get(i);
	    if (value == coin) {
		// we found the right kind of coin
		coins.remove(i);
		// don't keep looking!
		return;
	    }
	}
	// if we get here, it's a problem -- we didn't have the coin we wanted
	System.out.println("Had no coin of value " + coin);
    }

    /**
       determine the number of coins in the pocket

       @return the number of coins in the pocket
    */
    public int numCoins() {

	return coins.size();
    }

    /**
       determing the total value of coins in the pocket

       @return sum of the values of all coins in the pocket
    */
    public int value() {
	int total = 0;
	for (int i=0; i<coins.size(); i++) {
	    // again, autounboxing
	    total += coins.get(i);
	}
	return total;
    }

    /**
       try out our pocket change class

       @param args command-line arguments
    */
    public static void main(String[] args) {

	// construct the pocket
	PocketChange pocket = new PocketChange();

	// make some transactions
	pocket.add(1);
	pocket.add(10);
	pocket.add(5);
	pocket.add(5);
	pocket.add(25);
	pocket.add(1);
	pocket.add(10);
	pocket.add(10);
	
	System.out.println("Pocket with " + pocket.numCoins() +
			   " coins, value " + pocket.value());

	pocket.remove(25);
	pocket.remove(10);
	pocket.remove(10);
	pocket.add(1);
	pocket.add(1);

	System.out.println("Pocket with " + pocket.numCoins() + 
			   " coins, value " + pocket.value());
    }
}