Java homework help

Status
Not open for further replies.

mypcbroke

Baseband Member
Messages
49
Hey guys,

First off all I'm not looking for you to write the program but rather just show me the right direction to go.

"Using the coin class design, implement a driver class called 'FlipRace' whose main method creates 2 coin objects, then continually flips them both to see which coin first comes up heads 3 times in a row. Continue flipping until one of the coins wins, and consider they may tie. Print the results of each turn, print the winner, and print the total flips required."

Okay, so far I created the main class FlipRace and another class CoinFlip, so I have nothing. I know I need the 2 coin objects, but I'm not sure if I put them in the driver class or the class where my main block of code will be that flips and reports the results (my guess is the latter)

Since it flips until 3 heads in a row, I believe I need to use an if statement or a for loop. I also think the random package should be imported and used somehow but I'm having trouble putting this all together.


Can anyone point me in the right direction, or offer anything to get me started?


Much appreciated
 
We can't help you too much because that would be against the rules (as you probably know). However I will give you the following hints:

1) You should only need 2 classes; Coin and FlipRace
2) You will need both a loop and an if condition
3) It is implied that the result of flipping a Coin is random
4) This is very simple (so if it starts to get complicated, you're doing it wrong)

Why not try to post some pseudocode, it might help you work it through in your mind.
 
We can't help you too much because that would be against the rules (as you probably know). However I will give you the following hints:

1) You should only need 2 classes; Coin and FlipRace
2) You will need both a loop and an if condition
3) It is implied that the result of flipping a Coin is random
4) This is very simple (so if it starts to get complicated, you're doing it wrong)

Why not try to post some pseudocode, it might help you work it through in your mind.

Thanks, that did help me get started. However it's getting my ideas on how to tackle the problem into psuedocode that is challenging me right now
 
Never start typing before you write out at least a general psuedo program. I am currently in school and when I began programming we started in java. I never wrote psuedocode and it always took a while to figure things out.
If you just take some paper and write down what you know needs to happen as generically as possible. It will help you and us.
 
Maybe if you post what you have, anything at all, we can help clarify how the requirements relate to your code.

When I posted this I literally had nothing :sad: but I was able to get some help in class. Here's what I have so far:

Code:
 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package coinproj;

/**
 *
 * @author mypcbroke
 */
public class FlipRace {

    /**
     * 
     */
    public static void main(String[] args) {
        
        Coin coin1 = new Coin();
        Coin coin2 = new Coin();
        
        int face1=2, face2=2;
        
        int count1 = 0, count2 = 0; //Two counters for two coins
    
    while (count1 != 3 && count2 != 3)
    {
        coin1.flip();
        coin2.flip();
        
        System.out.println("The first coin returns:" + coin1 +
                "\t The second coin returns:" +coin2);
                
                if (coin1.isHeads())
                {
                    if (face1 == Coin.HEADS){
                        count1++;
                    }
                }
                face1 = coin1.face;
                
                if(coin2.isHeads())
                {
                    if(face2 == Coin.HEADS){
                        count2++;
                    }
                    
                }
                
                face2 = coin2.face;
    }
     if (count1 == 3)
     {
         System.out.println("The winner is coin number one which required  " + ??? + "\t flips");
     }else{
         System.out.println("");
     
         
     if(count2 == 3)
     {
         System.out.println("The winner is coin number two which required  " + ??? + "\t flips");
     }else{
         System.out.println("");
                
     }
     
         
              
        
     }
    }
}

So I have my loops and logic, but I need to find a way to display the total number of flips (I think I need to create a new variable to record these because plugging in count1 and count2 don't work)

Also, the loop seems to keep running even after I get 3 heads in a row, not sure why...


EDIT: I'm using this coin class source code:
Code:
//********************************************************************
//  Coin.java       Author: Lewis/Loftus
//
//  Represents a coin with two sides that can be flipped.
//********************************************************************
package coinproj;

public class Coin
{
   public static final int HEADS = 0;
   public static final int TAILS = 1;

   public int face;

   //-----------------------------------------------------------------
   //  Sets up the coin by flipping it initially.
   //-----------------------------------------------------------------
   public Coin ()
   {
      flip();
   }

   //-----------------------------------------------------------------
   //  Flips the coin by randomly choosing a face value.
   //-----------------------------------------------------------------
   public void flip ()
   {
      face = (int) (Math.random() * 2);
   }

   //-----------------------------------------------------------------
   //  Returns true if the current face of the coin is heads.
   //-----------------------------------------------------------------
   public boolean isHeads ()
   {
      return (face == HEADS);
   }

   //-----------------------------------------------------------------
   //  Returns the current face of the coin as a string.
   //-----------------------------------------------------------------
   public String toString()
   {
      String faceName;

      if (face == HEADS)
         faceName = "Heads";
      else
         faceName = "Tails";

      return faceName;
   }
}
 
Status
Not open for further replies.
Back
Top Bottom