Question

In: Computer Science

JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we...

JAVA PROGRAMMING
Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>.

  • Supply a method void addCoin(String coinName).
  • Add a method toString to the Purse class that prints the coins in the purse in the format Purse[Quarter,Dime,Nickel,Dime].
  • Write a method reverse that reverses the sequence of coins in a purse.
  • Implement a TestPurse class with a main method in it. It will use the toString method 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].
  • MUST BE TWO SEPERATE CLASSES

IMP: This is what I have so far. Please correct it.

PURSE CLASS:
import java.util.Collections;
import java.util.ArrayList;

public class Purse {

   private ArrayList<String> coins;
  
   public Purse() {
       coins = new ArrayList<String>();
   }

   public void addCoin(String nameofCoin)
   {
      
   }
  
  
   public String toString()
   {
return coins.toString();  
   }
  
   public String reverse()
{
ArrayList<String> pl = new ArrayList<String>(coins);
Collections.reverse(pl);
return pl.toString();
}
}

//TEST PURSE CLASS:

public class TestPurse {

   public static void main(String[] args) {
      
       Purse pu = new Purse();
       pu.addCoin("Dime");
       pu.addCoin("Dime");
       pu.addCoin("Nickel");
       pu.addCoin("Quarter");
      
       System.out.println(pu);
       System.out.println("Expected the Purse: Dime, Dime, Nickel, Quarter");
       System.out.println(pu.reverse());
      

   }

}

Solutions

Expert Solution

//Java code (Try this solution)

import java.util.Collections;
import java.util.ArrayList;

public class Purse {

    private ArrayList<String> coins;

    //Constructor
    public Purse() {
        
        coins = new ArrayList<String>();
    }

    public void addCoin(String nameofCoin)
    {
        //add coin to the arrayList coins using add()
        coins.add(nameofCoin);
    }


    public String toString()
    {
        String result="";
        for (String coin:coins
             ) {
            result+=coin+", ";
        }
        return result;
    }

    public String reverse()
    {
        ArrayList<String> pl = new ArrayList<String>(coins);
        Collections.reverse(pl);
        return pl.toString();
    }
}

//=========================================

public class TestPurse {

    public static void main(String[] args) {

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

        System.out.println(pu);
        System.out.println("Expected the Purse: Dime, Dime, Nickel, Quarter");
        System.out.println(pu.reverse());


    }

}

//Output

//If you need any help regarding this solution ........... please leave a comment ........ thanks


Related Solutions

Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
you have a purse which contains six coins, each coin is either a penny , a...
you have a purse which contains six coins, each coin is either a penny , a two pence coin, a ten pence coin or a twenty pence coin. find the expected total value of all the coins in the purse.
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's...
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's and JSP 3rd Edition. I need help modifying some code. I will post the code I was told to open that needs to be modified below. In this exercise, you'll enhance the Future Value application to store the amount and interest rate in the user's session. That way, the user can experiment with different numbers of years to see the value of his or...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT