Results 1 to 3 of 3
  1. #1
    nikki101 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Constructors not right?

    thanks
    Last edited by nikki101; 05-26-2013 at 08:47 AM.

  2. #2
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Constructors not right?

    If you are passing TripleString thePull ...then why are you declaring new object ? just write thepull.getstring()

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Constructors not right?

    Quote Originally Posted by nikki101 View Post
    I believe my error is in the getpaymultipler method due to the constructor. I am totally lost on how to get it right.

    import java.util.Scanner;
    import java.util.regex.Pattern;
    import java.text.DecimalFormat;
    import org.apache.commons.lang.StringUtils;
    import java.lang.Math;

    public class homeworkweek7
    {
    public static void main(String[] args)
    {
    //variables
    int theBetAmount = 0;
    TripleString thePull;
    int gambleIncrease = 0;
    int payoutOfGamble = 0;
    while(true)
    {
    theBetAmount = getBet();
    if (theBetAmount >0 && theBetAmount <= 50)
    {
    thePull = pull();
    gambleIncrease = getPayMultiplier (thePull);
    payoutOfGamble = theBetAmount*gambleIncrease;
    display(thePull, payoutOfGamble);
    }
    }
    }
    //gets the user bet amount and check for validity or quits
    public static int getBet()
    {
    int theBetAmount = 0;
    Scanner input = new Scanner(System.in);
    do
    {
    System.out.println("How much would you like to bet (0-100)" +
    " or 0 to quit?");
    theBetAmount = input.nextInt();
    if (theBetAmount > 50 || theBetAmount < 0)
    {
    System.out.println("Bet not accepted. Try Again.");
    }
    else if (theBetAmount == 0)
    {
    System.out.println("You have quit");
    System.exit(0);
    }
    }
    while (theBetAmount > 100 && theBetAmount < 0);

    return theBetAmount;
    }
    // fills TripleString using randomstring()
    public static TripleString pull()
    {
    TripleString pulled = new TripleString();
    pulled.SetString1(randString());
    pulled.setString2(randString());
    pulled.setString3(randString());
    return pulled;
    }
    //randomly generates a slot output
    public static String randString()
    {
    double randomNumber = 0;
    int randomIntNumber = 0;
    String outcomeOutput = "";
    randomNumber = Math.random();
    randomIntNumber = (int)((100)*randomNumber);
    if (randomIntNumber >= 0 && randomIntNumber <= 45)
    {
    outcomeOutput = "BAR";
    }
    else if (randomIntNumber > 45 && randomIntNumber <= 70)
    {
    outcomeOutput = "Cherries";
    }
    else if (randomIntNumber >70 && randomIntNumber <= 85)
    {
    outcomeOutput = "Space";
    }
    else
    {
    outcomeOutput = "7";
    }
    return outcomeOutput;
    }
    //Calculates the multiple of winnings if user wins
    public static int getPayMultiplier (TripleString thePull)
    {
    //order matters for payout
    int payoutOfPull = 0;
    TripleString pulled = new TripleString();
    String string1 = "";
    String string2 = "";
    String string3 = "";
    string1 = pulled.getString1();
    string2 = pulled.getString2();
    string3 = pulled.getString3();

    if (string1 == "Cherries" && string2 != "Cherries" && (string3 ==
    "Cherries" || string3 == "BAR" || string3 == "7" || string3
    == "Space"))
    {
    payoutOfPull = 5;
    }
    else if (string1 == "Cherries" && string2 == "Cherries" && string3
    != "Cherries")
    {
    payoutOfPull = 15;
    }
    else if (string1 == "Cherries" && string2 == "Cherries" && string3
    == "Cherries")
    {
    payoutOfPull = 30;
    }
    else if (string1 == "BAR" && string2 == "BAR" && string3 == "BAR")
    {
    payoutOfPull = 50;
    }
    else if (string1 == "7" && string2 == "7" && string3 == "7")
    {
    payoutOfPull = 100;
    }
    return payoutOfPull;
    }

    //display the pull and the amount the user wins
    public static void display (TripleString thePull, int winnings)
    {
    System.out.println("Whirrrrrr.... and your pull is ...");
    System.out.println("");
    if(winnings > 0)
    {
    System.out.println("Congratulations. you win: " + winnings);
    }
    else
    {
    System.out.println("Sorry, you lose.");
    }
    }
    }
    class TripleString
    {
    private String string1;
    private String string2;
    private String string3;

    public static int MAX_LEN = 20;

    // default constructor
    public TripleString()
    {
    this.string1 = "";
    this.string2 = "";
    this.string3 = "";
    }
    public boolean validString(String str)
    {
    boolean outcomeOfLength;
    if(str.length()< MAX_LEN && str != null)
    {
    outcomeOfLength = true;
    }
    else
    {
    outcomeOfLength = false;
    }
    return outcomeOfLength;
    }
    //get and setters
    public String getString1()
    {
    return string1;
    }
    public boolean SetString1(String str)
    {
    if (!validString(str))
    return false;
    this.string1 = str;
    return true;
    }
    public void setString1(String string1)
    {
    this.string1 = string1;
    }
    public String getString2()
    {
    return string2;
    }
    public boolean SetString2(String str)
    {
    if (!validString(str))
    return false;
    this.string2 = str;
    return true;
    }
    public void setString2(String string2)
    {
    this.string2 = string2;
    }
    public String getString3()
    {
    return string3;
    }
    public boolean SetString3(String str)
    {
    if (!validString(str))
    return false;
    this.string3 = str;
    return true;
    }
    public void setString3(String string3)
    {
    this.string3 = string3;
    }
    }
    @OP: please don't delete post contents, as this makes it very hard for people to follow the discussion in a thread.

Similar Threads

  1. Constructors
    By zhider in forum New To Java
    Replies: 5
    Last Post: 11-13-2012, 04:43 AM
  2. Help with constructors
    By philip1597 in forum New To Java
    Replies: 7
    Last Post: 08-31-2012, 02:56 PM
  3. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 01:10 AM
  4. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 11:51 PM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 04:15 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
  •