Create a NoSuchCoin class that will act as a custom exception. Coin.java public
ID: 3600319 • Letter: C
Question
Create a NoSuchCoin class that will act as a custom exception.
Coin.java
public abstract class Coin {
private String name;
public Coin(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Dime.java
public class Dime extends Coin {
private double value;
public Dime(String name, double value) {
super(name);
this.value = value;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
}
Nickel.java
public class Nickel extends Coin {
private double value;
public Nickel(String name, double value) {
super(name);
this.value = value;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
}
Penny.java
public class Penny extends Coin{
private double value;
public Penny(String name, double value) {
super(name);
this.value = value;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
}
Quarter.java
public class Quarter extends Coin {
private double value;
public Quarter(String name, double value) {
super(name);
this.value = value;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
}
Pocket.java
import java.util.ArrayList;
public class Pocket {
ArrayList<Coin> arl;
public Pocket() {
super();
this.arl = new ArrayList<Coin>();
}
public void addCoin(Coin c) {
arl.add(c);
}
public void removeCoin(Coin c) {
arl.remove(c);
}
public int countCoins(Coin c) {
int count = 0;
for (int i = 0; i < arl.size(); i++) {
if (c.getName().equals(arl.get(i).getName())) {
count++;
}
}
return count;
}
public double calTotalValue() {
double totVal = 0;
for (int i = 0; i < arl.size(); i++) {
if (arl.get(i) instanceof Dime) {
Dime d = (Dime) arl.get(i);
totVal += d.getValue();
} else if (arl.get(i) instanceof Nickel) {
Nickel n = (Nickel) arl.get(i);
totVal += n.getValue();
} else if (arl.get(i) instanceof Penny) {
Penny p = (Penny) arl.get(i);
totVal += p.getValue();
} else if (arl.get(i) instanceof Quarter) {
Quarter q = (Quarter) arl.get(i);
totVal += q.getValue();
}
}
return totVal;
}
}
PocketMain.java
import java.util.Scanner;
public class PocketMain {
public static void main(String[] args) {
Pocket p = new Pocket();
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner scan = new Scanner(System.in);
System.out.println("1.Add a coin (must specify type)");
System.out.println("2.Remove a coin (must specify type)");
System.out.println("3.Display a coin count");
System.out.println("4.Display a total value in the pocket");
System.out.println("5.Exit");
System.out.print("Enter Choice:");
int choice = scan.nextInt();
switch (choice) {
case 1: {
}
case 2: {
}
case 3: {
}
case 4: {
}
case 5: {
}
default: {
}
}
scan.close();
}
}
Explanation / Answer
All the custom exceptions must extend the class 'Exception'
We can setup the message for exception using constructors.
class NoSuchCoin extends Exception{
NoSuchCoin(){
super("No such coin exists");
}
NoSuchCoin(String name, double value){
super("No coin, "+name+" Exists with the value "+value);
}
}