Results 1 to 3 of 3
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default Problem calling classes to flip coin x number of times and record heads or tails

    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


    Java Code:
    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);
        }
        
    }
    Java Code:
    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;
     
        }
        
        
    }
    Java Code:
    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());
            }
    }

  2. #2
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    I figured this one out so no need to respond. Coin was only being checked once because it was outside the loop. Also since MonetaryCoin is a subclass of Coin, I did not need to create a Coin object and call my isHeads method to determine heads or tails. Instead I just called it through MonetaryCoin and since that is in the loop a new decision was made each time.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    for(int p = 0; p < numPennies; p++){
        MonetaryCoin mc = new MonetaryCoin(pennyValue);
        // You caught this one:
        coinFace = mc.isHeads();
        ...
        // (coinFace = true) resets coinFace to true every time.
        if(coinFace == true){
        // or you can write it as
        if(coinFace) {

Similar Threads

  1. [SOLVED] Problem with extending classes...
    By Bizmark in forum New To Java
    Replies: 4
    Last Post: 04-07-2008, 11:21 PM
  2. Replies: 2
    Last Post: 02-16-2008, 08:52 PM
  3. presse 1 menuitem execute 4 times the same JFrame
    By marcvb in forum New To Java
    Replies: 0
    Last Post: 12-27-2007, 09:48 PM
  4. Calling jar classes from java executable
    By SteM in forum Advanced Java
    Replies: 1
    Last Post: 11-27-2007, 08:21 AM
  5. Problem calling another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 10-25-2007, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •