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

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...
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...
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...
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...
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.
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once The program should output the average, largest, and smallest of 5 numbers. You must implement the methods listed below in your program. static float getAverage(int[] data) {...} static int getLargest(int[] data) {...} static int getSmallest(int[] data) {...}
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of...
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G, T}. Implement a special hash table to store all short DNA segments of certain length (e.g., 20) found in a long DNA sequence. Every DNA segment is stored as a (key, value) pair, where the key is the sequence of the segment, and the value is the position of the segment in the DNA. For example given a DNA sequence of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT