how wTakes in a coin name as part of the constructor and stores it in a private
ID: 3827432 • Letter: H
Question
how wTakes 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 value of the coin
A method that returns the coins name via the parent class And any other methods that should be required.
Create a Pocket class that includes the following:
An ArrayList of type coin to manage coins.(You MUST use an ARRAYLIST) A method that allows a coin to be added
A method that allows a coin to be removed
A method that returns a count of the coins in your arraylist by coin type.
A method that returns a total value of all the coins in your arraylist
Create a NoSuchCoin class that will act as your custom exception (see pg.551 in book). Create a PocketMain class that includes the following :
Creates an object of type pocket to be used below
A main method that prompts the user with a menu of options to include:
Add a coin (must specify type) Remove a coin (must specify type) Display a coin count
Display a total value in the pocket An ability to exit the program
This method must handle poor user input somehow using your custom exception
How would I go about on starting this code?
Explanation / Answer
Here is your code: -
Coin.java
public abstract class Coin {
private String name;
public Coin(String s) {
this.name = s;
}
public String getName() {
return this.name;
}
public abstract Integer getValue();
}
Penny.java
public class Penny extends Coin {
private int value;
Penny(String n) {
super(n);
this.value = 1;
}
@Override
public Integer getValue() {
return this.value;
}
public String getName() {
return super.getName();
}
}
Nickle.java
public class Nickle extends Coin {
private int value;
Nickle(String n) {
super(n);
this.value = 5;
}
@Override
public Integer getValue() {
return this.value;
}
public String getName() {
return super.getName();
}
}
Dime.java
public class Dime extends Coin {
private int value;
Dime(String n) {
super(n);
this.value = 10;
}
@Override
public Integer getValue() {
return this.value;
}
public String getName() {
return super.getName();
}
}
Quarter.java
public class Quarter extends Coin {
private int value;
Quarter(String n) {
super(n);
this.value = 10;
}
@Override
public Integer getValue() {
return this.value;
}
public String getName() {
return super.getName();
}
}
Pocket.java
import java.util.ArrayList;
import java.util.Iterator;
public class Pocket {
private ArrayList<Coin> coins;
Pocket() {
this.coins = new ArrayList<>();
}
public void addCoin(Coin c) {
this.coins.add(c);
}
public void removeCoin(Coin c) {
for (int i = 0; i < this.coins.size(); i++) {
if (this.coins.get(i).getName().equals(c.getName())) {
this.coins.remove(i);
break;
}
}
}
public int getCoinCount(String s) {
int count = 0;
Iterator<Coin> i = this.coins.iterator();
Coin c;
while (i.hasNext()) {
c = i.next();
if (s.equals("penny") && (c instanceof Penny)) {
count++;
} else if (s.equals("nickle") && (c instanceof Nickle)) {
count++;
} else if (s.equals("dime") && (c instanceof Dime)) {
count++;
} else if (s.equals("quarter") && (c instanceof Quarter)) {
count++;
}
}
return count;
}
public int totalValue() {
int sum = 0;
Iterator<Coin> i = this.coins.iterator();
while (i.hasNext()) {
sum = sum + i.next().getValue();
}
return sum;
}
}
NoSuchCoinException.java
public class NoSuchCoinException extends Exception {
NoSuchCoinException(String msg) {
super(msg);
}
}
PocketMain.java
import java.util.Scanner;
public class PocketMain {
public static void main(String[] args) {
Pocket myPocket = new Pocket();
int choice = -1;
int coinType;
Scanner in = new Scanner(System.in);
while (choice != 5) {
System.out.println("Main Menu.Enter the desired choice number: -");
System.out.println("1. Add a coin.");
System.out.println("2. Remove a coin.");
System.out.println("3. Display coins count.");
System.out.println("4. Display Total Value of Pocket.");
System.out.println("5. Exit");
choice = in.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the type of coin to be added (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter): ");
try {
coinType = in.nextInt();
if (coinType == 1) {
Coin c = new Penny("penny");
myPocket.addCoin(c);
} else if (coinType == 2) {
Coin c = new Nickle("nickle");
myPocket.addCoin(c);
} else if (coinType == 3) {
Coin c = new Dime("dime");
myPocket.addCoin(c);
} else if (coinType == 4) {
Coin c = new Quarter("quarter");
myPocket.addCoin(c);
} else {
throw new NoSuchCoinException("Please enter a valid coin type");
}
System.out.println("Coin Added Successfully.");
} catch (NoSuchCoinException e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
break;
case 2:
System.out.println("Enter the type of coin to be removed (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter): ");
try {
coinType = in.nextInt();
if (coinType == 1) {
if(myPocket.getCoinCount("penny") > 0) {
Coin c = new Penny("penny");
myPocket.removeCoin(c);
} else {
System.out.println("There is no such coin present for this coin type.");
}
} else if (coinType == 2) {
if(myPocket.getCoinCount("nickle") > 0) {
Coin c = new Penny("nickle");
myPocket.removeCoin(c);
} else {
System.out.println("There is no such coin present for this coin type.");
}
} else if (coinType == 3) {
if(myPocket.getCoinCount("dime") > 0) {
Coin c = new Penny("dime");
myPocket.removeCoin(c);
} else {
System.out.println("There is no such coin present for this coin type.");
}
} else if (coinType == 4) {
if(myPocket.getCoinCount("quarter") > 0) {
Coin c = new Penny("quarter");
myPocket.removeCoin(c);
} else {
System.out.println("There is no such coin present for this coin type.");
}
} else {
throw new NoSuchCoinException("Please enter a valid coin type");
}
System.out.println("Coin Removed Successfully.");
} catch (NoSuchCoinException e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
break;
case 3:
System.out.println("Count of Penny : "+myPocket.getCoinCount("penny"));
System.out.println("Count of nickle : "+myPocket.getCoinCount("nickle"));
System.out.println("Count of dime : "+myPocket.getCoinCount("dime"));
System.out.println("Count of quarter : "+myPocket.getCoinCount("quarter"));
break;
case 4:
System.out.println("Total value in pocket:"+myPocket.totalValue()+" Cents.");
break;
case 5:
break;
}
}
in.close();
}
}
Sample run: -
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
1
Enter the type of coin to be added (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter):
1
Coin Added Successfully.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
1
Enter the type of coin to be added (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter):
2
Coin Added Successfully.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
1
Enter the type of coin to be added (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter):
3
Coin Added Successfully.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
2
Enter the type of coin to be removed (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter):
2
Coin Removed Successfully.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
3
Count of Penny : 1
Count of nickle : 0
Count of dime : 1
Count of quarter : 0
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
4
Total value in pocket:11 Cents.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
1
Enter the type of coin to be added (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter):
4
Coin Added Successfully.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
3
Count of Penny : 1
Count of nickle : 0
Count of dime : 1
Count of quarter : 1
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
4
Total value in pocket:21 Cents.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
1
Enter the type of coin to be added (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter):
2
Coin Added Successfully.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
3
Count of Penny : 1
Count of nickle : 1
Count of dime : 1
Count of quarter : 1
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
4
Total value in pocket:26 Cents.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
2
Enter the type of coin to be removed (1- Penny, 2 - Nickle, 3 - Dime, 4- quarter):
3
Coin Removed Successfully.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
3
Count of Penny : 1
Count of nickle : 1
Count of dime : 0
Count of quarter : 1
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
4
Total value in pocket:16 Cents.
Main Menu.Enter the desired choice number: -
1. Add a coin.
2. Remove a coin.
3. Display coins count.
4. Display Total Value of Pocket.
5. Exit
5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.