Java 1 Coin Assignments

BACKGROUND

- Make sure that you have 3 constructors. (See handout 6.)
- Be sure that your program can successfully flip coins, returning "heads" or "tails"
- ADD an accessor called getValue() that returns the value of a coin.
- Assignments 1 and 2 are simple in scope.
- Assignment 3 requires the use of ArrayLists and also creating Random numbers within a particular range.
- Assignment 4 (Extra Credit) is a bit more involved than those three.

PROCESS TO FOLLOW

- Use correct style and write comments! (Remember when and WHEN NOT to indent.)
-
Use handouts 6 and 7 as guides, as well as CoinTester and CoinTesterAL (for assignment 3.)
- Remember that you can loop through an ArrayList with the following header: for(int i = 0; i < listName.size(); i++)
- Work with a neighbor.
- Do each part in PIECES. Compile and run your program FREQUENTLY.

YOUR TASK

Write each application class in the Coin project with the names C1, C2, etc. The output from the terminal window should be copied and pasted AT THE TOP your application program. If you do the extra credit, call your class Purse.


C1 - Write an application program that creates a penny, nickel and dime and flips them 100 times. Create the penny using the default constructor. Create the nickel using the constructor that takes a String as the parameter and create the dime using the constructor that takes a double as the parameter. You should have four int variables: numLoops should be declared outside the loop (and set equal to 100). Your program should also have numHeadsPenny, numHeadsNickel, numHeadsDime, all should be declared outside the loop (and initialized to 0.) Count the number of heads for each coin. Note: You will NOT create a variable for the number of tails. (Math will assist you in figuring this out.)

Program Output: The number of heads and tails that came up for each coin.

SAMPLE OUTPUT
==================This is program C1==================
Penny: 57 Heads and 43 tails.
Nickel: 47 Heads and 53 tails.
Dime: 58 Heads and 42 tails.
======================================================



C2 - Write an application program that creates two pennies and flips them 1000 times. Count the number of times they both came up heads ON THE SAME TOSS (Use a compound conditional.). In other words, inside a for loop, flip each one and ask if on that flip they both were heads.

Program Output: The number of times they were BOTH heads (on the same flip.) What percent that was. (See box below.)

SAMPLE OUTPUT
==================This is program C2==================
They both came up heads: 263 times out of 1000 tosses.
That is the equivalent of 26.3% of the time.
======================================================



C3 - Write an application program that creates a random handful of coins (an ArrayList) using the following algorithm:

1. Choose a random number between 2 and 10 for the number of coins.
2. In a loop for that many times, choose another random number between 1 and 5. You will need to store this number in a variable. DECLARE this variable outside the loop! You will then re-assign its value every iteration of the for loop. (In other words, don't write int typeOfCoin = ________; in the loop, just have typeOfCoin = ________; (remove the data type declaration.)
3. If this number is a 1 create a penny, if it is a 2 create a nickel, a 3 create a dime, a 4 create a quarter and a 5 create a half-dollar.
4. AFTER THE LOOP, do the printing. (Do NOT do it inside the for loop that creates the coins.)

Program Output: How many coins will be added. The names of each coin. The total value of your handful of coins. (Use DecimalFormat [in the Coin class] for correct printing of the dollar amount

SAMPLE OUTPUT
==================This is program C3==================
3 coins will be added to your handful.
I am a penny worth $0.01 dollars.
I am a quarter worth $0.25 dollars.
I am a penny worth $0.01 dollars.
Your coins are worth a total of $0.27
=====================================================



C4 (extra credit) Write a Purse class that can simplify what the main method has been doing. Purse should have a private data field, an ArrayList, itsCoins. Its constructor will simply create the Arraylist. It should have methods

public void addPennies(int count) // Each add method should create new coins of the proper
public void addNickels(int count) // name and value
public void addDimes(int count)
public void addQuarters(int count) and
public int getValue() // returns the amount of money in the purse in cents

Use this for your main method (C4)

Purse shoulderbag = new Purse();
shoulderbag.addPennies (8);
shoulderbag.addNickels(3);
shoulderbag.addDimes(4);
shoulderbag.addQuarters(6);
System.out.println(“The purse contains “ + shoulderbag.getValue() + “ cents” );
//add code here to print out value in dollars and cents ( $2.13)