Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-11-2007, 08:47 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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


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); } }
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; } }
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()); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-11-2007, 09:55 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-11-2007, 10:07 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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) {
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem with extending classes... Bizmark New To Java 4 04-08-2008 01:21 AM
Recursive Method ==> find how many times a value is repeated in an array NatNat New To Java 2 02-16-2008 10:52 PM
presse 1 menuitem execute 4 times the same JFrame marcvb New To Java 0 12-27-2007 11:48 PM
Calling jar classes from java executable SteM Advanced Java 1 11-27-2007 10:21 AM
Problem calling another class adlb1300 New To Java 3 10-25-2007 04:05 PM


All times are GMT +3. The time now is 10:54 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org