I'm working on a class project that has 3 classes. CoinFlip is the driver class, Coin is a class that flips the coin and determines heads or tails. The third class MonetaryCoin, which is a subclass of Coin, determines the value of the coin. The program is setup to prompt the user for the number of pennies, nickels, dimes and quarters to flip. Based on the flips I need to output the total value of all coins, the total value of the coins that came up heads and the total value of the coins that came up tails. I have tested the individual parts of the classes and know that the coin is flipped properly and that it records heads or tails correctly based on the flip. The problem I'm having now is that I cannot get the coins to flip and the side it lands on to be recorded properly so that I can output the results. everything is now coming up as heads no matter what. I'm thinking that I'm either calling to the other classes incorrectly or not calling something when I need to. I'm relatively new to Java so it is probably something easy that I'm missing but would appreciate any help that could be provided to point me in the right direction. My code is below.
Thanks
import javax.swing.JOptionPane;
public class CoinFlip {
public static void main(String[] args) {
double pennyValue = 0.01;
double nickelValue = 0.05;
double dimeValue = 0.10;
double quarterValue = 0.25;
double totalPennyValue = 0.00;
double totalNickelValue = 0.00;
double totalDimeValue = 0.00;
double totalQuarterValue = 0.00;
double totalCoinValue = 0.00;
double coinValue = 0.00;
String pennies = JOptionPane.showInputDialog("How many pennies would you\n" +
" like to create?\n\n");
int numPennies = Integer.parseInt(pennies);
String nickels = JOptionPane.showInputDialog("How many nickels would you\n" +
" like to create?\n\n");
int numNickels = Integer.parseInt(nickels);
String dimes = JOptionPane.showInputDialog("How many dimes would you\n" +
" like to create?\n\n");
int numDimes = Integer.parseInt(dimes);
String quarters = JOptionPane.showInputDialog("How many quarters would you\n" +
" like to create?\n\n");
int numQuarters = Integer.parseInt(quarters);
totalCoinValue = ((pennyValue * numPennies) + (nickelValue * numNickels) + (dimeValue * numDimes) + (quarterValue * numQuarters));
System.out.println(totalCoinValue);
Coin c = new Coin();
boolean coinFace;
int totalHeads = 0;
int totalTails = 0;
double headsValue = 0.00;
double tailsValue = 0.00;
for(int p = 0; p < numPennies; p++){
MonetaryCoin mc = new MonetaryCoin(pennyValue);
coinFace = c.isHeads();
if(coinFace = true){
totalHeads++;
headsValue = headsValue + pennyValue;
}
if(coinFace != true){
totalTails++;
tailsValue = tailsValue + pennyValue;
}
}
System.out.println(totalHeads);
System.out.println(headsValue);
System.out.println(totalTails);
System.out.println(tailsValue);
}
}
public class Coin {
public static final int HEADS = 0;
public static final int TAILS = 1;
private int face;
public Coin() {
coinFlip();
}
public void coinFlip() {
face = (int) (Math.random() * 2);
}
boolean isHeads() {
if(face == 0){
return true;
}
return false;
}
public String toString(){
String faceName = "tails";
if(face == 0){
faceName = "heads";
}
return faceName;
}
}
public class MonetaryCoin extends Coin{
private double value;
public MonetaryCoin(double coinValue) {
super();
setValue(coinValue);
}
public void setValue(double coinValue){
value = coinValue;
}
public double getValue() {
return value;
}
public String toString(){
return String.format("%s: %s\n%s: %s\n",
"Side facing up", super.toString(),
"Value of coin", getValue());
}
}