0

First, yes I have researched this question. Yes, I have seen similar things, and have had no problem with that, but this is an added twist and I'm a bit confused. So this is what I'm trying to do:

For each iteration of the loop, toss all coins. If a coin comes up heads then add the value of the coin to the balance. If a coin comes up tails then subtract the value of the coin from the balance. After all iterations, display the balance and the number of seconds it took to execute the loop formatted to three decimal places.

Coin.java class

import java.util.Random; 

public class Coin
{

private String sideUp;
private Random headORtail = new Random();

public Coin() //no-arg constructor to determine what side of the coin is facing up
{
toss(); //calls the toss method
}



public Coin(String whatSide) //parameterized constructor to set initial side of coin 
{
sideUp = whatSide;
}



public void toss() //simulates coin toss
{
int num = headORtail.nextInt(2); //random number in the range of 0 - 1

if(num == 0)
{
sideUp = "Heads"; //0 for heads
}
else
{
sideUp = "Tails"; //1 for tails
}

}



public String getSideUp() //returns value of sideUp
{
return sideUp;
}


}

CoinDemo.java import java.util.Scanner;

public class CoinDemo
{
    public static void main(String[] args)
{

//uses parameterized constructor to allow the programmer to set the initial sideUp
Coin penny = new Coin("Tails"); 
//uses no-arg constructor to create coin objects
Coin nickel = new Coin(); 
Coin dime = new Coin();  
Coin quarter = new Coin();
Coin half = new Coin();
Scanner keyboard = new Scanner(System.in); //Creates a new scanner to allow user to enter input



System.out.println("Please enter the the number of coin flips to be performed.\nMust be greater than 0: ");
int numFlips = keyboard.nextInt();  //Stores the user input into numFlips



//Validation loop checks value entered
while (numFlips <= 0)
{
System.out.println("\n\t~~~ERROR~~~");
System.out.println("The number entered must be greater than 0, please try again.");
System.out.println("\t~~~~~~~~~~~\n");
    System.out.println("Please enter the the number of coin flips to be performed. Must be greater than 0: ");
    numFlips = keyboard.nextInt(); //stores the user input into numFlips
}



long totalTime = System.currentTimeMillis(); //Once valid number is entered start the timer

double totalCoin;



for (int i = 0; i < numFlips; i++) //for loop to increment until equal to user entered numFlips
{     
//calls the Coin object's toss method and stores result in their respective instance variables
penny.toss();
nickel.toss();
dime.toss();
quarter.toss();
half.toss();



}

}
}

My understanding is that after for example penny.toss(); executes in the loop it will either store the string "Heads" or "Tails" in the instance variable penny. So I tried to use

if (penny == "Heads") 
{
totalCoin += 0.01;
} 
else 
{
totalCoin -= 0.01;
}

I'm not sure how else to go about this. I'm fairly new to programming, so please understand I'm trying to learn. That's why I'm here asking for help to understand how to do this.

Kr4ckl3s
  • 45
  • 6
  • 1
    What is your question/problem? Read http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – JB Nizet Nov 15 '15 at 22:05
  • `penny` is a instance of `Coin`, it is not compatible with `String`, you should try using something more like `if ("Heads".equals(penny.getSideUp())) {`. You should also consider using an array to store all your `Coin`s in, it will make life simpler – MadProgrammer Nov 15 '15 at 22:08
  • This exercise focuses on classes. I only vaguely know how to use arrays, but am restricted to doing this a certain way. Creative problem solving is unfortunately not encouraged. – Kr4ckl3s Nov 15 '15 at 22:16

1 Answers1

1

First thing i saw, when you compare strings, you should do

if (penny.getSideUp().equals("Heads"))

instead of using ==.

Also, you need to compare the sideUp variable, not your class.

Aimert
  • 146
  • 6