Purchase Solution

Java - Purse Methods

Not what you're looking for?

Ask Custom Question

Write a method for the Purse class public boolean sameCoins(Purse other) that checks whether the other purse has the same coins, perhaps in a different order. For example, the purses

Purse[Quarter,Dime,Nickel,Dime]

and

Purse[Nickel,Dime,Dime,Quarter]

should be considered equal. You will probably need one or more helper methods.

Complete the following class in your solution:

import java.util.ArrayList;

/**
A purse holds a collection of coins.
*/
public class Purse
{
. . .

private ArrayList<String> coins;

/**
Constructs an empty purse.
*/
public Purse()
{
coins = new ArrayList<String>();
}

/**
Add a coin to the purse.
@param coinName the coin to add
*/
public void addCoin(String coinName)
{
. . .
}

/**
Returns a string describing the object.
@return a string in the format "Purse[coinName1,coinName2,...]"
*/
public String toString()
{
. . .
}

/**
Determines if a purse has the same coins in the same or different
order as another purse.
@param other the other purse
@return true if the two purses have the same coins in the
same or different order, false otherwise
*/
public boolean sameCoins(Purse other)
{
. . .
}
}

Use the following class as your tester class:

/**
This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");

Purse b = new Purse();
b.addCoin("Nickel");
b.addCoin("Dime");
b.addCoin("Dime");
b.addCoin("Quarter");

System.out.println(a.sameCoins(b));
System.out.println("Expected: true");

Purse c = new Purse();
c.addCoin("Quarter");
c.addCoin("Penny");
c.addCoin("Nickel");
c.addCoin("Dime");

Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Dime");
d.addCoin("Quarter");

System.out.println(c.sameCoins(d));
System.out.println("Expected: false");
}
}

Purchase this Solution

Solution Summary

Java purse methods are examined.

Purchase this Solution


Free BrainMass Quizzes
Basic Networking Questions

This quiz consists of some basic networking questions.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.