Results 1 to 10 of 10
  1. #1
    Alejandro_T is offline Member
    Join Date
    Dec 2016
    Posts
    6
    Rep Power
    0

    Default Assignment has me on edge....

    Modifying the Coin Class
    1. Create a new class named BiasedCoin that models a biased coin (heads and tails are not equally likely outcomes of a
    flip). To do this modify the coin class from the Listing 5.4 of text (in the file Coin.java) as follows:
    􀀀 Add a private data member bias of type double. This data member will be a number between 0 and 1 (inclusive) that
    represents the probability the coin will be HEADS when flipped. So, if bias is 0.5, the coin is an ordinary fair coin.
    If bias is 0.6, the coin has probability 0.6 of coming up heads (on average, it comes up heads 60% of the time).
    􀀀 Modify the default constructor by assigning the value 0.5 to bias before the call to flip. This will make the default
    coin a fair one.
    􀀀 Modify flip so that it generates a random number then assigns face a value of HEADS if the number is less than the
    bias; otherwise it assigns a value of TAILS.
    􀀀 Add a second constructor with a single double parameter—that parameter will be the bias. If the parameter is valid
    (a number between 0 and 1 inclusive) the constructor should assign the bias data member the value of the parameter;
    otherwise it should assign bias a value of 0.5. Call flip (as the other constructor does) to initialize the value of face.
    2. Compile your class to make sure you have no syntax errors.
    3. Write a program that uses three BiasedCoin objects. Instantiate one as a fair coin using the constructor with no
    parameter. Read in the biases for the other two coins and instantiate those coins using the constructor with the bias as a
    parameter. Your program should then have a loop that flips each coin 100 times and counts the number of times each is
    heads. After the loop print the number of heads for each coin. Run the program several times testing out different biases.



    We haven't covered this in class and are being expected to hand this in. It is so confusing and I have tried asking for help, but no one seems to understand this in the GA's office or tutor center.

    Thank you in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Assignment has me on edge....

    What haven't you covered?

    You apparently have a start point (it mentions a Coin class) that you need to rename and use to add the new functionality.
    Step 1 explains in quite a lot of detail what to change.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Assignment has me on edge....

    Moved to New section.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Assignment has me on edge....

    You can almost, to the letter, follow the instructions. It's all in there. Let me give you a start:

    Create a new class named BiasedCoin that models a biased coin
    Java Code:
    class BiasedCoin {
      // here is the content of the Coin class you were supposed to copy and modify
    }
    Add a private data member bias of type double
    Java Code:
    class BiasedCoin {
       private double bias;
    
      //other content
    }
    Modify the default constructor by assigning the value 0.5 to bias
    Java Code:
    class BiasedCoin {
       private double bias;
    
       public BiasedCoin() {
           bias = 0.5;
       } 
    
      //other content
    }
    I know I am giving away pretty much here, but this should enable you to get started and finish this easily. Let us know how it goes.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Assignment has me on edge....

    My crystal ball tells me the Coin class looks like this, is this correct?

    Java Code:
    import java.util.Random;
    
    public class Coin
    {
       private final int HEADS = 0;
       private final int TAILS = 1;
    
       private 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;
       }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Assignment has me on edge....

    Omg, all the professors are stealing this from eachother. It's all over the internet, literally :)

    https://www.google.nl/search?num=30&...KceogAb9ppHACA
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  7. #7
    Alejandro_T is offline Member
    Join Date
    Dec 2016
    Posts
    6
    Rep Power
    0

    Default Re: Assignment has me on edge....

    So I was able to write the code for everything that was the beginning portion of it. Now I am having trouble understanding the last part which is the "Write a program that uses three BiasedCoin...."

    Am I suppose to create another class or project. it is unclear to me. and should I be calling the BiasedCoin class? if so, how?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Assignment has me on edge....

    calling the BiasedCoin class? if so, how?
    Use the new statement to create an instance of the class, save the reference to that instance and use it to call the class's methods for testing.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Assignment has me on edge....

    I would create another class, called whatever you want (TestBiasedCoin?), int he same package (place) as the BiasedCoin class.
    That will have a main() method (your notes should tell you what that is if you've ever run a Java program before) that will do the work.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Assignment has me on edge....

    Your program, like Tolls says, probably lives in a main method somewhere. It can be in the BiasedCoin class itself, but for clarity sake it's probably better to create that TestBiasedCoin class. You will create three new objects with the new keyword:
    Java Code:
    BiasedCoin coin1 = new BiasedCoin();
    BiasedCoin coin2 = new BiasedCoin(0.6);
    BiasedCoin coin3 = new BiasedCoin(0.9);
    You can now execute the methods on each object.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Similar Threads

  1. Finding a cell on Edge
    By ssk2659 in forum New To Java
    Replies: 2
    Last Post: 01-23-2015, 09:00 AM
  2. Blue Mind Edge 2013-04-15
    By java software in forum Java Software
    Replies: 0
    Last Post: 04-20-2013, 08:46 AM
  3. Canny Edge Detection
    By tiny in forum Advanced Java
    Replies: 4
    Last Post: 03-16-2011, 04:54 PM
  4. Image Matching Using Edge Detection
    By krishnanrocks87 in forum Java 2D
    Replies: 0
    Last Post: 02-22-2011, 05:39 AM
  5. Edge detector
    By ranwolf in forum SWT / JFace
    Replies: 9
    Last Post: 09-11-2008, 01:42 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
  •