Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming Question --Below is Purse.Java Code: (we need to correct it with the missing part.)-- import java.util.ArrayList; public class Purse { private ArrayList listOfCoins;

Java Programming Question

image text in transcribed

--Below is Purse.Java Code: (we need to correct it with the missing part.)--

import java.util.ArrayList;

public class Purse

{

private ArrayList listOfCoins;

/**

* Constructs an empty purse.

*/

/**

* Adds a coin to the purse regardless of whether the coin was in the

* purse already or not

* @param coin the coin to add

*/

/**

* Removes a coin from the purse that is the same (denomination) as coin

* i.e. it removes a coin from the pruse that is equal to coin.

*

* precondition: equals has been define for coin

* @param coin a coin that matches the one that should be removed from purse

* @return true if the matching coin was removed from the purse, false otherwise

*/

/**

* Gives a text representation of the purse.

* @return a string in the format "Purse[coin1,coin2,...]"

*/

public String toString()

{

return "Purse" + listOfCoins.toString();

}

/**

* Counts the number of occurrences of coin in this purse.

*

* IMPLEMENTATION detail for the lab:

* in order for two coins to be considered equal, their value and their name

* must match but there is a method equals defined inside

* of the Coin class already

* @param coin the item to match against

* @return count the number of times the coin is in purse

*/

/**

* Determines if a purse has the same coins as the otherPurse,

* (regardless of order and possible duplicates).

*

* IMPLEMENTATION detail for the lab: you must use the method this.occurrences

* when implementing hasSameCoins

* @param otherPurse the other purse with coins

* @return true if this and the otherPurse have the same coins, false otherwise

*/

/**

* Gives the highest monetary value of any coin in the purse.

* @return the highest coin value of any coin in the purse and 0 if the purse is empty.

*/

}

--Below is the PurseTester.java code: (we should write more tests than the one given)--

public class PurseTester

{

/**

* Creates a few purses and test the various methods of the class.

* All the parameters are 'hardwired' here ... there is no input from a user.

* @param args not used

*/

public static void main(String[] args)

{

Purse a = new Purse();

a.addCoin(new Coin("quarter", 25));

a.addCoin(new Coin("Dime", 10));

a.addCoin(new Coin("Nickel", 5));

a.addCoin(new Coin("Dime", 10));

System.out.println("a = " + a);

Purse b = new Purse();

b.addCoin(new Coin("nickel", 5));

b.addCoin(new Coin("dime", 10));

b.addCoin(new Coin("dime", 10));

b.addCoin(new Coin("quarter", 25));

System.out.println("b = " + b);

// at this point we expect a and be to be equal

testHasSameCoins(a, b, "a","b", true);

Purse c = new Purse();

c.addCoin(new Coin("quarter", 25));

c.addCoin(new Coin("penny", 1));

c.addCoin(new Coin("Nickel", 5));

c.addCoin(new Coin("dime", 10));

System.out.println("c = " + c);

Purse d = new Purse();

d.addCoin(new Coin("NICKEL", 5));

d.addCoin(new Coin("DIME", 10));

d.addCoin(new Coin("DIME", 10));

d.addCoin(new Coin("QUARTER", 25));

d.addCoin(new Coin("quarter", 25));

System.out.println("d = " + d);

// c and d are different so we expect as output false

testHasSameCoins(c, d, "c", "d", false);

// d and d are the same so we expect as output true

testHasSameCoins(d, d, "d", "d", true);

Purse e = new Purse();

System.out.println("e = " + e);

// the empty purse and any other purse with coins are different, so expecting false

testHasSameCoins(e, d, "e", "d", false);

Purse f = null;

System.out.println("f = " + f + " ");

// the empty purse and the null reference are not the same

System.out.print("Do e and f have the same coins? ");

System.out.println(e.hasSameCoins(f) + " and " + "Expected: false ");

// missing ares test for some methods like

// removeCoin

// getHighestCoinValue

}

/**

* Take two purses and their names and report whether they are the same.

* Use the method hasSameCoins when comparing.

* @param x purse against which y is compared, x != null

* @param y purse against which x is compared, y != null

* @param xName name that will be printed for the first parameter

* @param yName name that will be printed for the second parameter

* @param expectedValue what the result of the comparison should be

*/

public static void testHasSameCoins(Purse x, Purse y,

String xName, String yName, boolean expectedValue)

{

System.out.println();

System.out.print("Do " + xName + " and " + yName + " have the same coins? ");

System.out.println(x.hasSameCoins(y) + " and " + y.hasSameCoins(x));

System.out.println("Expected: " + expectedValue + " and " + expectedValue + " ");

}

}

--Below is a Coin.java code:--

public class Coin

{

private int value;

private String name;

/**

Constructs a coin and gives it the name with lower case

letters with the passed monetary value.

@param aName the name of the coin

@param aValue the monetary value of the coin.

*/

public Coin(String aName, int aValue)

{

value = aValue;

name = aName.toLowerCase();

}

/**

Retrieves the coin value.

@return the value

*/

public int getValue()

{

return value;

}

/**

Retrieves the coin name.

@return the name

*/

public String getName()

{

return name;

}

/**

Makes a string of the form (value,name)

@return the string

*/

public String toString()

{

return "(" + name + "," + value + ")";

}

/**

Determines if two Coins are equal.

@return true if the monetary value in pennies and the names are the same.

*/

public boolean equals(Object otherObject)

{

if (otherObject == null)

return false;

if (getClass() != otherObject.getClass())

return false;

Coin other = (Coin) otherObject;

return value == other.value && name.equalsIgnoreCase(other.name);

}

}

Implement a class Purse. A purse contains a collection of coins. The class Coin is provided for you (and documented as well) Store the coins as an ArrayList of Coin in the class Purse. Supply a constructor. Fill the "missing code" in the methods and constructor of Purse. Look at the folder "docs" for the documentation of both Coin and Purse classes Check the Java API for ArrayList methods that are not mentioned in the class. For example the "toString" method of ArrayList. Please note that the equals method of the Coin class has been implemented and can be used. Test that your class Purse is correct with the Purse Tester.java. Add more tests as necessary. To submit at the end of the Lab3 with D2L Purse.java (with the missing code included) - PurseTester.java (with more tests than the ones given)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is the principle of thermodynamics? Explain with examples

Answered: 1 week ago