Results 1 to 7 of 7
  1. #1
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default The if Statement, need some help!

    Following is the code. The instructions are in the comments and I was able to complete the first 2 parts i think correctly. I am having trouble towards the bottom where it asks to implement the if statement. A few pointers would help! Like I said the instructions are in the comments before each part. If you see anything wrong with the first two parts as well please let me know. I attempted the first if statement but I know its not 100% correct and I still need to implement the statements after the condition.


    import java.util.*;

    public class PlayingCard
    {
    private int faceValue;
    private int suitValue;

    public static final int HEARTS = 0;
    public static final int DIAMONDS = 1;
    public static final int SPADES = 2;
    public static final int CLUBS = 3;

    public static final int ACE = 1;
    public static final int KING = 13;
    public static final int QUEEN = 12;
    public static final int JACK = 11;

    /**
    * Constructor for PlayingCard class.
    * Generates a random playing card (randomly selected face value and suit).
    */
    public PlayingCard()
    {
    generateNewRandomCard();
    }

    /**
    * Constructor for PlayingCard class.
    *
    * @param face The face value for the card. 2 through 10 are used for the numbered cards.
    * Pass one of the named constants as the value for this parameter for the
    * Ace, King, Queen, or Jack.
    *
    * @param suit The suit of the card. Pass one of the named constants for the suit of the
    * card.
    */
    public PlayingCard(int face, int suit)
    {
    faceValue = face;
    suitValue = suit;
    }

    /**
    * Generates a random card. That is, this method can be called to select a
    * new random face value and suit.
    */
    public void generateNewRandomCard()
    {
    Random flip = new Random();

    // STEP 1: Complete the following statement so that a random "suit" is selected.
    // Note that we have been given constants above that will be used to
    // represent each of the suits. You want to complete the following
    // statement to select a random integer in the range 0 to 3.
    // Recall from a previous assignment, that we can generate a random
    // number between 0 and n by calling the nextInt method of the Random
    // class. For example, our Random object has been called flip above,
    // so we can generate a random number between 0 and n with flip.nextInt(n+1).
    // In other words, if we want a random number between 0 and 9 we would
    // have flip.nextInt(10) Only here, we want a random number between
    // 0 and 3.

    suitValue = flip.nextInt(4) ;

    // STEP 2: Complete the following statement to generate a random face value for the
    // Card. Face values can be Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen,
    // King. We have been given constants above to represent the "named" face
    // values. Specifically, we are using 1 for the Ace, 11, 12, and 13 for
    // Jack, Queen, and King. 2 through 10 will just be the obvious 2 through 10.
    //
    // To generate a Random face value we will need a random number between
    // 1 and 13. HINT: To get this, you can call nextInt, but you will need to
    // add 1 to the result, just like you did with the Dice program.

    faceValue = flip.nextInt(12) + 1 ;

    }


    /**
    * Accessor to get the face value of the card as a String. Specifically,
    * this method will return to you a String with one of "Ace", "2", "3", ..., "10", "Jack",
    * "Queen", "King". This method should return "NOT A CARD" if the face value is invalid.
    *
    * @return A String with the face value of the Card
    */
    public String getCardFaceValue()
    {
    //STEP 3: Write the Java statements necessary to determine which card is represented
    // by the instance field faceValue. Return the String that describes that card.
    // If the value of faceValue is not valid (not one of the 13 legal face values,
    // return the string "NOT A CARD".
    //
    // HINT: You will need an if statement. You will likely want to specifically
    // use the optional else if part as well.
    // HINT 2: In the conditions of your if statement, you can compare the value of
    // faceValue to the constant that represents the Ace. If those values are
    // equal, have a return statement that returns "Ace". You'll then want
    // an else if with a condition that checks if the card is a King, and so forth.
    // HINT 3: If you determine that the card is one of the numbered values, you will
    // need to return the String version of it. Recall that one way of turning
    // a number into a String is to concatenate it with the empty String. That is
    // If n is a variable of type int and you want to return the String version of it,
    // you can do: return "" + n;


    @return (if faceValue == ACE)

    }

    /**
    * Accessor to get the suit of the card as a String. Specifically,
    * this method will return to you a String with one of "Hearts", Spades", "Clubs", "Diamonds".
    * It will return "NOT A CARD" if the suit is not one of the legal suits.
    *
    * @return A String representing the name of the suit of the card.
    */
    public String getCardSuit()
    {
    //STEP 4: Write an if statement (you will likely need to use the else if part) to compare
    // the value of the instance field suitValue to each of the 4 possible choices (the
    // constants defined at the top). This if statement should determine which
    // suit the card is from and should return the name of it as a String.
    // If the value stored in suitValue is not one of the 4 legal values, return "NOT A CARD"
    // (hint: this last case can be an else).


    }






    /**
    * Accessor to get the suit as an integer.
    * @return the suit as an integer between 0 and 3 that corresponds to the named constants for the suit
    */
    public int getCardSuitAsInt()
    {
    // DO NOT CHANGE THIS METHOD IN ANY WAY
    return suitValue;
    }

    /**
    * Accessor to get the face value as an integer.
    * @return the face value as an integer between 1 and 13 that corresponds to the named constants for the face value
    */
    public int getCardFaceAsInt()
    {
    // DO NOT CHANGE THIS METHOD IN ANY WAY
    return faceValue;
    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    For the second part, how do you get the value 13 returned back?
    For the third part, read the requirements carefully. You are supposed to return a string based on the face value. You know that if the value is 1 then it's an ACE which in code translates to
    Java Code:
    if(faceValue == 1) {
      return "Ace";
    }
    You should be able to figure out the rest.

  3. #3
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Still seem to be having a tough time. here is what I have.

    After the first if statements I get an error at the end saying i am missing a return statement after I added the last }. Also, still stumped about the very last part, not understanding what they are looking for. Any help would be greatly appreciated.








    import java.util.*;

    public class PlayingCard
    {
    private int faceValue;
    private int suitValue;

    public static final int HEARTS = 0;
    public static final int DIAMONDS = 1;
    public static final int SPADES = 2;
    public static final int CLUBS = 3;

    public static final int ACE = 1;
    public static final int KING = 13;
    public static final int QUEEN = 12;
    public static final int JACK = 11;

    /**
    * Constructor for PlayingCard class.
    * Generates a random playing card (randomly selected face value and suit).
    */
    public PlayingCard()
    {
    generateNewRandomCard();
    }

    /**
    * Constructor for PlayingCard class.
    *
    * @param face The face value for the card. 2 through 10 are used for the numbered cards.
    * Pass one of the named constants as the value for this parameter for the
    * Ace, King, Queen, or Jack.
    *
    * @param suit The suit of the card. Pass one of the named constants for the suit of the
    * card.
    */
    public PlayingCard(int face, int suit)
    {
    faceValue = face;
    suitValue = suit;
    }

    /**
    * Generates a random card. That is, this method can be called to select a
    * new random face value and suit.
    */
    public void generateNewRandomCard()
    {
    Random flip = new Random();

    // STEP 1: Complete the following statement so that a random "suit" is selected.
    // Note that we have been given constants above that will be used to
    // represent each of the suits. You want to complete the following
    // statement to select a random integer in the range 0 to 3.
    // Recall from a previous assignment, that we can generate a random
    // number between 0 and n by calling the nextInt method of the Random
    // class. For example, our Random object has been called flip above,
    // so we can generate a random number between 0 and n with flip.nextInt(n+1).
    // In other words, if we want a random number between 0 and 9 we would
    // have flip.nextInt(10) Only here, we want a random number between
    // 0 and 3.

    suitValue = flip.nextInt(CLUBS) ;

    // STEP 2: Complete the following statement to generate a random face value for the
    // Card. Face values can be Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen,
    // King. We have been given constants above to represent the "named" face
    // values. Specifically, we are using 1 for the Ace, 11, 12, and 13 for
    // Jack, Queen, and King. 2 through 10 will just be the obvious 2 through 10.
    //
    // To generate a Random face value we will need a random number between
    // 1 and 13. HINT: To get this, you can call nextInt, but you will need to
    // add 1 to the result, just like you did with the Dice program.

    faceValue = flip.nextInt(12) + 1 ;

    }


    /**
    * Accessor to get the face value of the card as a String. Specifically,
    * this method will return to you a String with one of "Ace", "2", "3", ..., "10", "Jack",
    * "Queen", "King". This method should return "NOT A CARD" if the face value is invalid.
    *
    * @return A String with the face value of the Card
    */
    public String getCardFaceValue()
    {
    //STEP 3: Write the Java statements necessary to determine which card is represented
    // by the instance field faceValue. Return the String that describes that card.
    // If the value of faceValue is not valid (not one of the 13 legal face values,
    // return the string "NOT A CARD".
    //
    // HINT: You will need an if statement. You will likely want to specifically
    // use the optional else if part as well.
    // HINT 2: In the conditions of your if statement, you can compare the value of
    // faceValue to the constant that represents the Ace. If those values are
    // equal, have a return statement that returns "Ace". You'll then want
    // an else if with a condition that checks if the card is a King, and so forth.
    // HINT 3: If you determine that the card is one of the numbered values, you will
    // need to return the String version of it. Recall that one way of turning
    // a number into a String is to concatenate it with the empty String. That is
    // If n is a variable of type int and you want to return the String version of it,
    // you can do: return "" + n;


    if(faceValue == 1) {
    return "Ace";
    }
    if (faceValue==2) {
    return "2";
    }
    if(faceValue==3) {
    return "3";
    }
    if(faceValue==4) {
    return "4";
    }
    if(faceValue==5) {
    return "5";
    }
    if (faceValue==6) {
    return "6";
    }
    if (faceValue==7) {
    return "7";
    }
    if (faceValue==8) {
    return "8";
    }
    if (faceValue==9) {
    return "9";
    }
    if (faceValue==10) {
    return "10";
    }
    if (faceValue==11) {
    return "Jack";
    }
    if (faceValue==12) {
    return "Queen";
    }
    if (faceValue==13) {
    return "King";

    }
    }
    /**
    * Accessor to get the suit of the card as a String. Specifically,
    * this method will return to you a String with one of "Hearts", Spades", "Clubs", "Diamonds".
    * It will return "NOT A CARD" if the suit is not one of the legal suits.
    *
    * @return A String representing the name of the suit of the card.
    */
    public String getCardSuit()
    {
    //STEP 4: Write an if statement (you will likely need to use the else if part) to compare
    // the value of the instance field suitValue to each of the 4 possible choices (the
    // constants defined at the top). This if statement should determine which
    // suit the card is from and should return the name of it as a String.
    // If the value stored in suitValue is not one of the 4 legal values, return "NOT A CARD"
    // (hint: this last case can be an else).

    if (suitValue == 1) {
    return "HEART";
    }




    }

    /**
    * Accessor to get the suit as an integer.
    * @return the suit as an integer between 0 and 3 that corresponds to the named constants for the suit
    */
    public int getCardSuitAsInt()
    {
    // DO NOT CHANGE THIS METHOD IN ANY WAY
    return suitValue;
    }

    /**
    * Accessor to get the face value as an integer.
    * @return the face value as an integer between 1 and 13 that corresponds to the named constants for the face value
    */
    public int getCardFaceAsInt()
    {
    // DO NOT CHANGE THIS METHOD IN ANY WAY
    return faceValue;
    }
    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You are failing to read some things ...
    1.) You failed to read the question I asked you above.
    2.) You failed to read the part in your instructions which says
    // HINT 3: If you determine that the card is one of the numbered values, you will
    // need to return the String version of it. Recall that one way of turning
    // a number into a String is to concatenate it with the empty String. That is
    // If n is a variable of type int and you want to return the String version of it,
    // you can do: return "" + n;
    That hint helps you to get rid of that long chain of if statements. If it's one of the numbers then you just need one return for them.
    3.) The instructions asked you to use if else.
    4.) You also didn't read the site guidelines about using code tags when posting code.

    The missing return statement thing is the compiler realizing that if all your if tests fail, the method will not return anything! This cannot be allowed since the method declares that it returns a String. Better return some string at the end of the method. The instructions also tell you the string to return if the ifs didn't match.

  5. #5
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Java Code:
    
         if(faceValue == 1) {
      return "Ace";
    }
      else if(faceValue==2) {
          return "Two";
        }
        else if(faceValue==3) {
            return "Three";
        }
    
    
    
    
    
    
    
    }
    Even with just the

    if(faceValue == 1) {
    return "Ace";
    }


    I still get missing statement, but shouldn't it work cause Ace is a String?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    "Ace" is only returned if faceValue == 1.
    What if faceValue = 42? What is returned? None of the ifs will match so there is nothing to return.
    Your hints say
    // If the value of faceValue is not valid (not one of the 13 legal face values,
    // return the string "NOT A CARD".
    So at the very end of your method, as the last line, return "NOT A CARD" because if the code gets to that point it means that the face value did not match any of the ifs and so it doesn't represent a card.
    Now go through the 4 points I posted above again.

  7. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Hi, please indent your code properly and please use the code tags next time

    Java Code:
    import java.util.*;
    
    public class PlayingCard
    {
    private int faceValue;
    private int suitValue;
    
    public static final int HEARTS = 0;
    public static final int DIAMONDS = 1;
    public static final int SPADES = 2;
    public static final int CLUBS = 3;
    
    public static final int ACE = 1;
    public static final int KING = 13;
    public static final int QUEEN = 12;
    public static final int JACK = 11;
    
    /**
    * Constructor for PlayingCard class.
    * Generates a random playing card (randomly selected face value and suit).
    */
    public PlayingCard()
    {
    generateNewRandomCard();
    }
    
    /**
    * Constructor for PlayingCard class.
    * 
    * @param face The face value for the card. 2 through 10 are used for the numbered cards.
    * Pass one of the named constants as the value for this parameter for the
    * Ace, King, Queen, or Jack.
    * 
    * @param suit The suit of the card. Pass one of the named constants for the suit of the
    * card.
    */
    public PlayingCard(int face, int suit)
    {
    faceValue = face;
    suitValue = suit;
    }
    
    /**
    * Generates a random card. That is, this method can be called to select a
    * new random face value and suit.
    */
    public void generateNewRandomCard()
    {
    Random flip = new Random();
    
    // STEP 1: Complete the following statement so that a random "suit" is selected.
    // Note that we have been given constants above that will be used to
    // represent each of the suits. You want to complete the following
    // statement to select a random integer in the range 0 to 3.
    // Recall from a previous assignment, that we can generate a random
    // number between 0 and n by calling the nextInt method of the Random
    // class. For example, our Random object has been called flip above,
    // so we can generate a random number between 0 and n with flip.nextInt(n+1).
    // In other words, if we want a random number between 0 and 9 we would
    // have flip.nextInt(10) Only here, we want a random number between
    // 0 and 3.
    
    suitValue = flip.nextInt(4) ;
    
    // STEP 2: Complete the following statement to generate a random face value for the
    // Card. Face values can be Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen,
    // King. We have been given constants above to represent the "named" face
    // values. Specifically, we are using 1 for the Ace, 11, 12, and 13 for 
    // Jack, Queen, and King. 2 through 10 will just be the obvious 2 through 10.
    //
    // To generate a Random face value we will need a random number between 
    // 1 and 13. HINT: To get this, you can call nextInt, but you will need to
    // add 1 to the result, just like you did with the Dice program.
    
    faceValue = flip.nextInt(12) + 1 ;
    
    }
    
    
    /**
    * Accessor to get the face value of the card as a String. Specifically,
    * this method will return to you a String with one of "Ace", "2", "3", ..., "10", "Jack",
    * "Queen", "King". This method should return "NOT A CARD" if the face value is invalid.
    * 
    * @return A String with the face value of the Card
    */
    public String getCardFaceValue()
    {
    //STEP 3: Write the Java statements necessary to determine which card is represented
    // by the instance field faceValue. Return the String that describes that card.
    // If the value of faceValue is not valid (not one of the 13 legal face values,
    // return the string "NOT A CARD".
    //
    // HINT: You will need an if statement. You will likely want to specifically
    // use the optional else if part as well.
    // HINT 2: In the conditions of your if statement, you can compare the value of
    // faceValue to the constant that represents the Ace. If those values are
    // equal, have a return statement that returns "Ace". You'll then want
    // an else if with a condition that checks if the card is a King, and so forth.
    // HINT 3: If you determine that the card is one of the numbered values, you will
    // need to return the String version of it. Recall that one way of turning
    // a number into a String is to concatenate it with the empty String. That is
    // If n is a variable of type int and you want to return the String version of it,
    // you can do: return "" + n;
    
    
    @return (if faceValue == ACE)
    
    }
    
    /**
    * Accessor to get the suit of the card as a String. Specifically,
    * this method will return to you a String with one of "Hearts", Spades", "Clubs", "Diamonds".
    * It will return "NOT A CARD" if the suit is not one of the legal suits.
    * 
    * @return A String representing the name of the suit of the card.
    */
    public String getCardSuit()
    {
    //STEP 4: Write an if statement (you will likely need to use the else if part) to compare
    // the value of the instance field suitValue to each of the 4 possible choices (the
    // constants defined at the top). This if statement should determine which
    // suit the card is from and should return the name of it as a String.
    // If the value stored in suitValue is not one of the 4 legal values, return "NOT A CARD"
    // (hint: this last case can be an else).
    
    
    }
    
    
    
    
    
    
    /**
    * Accessor to get the suit as an integer.
    * @return the suit as an integer between 0 and 3 that corresponds to the named constants for the suit
    */
    public int getCardSuitAsInt()
    {
    // DO NOT CHANGE THIS METHOD IN ANY WAY
    return suitValue; 
    }
    
    /**
    * Accessor to get the face value as an integer.
    * @return the face value as an integer between 1 and 13 that corresponds to the named constants for the face value
    */
    public int getCardFaceAsInt()
    {
    // DO NOT CHANGE THIS METHOD IN ANY WAY
    return faceValue; 
    }
    }

Similar Threads

  1. if statement help please!!
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 12-02-2008, 02:56 PM
  2. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 02:58 PM
  3. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM
  5. If Statement
    By aDrizzle in forum New To Java
    Replies: 4
    Last Post: 07-08-2007, 08:55 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
  •