Missing return statements?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KoreyAusTex
    New Member
    • Feb 2007
    • 36

    Missing return statements?

    If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()?

    Code:
    import java.util.*;
    public class Card
    {
        // instance variables
        //suits
        private int suit;
        private int spades;
        private int hearts;
        private int diamonds;
        private int clubs;
    
        //values
        private int value;
        private int ace;
        private int jack;
        private int queen;
        private int king;
    
        // constants representing the suits and values
        public static final int SPADES = 3;
        public static final int HEARTS = 2;
        public static final int DIAMONDS = 1;
        public static final int CLUBS = 0;
    
        public static final int ACE = 1;
        public static final int JACK = 11;
        public static final int QUEEN = 12;
        public static final int KING = 13;
    
        //constructors
        //constructor that creates a random, valid card
        public Card()
        {
            Random gen = new Random();
            suit = gen.nextInt(4);
            spades = 3;
            hearts = 2;
            diamonds = 1;
            clubs = 0;
    
            value = gen.nextInt(13);
            ace =1;
            jack = 11;
            queen = 12;
            king = 13;
       }
    
       //constructor that accepts two parameters.
       //First parameter is the suit
       //Second parameter is the card value
    
       public Card(int suit, int value)
       {
            this.suit = suit;
            this.spades = spades;
            this.diamonds = diamonds;
            this.clubs = clubs;
    
            this.ace = ace;
            this.jack = jack;
            this.queen = queen;
            this.king = king;
       }
    
       //override equals() method inherited from object
       public boolean equals(Object obj)
       {
           //check to see if object is a reference to the cards
           //and if so, check if its value and suit equals the currecnt value and suit
            if(obj instanceof Card)
            {
                Card crd = (Card) obj;
                if ((value == crd.value) && (suit == crd.suit)) return true;
                else return false;
            }
                else return false;
       }
    
       // override close () method from the object class
       public Object clone()
       {
            //create card that has the same instant values as the current card
            Card newCard = new Card();
            newCard.value = value;
            newCard.suit = suit;
            return newCard;
       }
    
       //a compareValue() method that takes another card object and
       //returns true if the current object has a greater value and
       //false otherwise.
       public boolean compareValue(Card other)
       {
            if(value >= value)
            {
                return true;
            }
            else return false;
       }
       //Getter method
       public String getSuitAsString()
       {
           //Return a String representing the card's suit
           switch ( suit )
           {
                case SPADES: return "Spades";
                case HEARTS: return "Hearts";
                case DIAMONDS: return "Diamonds";
                case CLUBS: return "Clubs";
                default: return "??";
           }
       }
    
       public String getValueAsString()
       {
           //Return a String representing the card's value
           switch ( value )
           {
                case 1: return "Ace";
                case 2: return "2";
                case 3: return "3";
                case 4: return "4";
                case 5: return "5";
                case 6: return "6";
                case 7: return "7";
                case 8: return "8";
                case 9: return "9";
                case 10: return "10";
                case 11: return "Jack";
                case 12: return "Queen";
                case 13: return "King";
                default: return "??";
           }
       }
    
       public String toString()
       {
            //Return a String representation of this card
            return getValueAsString() + " of " + getSuitAsString();
       }
    
       //Getter method for the value
       public String getValue()
       {
            if (value == ace)
            return "Ace";
            else if (value == jack)
            return "Jack";
            else if (value == queen)
            return "Queen";
            else if (value == king)
            return "King";
       }
    
    
       //Getter method for the suit
       public String getSuit()
       {
            if (suit == spades) return "Spades";
            else if (suit == hearts) return "Hearts";
            else if (suit == diamonds) return "Diamonds";
            else if (suit == clubs) return "Clubs";
       }
    
    }
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    @ getValue().....

    JVM says: "what if value is not equal to ace, jack, queen or king?" what should i return if none of them are satisfied?".... .. null? your name? the System's specification? an object? a cloned class?

    You must be aware of having "no else statement".... when returning a data....

    And also @ getSuit().....

    regards,
    sukatoa....

    Comment

    • KoreyAusTex
      New Member
      • Feb 2007
      • 36

      #3
      Originally posted by sukatoa
      @ getValue().....

      JVM says: "what if value is not equal to ace, jack, queen or king?" what should i return if none of them are satisfied?".... .. null? your name? the System's specification? an object? a cloned class?

      You must be aware of having "no else statement".... when returning a data....

      And also @ getSuit().....

      regards,
      sukatoa....
      Would that also apply to getSuit, that is what I was thinking.

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by KoreyAusTex
        Would that also apply to getSuit, that is what I was thinking.
        ...........Yep. ...........

        Update us,
        sukatoa

        Comment

        • KoreyAusTex
          New Member
          • Feb 2007
          • 36

          #5
          Originally posted by sukatoa
          ...........Yep. ...........

          Update us,
          sukatoa
          Ok got that fixed, everything compiled correctly, but now I am getting output:

          Card 1 value is - Invalid Value
          Card 2 suit is - Invalid Value
          Card 1 is the 2 of Diamonds.
          Card 2 is the ?? of Diamonds.

          The CardTester file I am using is here:

          Code:
          /**
           * Short program that uses the Card class.
           * NOTE: Not a comprehensive test!
           * @version (10/1/04)
           */
          public class CardTester
          {
              public static void main (String [] args) {
                  Card c1 = new Card(); //creates a random card
                  Card c2 = new Card(1,12); //creates the Queen of Diamonds
          
                  System.out.println("Card 1 value is - " + c1.getValue()); //note: this will print the numeric value
                  System.out.println("Card 2 suit is - " + c2.getSuit()); //note: this will print the numeric value
                  
                  System.out.println ("Card 1 is the " + c1.toString() + "." );
                          System.out.println ("Card 2 is the " + c2.toString() + "." );
              }
                  
          }
          Here is the fixed file:

          Code:
          public class Card
          {
              //suits
              private int suit;
              private int spades;
              private int hearts;
              private int diamonds;
              private int clubs;
          
              //values
              private int value;
              private int ace;
              private int jack;
              private int queen;
              private int king;
          
              //constants representing the suits and values
              public static final int SPADES = 3;
              public static final int HEARTS = 2;
              public static final int DIAMONDS = 1;
              public static final int CLUBS = 0;
          
              public static final int ACE = 1;
              public static final int JACK = 11;
              public static final int QUEEN = 12;
              public static final int KING = 13;
          
              //constructors
              //constructor that creates a random, valid card
              public Card()
              {
                  Random gen = new Random();
                  suit = gen.nextInt(4);
                  spades = 3;
                  hearts = 2;
                  diamonds = 1;
                  clubs = 0;
          
                  value = gen.nextInt(13);
                  ace = 1;
                  jack = 11;
                  queen = 12;
                  king = 13;
             }
          
             //constructor that accepts two parameters.
             //First parameter is the suit
             //Second parameter is the card value
          
             public Card(int suit, int value)
             {
                  this.suit = suit;
                  this.spades = spades;
                  this.diamonds = diamonds;
                  this.clubs = clubs;
          
                  this.ace = ace;
                  this.jack = jack;
                  this.queen = queen;
                  this.king = king;
             }
          
             //override equals() method inherited from object
             public boolean equals(Object obj)
             {
                 //check to see if object is a reference to the cards
                 //and if so, check if its value and suit equals the currecnt value and suit
                  if(obj instanceof Card)
                  {
                      Card crd = (Card) obj;
                      if ((value == crd.value) && (suit == crd.suit)) return true;
                      else return false;
                  }
                      else return false;
             }
          
             // override close () method from the object class
             public Object clone()
             {
                  //create card that has the same instant values as the current card
                  Card newCard = new Card();
                  newCard.value = value;
                  newCard.suit = suit;
                  return newCard;
             }
          
             //a compareValue() method that takes another card object and
             //returns true if the current object has a greater value and
             //false otherwise.
             public boolean compareValue(Card other)
             {
                  if(value >= value)
                  {
                      return true;
                  }
                  else return false;
             }
             //Getter method
             public String getSuitAsString()
             {
                 //Return a String representing the card's suit
                 switch (suit)
                 {
                      case SPADES: return "Spades";
                      case HEARTS: return "Hearts";
                      case DIAMONDS: return "Diamonds";
                      case CLUBS: return "Clubs";
                      default: return "??";
                 }
              }
          
             public String getValueAsString()
             {
                 //Return a String representing the card's value
                 switch (value)
                 {
                      case 1: return "Ace";
                      case 2: return "2";
                      case 3: return "3";
                      case 4: return "4";
                      case 5: return "5";
                      case 6: return "6";
                      case 7: return "7";
                      case 8: return "8";
                      case 9: return "9";
                      case 10: return "10";
                      case 11: return "Jack";
                      case 12: return "Queen";
                      case 13: return "King";
                      default: return "??";
                 }
             }
          
             public String toString()
             {
                  //Return a String representation of this card
                  return getValueAsString() + " of " + getSuitAsString();
             }
          
             //Getter method for the value
             public String getValue()
              {
                  if (value == ace) 
                  return "Ace";
                  else if (value == jack) 
                  return "Jack";
                  else if (value == queen) 
                  return "Queen";
                  else if (value == king) 
                  return "King";
                  return "Invalid Value";
          
              }
            
              //Getter method for the suit
              public String getSuit()
              {
                  if (suit == spades) 
                  return "Spades";
                  else if (suit == hearts) 
                  return "Hearts";
                  else if (suit == diamonds) 
                  return "Diamonds";
                  else if (suit == clubs) 
                  return "Clubs";
                  return "Invalid Value";
              }    
          
          }
          Last edited by KoreyAusTex; Apr 15 '08, 01:55 AM. Reason: messed up html tag

          Comment

          • sukatoa
            Contributor
            • Nov 2007
            • 539

            #6
            Could you post your expected correct output?

            I like to test your code.....

            sukatoa

            Comment

            • sukatoa
              Contributor
              • Nov 2007
              • 539

              #7
              Originally posted by sukatoa
              Could you post your expected correct output?

              I like to test your code.....

              sukatoa
              Try to put an else statement instead of directly return a value after the last ( else if ).. try to observe what happens,

              Update us,
              sukatoa

              Comment

              Working...