Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Read the Instructions, I have made the COIN CLASS but im confused about t

ID: 3549881 • Letter: P

Question

Please Read the Instructions,


I have made the COIN CLASS  but im confused about the WALLET CLASS  and got many errors.


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 as follows:

Attributes:        total value of all coins (in cents) stored in the wallet

Methods:          Constructor (that sets the total value to zero)

                        addCoin (that adds a given Coin object's value to the total value)

                        removeCoin (that removes a given Coin object's value from the total value)

                        toString method

Test the classes by creating a Demo, and adding and removing coins from it.

Explanation / Answer


class Coin
{
//Attributes: name and value of a coin (valid names and values are Toonie(200), Loonie(100), Quarter(25), Dime(10) and Nickel(5)).
private String name;
private int value;
//Methods : Constructor that sets the name and value;
public Coin(String name,int value)
{
this.name = name;
this.value = value;
}
//Get methods
public String getName() { return name; }
public int getValue() { return value; }
//toString method
public String toString() { return name + " " + value; }
}
//Create another class called Wallet as follows:
class Wallet
{
// Attributes: total value of all coins (in cents) stored in the wallet
private double total_value;
//Methods: Constructor (that sets the total value to zero)
public Wallet()
{
total_value = 0;
}
//addCoin (that adds a given Coin object's value to the total value)
public void addCoin(Coin C)
{
total_value = total_value + C.getValue();
}
//removeCoin (that removes a given Coin object's value from the total value)
public void removeCoin(Coin C)
{
total_value = total_value - C.getValue();
}
//toString method
public String toString()
{
return ""+total_value;
}
}
//Test the classes by creating a Demo, and adding and removing coins from it.

public class tester
{
public static void main(String[] args)
{
//Toonie(200), Loonie(100), Quarter(25), Dime(10) and Nickel(5)
Coin Nickel = new Coin("Nickel",5);
Coin Dime = new Coin("Dime",10);
Coin Quarter = new Coin("Quarter",25);
Coin Loonie = new Coin("Loonie",100);
Coin Toonie = new Coin("Toonie",200);
Wallet new_Wallet = new Wallet();
new_Wallet.addCoin(Loonie);
System.out.println("Now Wallet contains " + new_Wallet);
new_Wallet.addCoin(Quarter);
new_Wallet.addCoin(Quarter);
new_Wallet.addCoin(Dime);
System.out.println("Now Wallet contains " + new_Wallet);
new_Wallet.removeCoin(Nickel);
System.out.println("Now Wallet contains " + new_Wallet);
}
}