Purchase Solution

Java - Purse

Not what you're looking for?

Ask Custom Question

Write a method for the Purse class public boolean sameContents(Purse other) that checks whether the other purse has the same coins in the same order.

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
order as another purse.
@param other the other purse
@return true if the two purses have the same coins in the
same order, false otherwise
*/
public boolean sameContents(Object 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");

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

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

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

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

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

Purse e = new Purse();
e.addCoin("Nickel");
e.addCoin("Dime");
e.addCoin("Nickel");

Purse f = new Purse();
f.addCoin("Nickel");
f.addCoin("Dime");

System.out.println(e.sameContents(f));
System.out.println("Expected: false");
}
}

Purchase this Solution

Solution Summary

In an attached Java file, this solution provides the method for the Purse program.

Purchase this Solution


Free BrainMass Quizzes
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.

C# variables and classes

This quiz contains questions about C# classes and variables.

Basic Networking Questions

This quiz consists of some basic networking questions.

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.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.