Question

In: Computer Science

Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin...

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

Solutions

Expert Solution

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!


Related Solutions

JAVA Homework 1) Create a die class. This is similar to the coin class , but...
JAVA Homework 1) Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die. Altogether you will have...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following:...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following: a variable roof that holds type of roof (ex: convertible, hard-top,softtop) a variable doors that holds the car's number of doors(ex: 2,4) implement the changespeed method to add 20 to the speed each time its called add exception handling to the changespeed method to keep soeed under 65 implement the sound method to print "brooom" to the screen. create one constructor that accepts the...
Complete the following tasks: a. Create a class named Trip that includes four string variables: destination...
Complete the following tasks: a. Create a class named Trip that includes four string variables: destination (for example, “London”), means of transportation (for example, “air”), departure date (for example, “12/15/2015”), and trip's purpose (for example, “business”). Include two overloaded constructors. The default constructor sets each field to “XXX”. The nondefault constructor accepts four parameters—one for each field. Include two overloaded display() methods. The parameterless version displays all the Trip details. The second version accepts a string that represents a destination...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign4Test.java. Copy your code from Assignment 3 into the Student.java and StudentList.java. Assign4Test.java should contain the main method. In Student.java, add a new private variable of type Integer called graduationYear. In Student.java, create a new public setter method setGraduationYear to set the graduationYear. The method will have one parameter of type Integer. The method will set...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add...
Java (a) Create a class Router which stores the information of a router. It includes the...
Java (a) Create a class Router which stores the information of a router. It includes the brand, the model number (String) and the price (double, in dollars). Write a constructor of the class to so that the information mentioned is initialized when a Router object is created. Also write the getter methods for those variables. Finally add a method toString() to return the router information in the following string form. "brand: Linksys, model number: RVS4000, price: 1080.0" Copy the content...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT