Question

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

Project 2

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 HAPPNEING!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Coin.java

--------------------

package coinFamily;

//coin class

public abstract class Coin {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Coin(String name) {

super();

this.name = name;

}

public abstract float getValue();

}

//child classes for coin

//coin values are in cents

//1 penny = 1 cent

class Penny extends Coin{

private int coinValue;

  

public Penny(String name) {

super(name);

coinValue = 1;

}

@Override

public float getValue() {

return coinValue;

}

}

class Nickle extends Coin{

private int coinValue;

  

public Nickle(String name) {

super(name);

coinValue = 5;

}

@Override

public float getValue() {

return coinValue;

}

}

class Dime extends Coin{

private int coinValue;

  

public Dime(String name) {

super(name);

coinValue = 10;

}

@Override

public float getValue() {

return coinValue;

}

}

class Quarter extends Coin{

private int coinValue;

  

public Quarter (String name) {

super(name);

coinValue = 25;

}

@Override

public float getValue() {

return coinValue;

}

}

NoSuchCoin.java

---------------------------------

package coinFamily;

//class extends exception class

public class NoSuchCoin extends Exception{

//create new exception

public NoSuchCoin(String s){

super(s);

}

  

}

PocketClass.java

--------------------------------

package coinFamily;

import java.util.ArrayList;

import java.util.Scanner;

//pocket class

public class PocketClass {

//coin arraylist

private ArrayList<Coin> coins;

//default constructor

public PocketClass() {

super();

coins = new ArrayList<>();

}

//add coin to list

void addCoin(Coin coin){

coins.add(coin);

}

//remove coin by name

void removeCoin(Coin coin){

for (Coin c : coins ){

if(c.getName().equalsIgnoreCase(coin.getName())){

coins.remove(c);

return;

}

}

}

//get count by coin name

int getCount(Coin coin){

int count=0;

for (Coin c : coins ){

if(c.getName().equalsIgnoreCase(coin.getName())){

count++;

}

}

System.out.println("Coin name : "+coin.getName()+" Count : "+count);

return count;

}

//get total coin value

int getTotalValue(){

int totalValue=0;

for (Coin c : coins ){

totalValue+=c.getValue();

}

System.out.println("Total coin value is : "+totalValue+" Cents");

return totalValue;

}

//main method

public static void main(String[] args){

//scanner for int input

Scanner sc = new Scanner(System.in);

//scanner for string input

Scanner scString = new Scanner(System.in);

//user choice

int choice;

//for name

String name;

//pocket class

PocketClass pocket = new PocketClass();

do{

System.out.println("Enter you coice ");

System.out.println("1 : Add coin");

System.out.println("2 : Remove coin");

System.out.println("3 : Display coin count");

System.out.println("4 : Display total value in the pocket");

System.out.println("Anything else to exit.");

choice = sc.nextInt();

if(choice==1){

System.out.println("Enter coin name : ");

name = scString.nextLine();

if(name.equalsIgnoreCase("penny")){

Penny p = new Penny(name);

pocket.addCoin(p);

}else if(name.equalsIgnoreCase("Nickle")){

Nickle n = new Nickle(name);

pocket.addCoin(n);

}else if(name.equalsIgnoreCase("Dime")){

Dime d = new Dime(name);

pocket.addCoin(d);

}else if(name.equalsIgnoreCase("Quarter")){

Quarter q = new Quarter(name);

pocket.addCoin(q);

}else{

try {

throw new NoSuchCoin("Not a valid coin");

} catch (NoSuchCoin e) {

e.printStackTrace();

}

}

}else if(choice==2){

System.out.println("Enter coin name : ");

name = scString.nextLine();

if(name.equalsIgnoreCase("penny")){

Penny p = new Penny(name);

pocket.removeCoin(p);

}else if(name.equalsIgnoreCase("Nickle")){

Nickle n = new Nickle(name);

pocket.removeCoin(n);

}else if(name.equalsIgnoreCase("Dime")){

Dime d = new Dime(name);

pocket.removeCoin(d);

}else if(name.equalsIgnoreCase("Quarter")){

Quarter q = new Quarter(name);

pocket.removeCoin(q);

}else{

try {

throw new NoSuchCoin("Not a valid coin");

} catch (NoSuchCoin e) {

e.printStackTrace();

}

}

}else if(choice==3){

System.out.println("Enter coin name : ");

name = scString.nextLine();

if(name.equalsIgnoreCase("penny")){

Penny p = new Penny(name);

pocket.getCount(p);

}else if(name.equalsIgnoreCase("Nickle")){

Nickle n = new Nickle(name);

pocket.getCount(n);

}else if(name.equalsIgnoreCase("Dime")){

Dime d = new Dime(name);

pocket.getCount(d);

}else if(name.equalsIgnoreCase("Quarter")){

Quarter q = new Quarter(name);

pocket.getCount(q);

}else{

try {

throw new NoSuchCoin("Not a valid coin");

} catch (NoSuchCoin e) {

e.printStackTrace();

}

}

}else if(choice==4){

pocket.getTotalValue();

}

//repeat until choice is in the range

}while(choice>=1 && choice<=4);

sc.close();

scString.close();

}

}

output:

Console 3 PocketClass [Java Application] C:\Program Files\Javare7\bin\javaw.exe (Mar 10, 2018, 3 Enter you coice 1: Add coin

Total coin value is: 26 Cents Enter you coice 1: Add coin 2: Remove coin 3: Display coin count 4:Display total value inthe po

Add a comment
Know the answer?
Add Answer to:
Project 2 Tasks: Create a Coin.java class that includes the following:             Takes in a coin...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Money Lab using arraylists in Java Using the Coin class below create a program with the...

    Money Lab using arraylists in Java Using the Coin class below create a program with the following requirements Create an arraylist that holds the money you have in your wallet. You will add a variety of coins(coin class) and bills (ones, fives, tens *also using the coin class) to your wallet arraylist. Program must include a loop that asks you to purchase items using the coins in your wallet. When purchasing items, the program will remove coins from the arraylist...

  • In Java Create a class Worker. Your Worker class should include the following attributes as String...

    In Java Create a class Worker. Your Worker class should include the following attributes as String variables: Name, Worker ID, Worker Address, Worker City, Worker State Create the constructor that initializes the above worker attributes. Then make another class name HourlyWorker that inherits from the Worker class.   HourlyWOrker has to use the inherited parent class variables and add in hourlySalary and billableHours. Your HourlyWorker class should contain a constructor that calls the constructor from the Worker class to initialize the...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

  • Step 1: Declare a base class called Coin that contains a Boolean data element called face,...

    Step 1: Declare a base class called Coin that contains a Boolean data element called face, and a random generator to fill the face element with a Boolean value (true or false). True = heads, false = tails. The class needs a default constructor, a method to flip the coin, a method to return the face element, and a toString to display “Heads” if the face is true and “Tails” if not. Step 2: Declare a subclass of Coin that...

  • (To be written in Java code) Create a class named CollegeCourse that includes the following data...

    (To be written in Java code) Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display()...

  • Create another Java class named EmployeeMain within the same project, which includes the main method. a....

    Create another Java class named EmployeeMain within the same project, which includes the main method. a. Include another class named Employee in the same Java file. This class has the following instance variables and instance methods. Define all the instance/static variables with private access modifier and constructors, instance/static methods with public access modifier. Instance Variables empID: int employeeName: String basicSalary: double Constructor Set the value for empID. Instance Methods Get and set methods for all instance variables. displayEmployee: Display the...

  • Ive got this started but im really struggling its a c# program, I have to modify...

    Ive got this started but im really struggling its a c# program, I have to modify the class  Purse  to implement the interface ICloneable . create a main program that demonstrates that the Purse method Clone works. using System; using System.Collections; namespace TestingProject {    /// <summary>    /// A coin with a monetary value.    /// </summary>    public class Coin   {        ///   Constructs a coin.        ///   @param aValue the monetary value of the coin       ...

  • In Java Kindly be sure to take in user input and store the 3 worker classes...

    In Java Kindly be sure to take in user input and store the 3 worker classes in an ArrayList Create a class Worker. Your Worker class should include the following attributes as String variables: Name, Worker ID, Worker Address, Worker City, Worker State Create the constructor that initializes the above worker attributes. Then make another class name HourlyWorker that inherits from the Worker class.   HourlyWorker has to use the inherited parent class variables and add in hourlySalary and billableHours. Your...

  • JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The ...

    JAVA PROGRAMMING***** Please provide screenshots of output and write a tester class to test. The starter code has been provided below, just needs to be altered. For this project you will implement the Memento Design Pattern. To continue on with the food theme, you will work with Ice Cream Cones. You will need to use AdvancedIceCreamCone.java. Create at least three Ice Cream Cones - one with chocolate ice cream, one with vanilla ice cream and one with strawberry ice cream....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT