Create a class called Coin as follows Attributes name and va

Create a class called Coin as follows: Attributes: name and value of a coin (valid names and values are Toonie(200), Loonie(100), Quarter(25), Dime(10) and Nickel(5)). Methods : Constructor that sets the name and value; Get methods toString method

*I am using JGrasp so please make sure it is compatible

*Please comment throughout code

Exercise 2: Create a class called Coin as follows: Attributes name and value of a coin (valid names and values are Toonie(200), Loonie(100), Quarter(25), Dime(10 and Nickel(5). Methods Constructor that sets the name and value, Get methods toString method Create another class called Wallet that is aggregated from the Coin class as follows: Attributes An array of Coin objects capacity (how many coin objects it can hold an integer value) num (current number of coin objects in the wallet -an integer value) total value of all Coin objects (in cents) stored in the wallet (an integer value) Methods: Constructor (that sets the total value to zero, num to zero, the capacity to the given value, and creates an array of Coin objects of size equal to the capacity) addCoin (that adds a given Coin object to the Coin object array and updates the num and total value) removeCoin (that removes the last Coin object from the array and returns it, it should also update the total value and num toString method Note: addCoin and removeCoin methods should have the appropriate error checks Test the classes by creating a Wallet, and adding and removing coins from it.

Solution

WalletTest.java


public class WalletTest {

  
   public static void main(String[] args) {
           Coin c1 = new Coin(\"Toonie\", 200);
           Coin c2 = new Coin(\"Loonie\", 100);
           Coin c3 = new Coin(\"Quarter\", 25);
           Coin c4 = new Coin(\"Dime\", 10);
           Coin c5 = new Coin(\"Nickle\", 5);
           Wallet w = new Wallet(5);
           w.addCoin(c1);
           w.addCoin(c2);
           w.addCoin(c3);
           w.addCoin(c4);
           w.addCoin(c5);
           System.out.println(w.toString());
           w.removeCoin();
           System.out.println(w.toString());
           w.removeCoin();
           System.out.println(w.toString());
           w.addCoin(c5);
           System.out.println(w.toString());
   }

}

Wallet.java


import java.util.Arrays;

public class Wallet{
   Coin c[];
   private int capacity;
   private int num;
   private int totalValue;
   public Wallet(int capacity){
       c = new Coin[capacity];
       totalValue = 0;
       num = 0;
       this.capacity = capacity;
   }
   public void addCoin(Coin c1){
       c[num++] = c1;
       totalValue = totalValue + c1.getValue();
   }
   public Coin removeCoin(){
       num--;
       Coin c1 = c[num];
       c[num] = null;
       if(totalValue - c1.getValue() >= 0){
       totalValue = totalValue - c1.getValue();
       return c1;
       }
       else{
           System.out.println(\"Con not remove the coin\");
           num++;
           return null;
       }
   }
  
   public int getTotalValue() {
       return totalValue;
   }
   public String toString(){
       return \"Coins are: \"+Arrays.toString(c)+\" Total Value: \"+totalValue;
   }
}

Coin.java


public class Coin {
   private String name;
   private int value;
   public Coin(String name, int value) {
       this.name = name;
       this.value = value;
   }
   public String getName() {
       return name;
   }
   public int getValue() {
       return value;
   }
   public String toString(){
       return name+\"(\"+value+\")\";
   }
  
}

Output:

Coins are: [Toonie(200), Loonie(100), Quarter(25), Dime(10), Nickle(5)] Total Value: 340
Coins are: [Toonie(200), Loonie(100), Quarter(25), Dime(10), null] Total Value: 335
Coins are: [Toonie(200), Loonie(100), Quarter(25), null, null] Total Value: 325
Coins are: [Toonie(200), Loonie(100), Quarter(25), Nickle(5), null] Total Value: 330

Create a class called Coin as follows: Attributes: name and value of a coin (valid names and values are Toonie(200), Loonie(100), Quarter(25), Dime(10) and Nick
Create a class called Coin as follows: Attributes: name and value of a coin (valid names and values are Toonie(200), Loonie(100), Quarter(25), Dime(10) and Nick

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site