In: Computer Science
Java Project
Tasks:
Create a Coin.java class that includes the following:
Takes 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
YOU MUST COMMENT YOUR CODE SO I KNOW WHAT IS HAPPENING!!!
Deliverables:
Pocket.java
Coin.java
Nickle.java
Penny.java
Dime.java
Quarter.java
PocketMain.Java
NoSuchCoin.java
Answer:-
Hello, I have written the required code in java. Please find it below. I have include all the java files code.
JAVA CODE:-
Coin.java
public abstract class Coin{
private String name ;
/* Constructor*/
public Coin(String Name){
name = Name;
}
public String GetName(){
return name;
}
/* abstract methods for getters ans setters*/
public abstract double getValue();
public abstract void setValue(double a);
}
Penny.java
class Penny extends Coin {
private double value;
/* Constructor*/
public Penny(String name){
super(name);
}
public double getValue(){
return value;
}
public void setValue(double value)
{
this.value=value;
}
}
Nickle.java
class Nickle extends Coin {
private double value;
/* Constructor*/
public Nickle(String name){
super(name);
}
public double getValue(){
return value;
}
public void setValue(double value)
{
this.value=value;
}
}
Dime.java
class Dime extends Coin {
private double value;
/* Constructor*/
public Dime(String name){
super(name);
}
public double getValue(){
return value;
}
public void setValue(double value)
{
this.value=value;
}
}
Quarter.java
class Quarter extends Coin {
private double value;
/* COnstructor*/
public Quarter(String name){
super(name);
}
public double getValue(){
return value;
}
public void setValue(double value)
{
this.value=value;
}
}
Pocket.java
import java.util.ArrayList; // import the ArrayList class
import java.util.*;
import java.util.stream.*;
class Pocket{
ArrayList<Coin> coins = new
ArrayList<Coin>();
/* method to add a coin*/
public void AddCoin(Coin coin){
this.coins.add(coin);
}
/* method to remove a coin*/
public void RemoveCoin(String type){
for(int i=0;i<this.coins.size();i++)
{
Coin listval=coins.get(i);
if(listval instanceof Penny && type=="Penny")// we are
checking if the given String is of type Penny
{
this.coins.remove(listval);
}
if(listval instanceof Nickle && type=="Nickle")// we are
checking if the given String is of type Nickle
{
this.coins.remove(listval);
}
if(listval instanceof Dime && type=="Dime")// we are
checking if the given String is of type Dime
{
this.coins.remove(listval);
}
if(listval instanceof Quarter && type=="Quarter")// we are
checking if the given String is of type Quarter
{
this.coins.remove(listval);
}
}
}
/* method to count the number of coins of a given type*/
public int countCoin(String type){
int sum=0;
for(int i=0;i<this.coins.size();i++)
{
Coin listval=coins.get(i);
if(listval instanceof Penny && type=="Penny")// we are
checking if the given String is of type Penny
{
sum++;
}
if(listval instanceof Nickle && type=="Nickle")// we are
checking if the given String is of type Nickle
{
sum++;
}
if(listval instanceof Dime && type=="Dime")// we are
checking if the given String is of type Dime
{
sum++;
}
if(listval instanceof Quarter && type=="Quarter")// we are
checking if the given String is of type Quarter
{
sum++;
}
}
return sum;
}
/* method to get the total values of coin in list*/
public double totalValue()
{
double total=0;
for(int
i=0;i<this.coins.size();i++)
{
Coin listval =
coins.get(i);
total+=listval.getValue();
}
return total;
}
}
PocketMain.java
import java.util.Scanner;
class PocketMain
{
public static void main(String [] args)
{
Scanner in = new
Scanner(System.in);
Pocket pocket = new Pocket();
/* Showing a menu to User*/
int input=0;
do{
System.out.println("Press 1 to Add
a coin");
System.out.println("Press 2 to
Remove a coin");
System.out.println("Press 3 to
Display a coin count");
System.out.println("Press 4 to
Display a total value in the pocket");
System.out.println("Press 5 to
Exit");
/*Taking input from User*/
input = in.nextInt();
/* if User enters 1, then we will add a coin in list*/
if(input == 1){
System.out.println("Press 1 to add a Penny");
System.out.println("Press 2 to add
Nickle");
System.out.println("Press 3 to add
Dime");
System.out.println("Press 4 to add
Quarter");
int addInput = 0;
addInput = in.nextInt();
if(addInput == 1){
Coin p = new Penny("myPenny");
p.setValue(10);
pocket.AddCoin(p);
}
if(addInput == 2){
Coin p = new Nickle("myNickle");
p.setValue(20);
pocket.AddCoin(p);
}
if(addInput == 3){
Coin p = new Dime("myDime");
p.setValue(30);
pocket.AddCoin(p);
}
if(addInput == 4){
Coin p = new Quarter("myQuarter");
p.setValue(40);
pocket.AddCoin(p);
}
}
/* if User enters 2, then we will remove a coin in list*/
if(input == 2){
System.out.println("Press 1 to remove a Penny");
System.out.println("Press 2 to
remove Nickle");
System.out.println("Press 3 to
remove Dime");
System.out.println("Press 4 to
remove Quarter");
int removeInput = 0;
removeInput = in.nextInt();
if(removeInput == 1){
pocket.RemoveCoin("Penny");
}
if(removeInput == 2){
pocket.RemoveCoin("Nickle");
}
if(removeInput == 3){
pocket.RemoveCoin("Dime");
}
if(removeInput == 4){
pocket.RemoveCoin("Quarter");
}
}
/* if User enters 3, then we will display the number of coins of
selected type*/
if(input == 3){
System.out.println("Press 1 for Penny coin count");
System.out.println("Press 2 for
Nickle coin count");
System.out.println("Press 3 for
Dime coin count");
System.out.println("Press 4 for
Quarter coin count");
int removeInput = in.nextInt();
if(removeInput == 1){
System.out.println("Number of coins of type Penny is
"+pocket.countCoin("Penny"));
}
if(removeInput == 2){
System.out.println("Number of coins of type Nickle is
"+pocket.countCoin("Nickle"));
}
if(removeInput == 3){
System.out.println("Number of coins of type Dime is
"+pocket.countCoin("Dime"));
}
if(removeInput == 4){
System.out.println("Number of coins of type Quarter is
"+pocket.countCoin("Quarter"));
}
}
/* if User enters 4, then we will display the total values of coin
in list*/
if(input==4)
{
System.out.println("Total value of coins in list is
"+pocket.totalValue() );
}
if(input>5)
{
System.out.println("Invalid input, Please try
again");
}
} while (input!=5);
}
}
Screenchots of code:-
Please refer to the screenshots of the code for properly understanding the indentation of the code.
Coin.java
Penny.java
Nickle.java
Dime.java
Quarter.java
Pocket.java
screenshot 1:-
screenshot 2:-
screenshot 3:-
screenshot 4:-
PocketMain.java
screenshot 1:-
screenshot 2:-
screenshot 3:-
screenshot 4:-
OUTPUT:-
I hope, it would help.
Thanks!