Purse
1. Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>.
a. Supply a method
void addcoin(String coinName)
that adds a coin to the purse.
b. Write a method toString() to the Purse class that prints the coins in the purse in the format
Purse[Quarter, Dime, Nickel, Dime]
2. Write a method reverse that reverses the sequence of coins in a purse. Use the toString method of the preceding assignment to test your code. For example, if reverse is called with a purse
Purse[Quarter, Dime, Nickel, Dime]
Then the purse is changed to
Purse[Dime, Nickel, Dime, Quarter]
3. Write a method for the Purse class,
public void transfer(Purse other)
that transfers the contents of one purse to another. For example, if a is
Purse[Quarter, Dime, Nickel, Dime]
and b is
Purse[Dime, Nickel]
then after the call, a.transfer(b), a is
Purse[Quarter, Dime, Nickel, Dime, Dime, Nickel]
and b is empty.
4. 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.
5. Write a method
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.
Screenshot

------------------------------------------------------------------------------------------
Program
import java.util.ArrayList;
//Create a class Purse
class Purse{
//Member variable
private ArrayList<String> coins;
//default constructor
public Purse() {
coins=new
ArrayList<String>();
}
//Method to add coins in the purse
void addcoin(String coinName) {
coins.add(coinName);
}
//Meethod to print Purse contents
public String toString() {
String str="Purse[";
for(int i=0;i<coins.size();i++)
{
if(i<coins.size()-1) {
str+=coins.get(i)+",";
}
else {
str+=coins.get(i);
}
}
str+="]";
return str;
}
//Method to reverse the purse contents order
public void reversePurse() {
ArrayList<String>newArrayList=new
ArrayList<String>();
for(int
i=coins.size()-1;i>=0;i--) {
newArrayList.add(coins.get(i));
}
coins=newArrayList;
}
//Method to transfer data from one purse to
other
//remove coins from the other purse
public void transfer(Purse other) {
for(int
i=0;i<other.coins.size();i++) {
this.coins.add(other.coins.get(i));
}
other.coins.clear();
}
//Check the contents of other purse has same contents
in same order
public boolean sameContents(Purse other) {
if(coins.size()!=other.coins.size()) {
return
false;
}
else {
for(int
i=0;i<coins.size();i++) {
if(!coins.get(i).equals(other.coins.get(i))){
return false;
}
}
return
true;
}
}
//Method to same contents in two purse
public boolean sameCoins(Purse other) {
if(coins.size()!=other.coins.size()) {
return
false;
}
else {
if(coins.containsAll(other.coins) &&
other.coins.containsAll(coins)) {
return true;
}
else {
return false;
}
}
}
}
//Test class
public class PurseTest {
public static void main(String[] args) {
//Creaate a purse object
Purse p=new Purse();
//Add coins into the purse
p.addcoin("Quarter");
p.addcoin("Dime");
p.addcoin("Nickel");
p.addcoin("Dime");
//Display purse contents
System.out.println("Contents of the
purse: "+p);
//Reverse the purse
p.reversePurse();
//Display purse contents
System.out.println("Contents of the
purse after reverse: "+p);
//Create another purse
Purse p1=new Purse();
//Add coins
p1.addcoin("Dime");
p1.addcoin("Nickel");
//Display contents
System.out.println("Contents of the
two purses before transfer:");
System.out.println(p);
System.out.println(p1);
p.transfer(p1);
System.out.println("Contents of the
two purses after transfer:");
System.out.println(p);
System.out.println(p1);
//Create another purse
p1=new Purse();
//add coins
p1.addcoin("Dime");
p1.addcoin("Nickel");
p1.addcoin("Dime");
p1.addcoin("Quarter");
//Check both has same content in
same order
if(p.sameContents(p1)) {
System.out.println("P and p1 has same contents!!!");
}
else {
System.out.println("P and p1 has not same contents!!!");
}
//Check both has same coins
//Creaate a purse object
Purse p2=new Purse();
//Add coins into the purse
p2.addcoin("Quarter");
p2.addcoin("Dime");
p2.addcoin("Nickel");
p2.addcoin("Dime");
if(p1.sameCoins(p2)) {
System.out.println("P1 and p2
has same coins!!!");
}
else {
System.out.println("P1 and p2
has not same coins!!!");
}
//Check for no change
System.out.println("Display purse p2 after
operation: "+p2);
}
}
-----------------------------------------------------------------
Output
Contents of the purse:
Purse[Quarter,Dime,Nickel,Dime]
Contents of the purse after reverse:
Purse[Dime,Nickel,Dime,Quarter]
Contents of the two purses before transfer:
Purse[Dime,Nickel,Dime,Quarter]
Purse[Dime,Nickel]
Contents of the two purses after transfer:
Purse[Dime,Nickel,Dime,Quarter,Dime,Nickel]
Purse[]
P and p1 has not same contents!!!
P1 and p2 has same coins!!!
Display purse p2 after operation:
Purse[Quarter,Dime,Nickel,Dime]
Purse 1. Implement a class Purse. A purse contains a collection of coins. For simplicity, we...
Ive got this started but im really struggling its a c# program, I have to modify the class Purse to implement the interface ICloneable . create a main program that demonstrates that the Purse method Clone works. using System; using System.Collections; namespace TestingProject { /// <summary> /// A coin with a monetary value. /// </summary> public class Coin { /// Constructs a coin. /// @param aValue the monetary value of the coin ...
implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods. class PiggyBank The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace...
Write a class encapsulation the concept of coins (Coins java), assuming that coins have the following attributes: a number of quarters, a number of dims, a number of nickels, and a number of pennies. Include a constructor (accept 4 numbers that represent the number of coins for each coin type), mutator and accessot methods, and method tostring. The tostring method should return a string in the following format: Total Value: $5.50 10 quarters, 20 dimes, 19 nickels, and 5 pennies...
Project 2 Tasks: Create a Coin.java class that includes the following: Takes in a coin name as part of the constructor and stores it in a private string Has a method that returns the coins name Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following: A constructor that passes the coins name to the parent A private variable that defines the...
c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below. Your program should have two sections that correspond to Problems 12 and 13: 1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your...
Write a program that uses the selection sort algorithm to sort an array of coins by their value. Name your class SelectionSorter.java and include the method public static void sort(Coin[] a). The following classes are already included: Main.java and Coin.java and do not need to be downloaded and zipped into submission. Console [15, 5, 1, 100, 25, 50] [1, 5, 15, 25, 50, 100] Input Files Coin.java //package Sorting1; /** A coin. */ public class Coin { private int value;...
# JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...
Consider the Automobile class: public class Automobile { private String model; private int rating; // a number 1, 2, 3, 4, 5 private boolean isTruck; public Automobile(String model, boolean isTruck) { this.model = model; this.rating = 0; // unrated this.isTruck = isTruck; } public Automobile(String model, int rating, boolean isTruck) { this.model = model; this.rating = rating; this.isTruck = isTruck; } public String getModel()...
Assignment: A set is a collection of items without repetitions. The goal of this assignment is to implement a set as a data structure. Do the following. 1. (30 points) Starting with the template attached, implement a generic class Set(T) that maintains a set of items of generic type T using the class ArrayList(T) in the Java API. Your Set class must provide the following instance methods: add: that adds a new item. (Ignore if already in the set.) remove:...
A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...