Missing return statements?

Not sure what the problem is, but I have been working for the last few

Page 1 of 1

8 Replies - 1214 Views - Last Post: 14 April 2008 - 08:59 PM Rate Topic: -----

#1 KoreyAusTex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-July 07

Missing return statements?

Post icon  Posted 14 April 2008 - 04:54 PM

I am just having a hard time figuring out the "return statement missing error"? Any advice?

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";
   }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Missing return statements?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Missing return statements?

Posted 14 April 2008 - 05:53 PM

View PostKoreyAusTex, on 14 Apr, 2008 - 04:54 PM, said:

I am just having a hard time figuring out the "return statement missing error"? Any advice?



   //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";
   }



You know that this method will return Ace, Jack, Queen or King and cannot return anything else but the compiler does not know that.
So it says: "you must return a String and you don't.....
so you have to do something like that

   //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";
		System.out.println("Bug getValue didin't find it");
		// you have to return a String
		return "garbage";
   }



Or if you are really really really sure that the condition will never occurs you can

   //Getter method for the value
   public String getValue()
   {
		if (value == ace)
		return "Ace";
		if (value == jack)
		return "Jack";
		f (value == queen)
		return "Queen";
	
		return "King";
   }






Same thing... yes it is Spades, Hearts, Diamonds or Clubs
   //Getter method for the suit
   public String getSuit()
   {
		if (suit == spades) return "Spades";
		if (suit == hearts) return "Hearts";
		if (suit == diamonds) return "Diamonds";

		return "Clubs";
   }




Note: else if are useless if the statements executed in the if end by a return statement
Was This Post Helpful? 0
  • +
  • -

#3 KoreyAusTex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-July 07

Re: Missing return statements?

Posted 14 April 2008 - 06:56 PM

ok got everything and used a cardtester program:

/**
 * 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() + "." );
	}
		
}



Then I get this output, something is not right:

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.

Here is the fixed file:

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";
	}	

}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Missing return statements?

Posted 14 April 2008 - 07:17 PM

View PostKoreyAusTex, on 14 Apr, 2008 - 06:56 PM, said:

ok got everything and used a cardtester program:


[code]
//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;
}
[/quote]

What are you trying to do here ?

this.suit = suit;

is the only line that makes sense. You assign to the variable suite of your object card the value of suite passed as parameter
That is why you use the this.suite to say the suite of this object, not the parameter.

I guess you have a

this.value = value;

missing.

Now for the other there are no parameter spades, diamonds, clubs, ace, jack, queen, king
So
this.spades = spades;
this.diamonds = diamonds;
this.clubs = clubs;

this.ace = ace;
this.jack = jack;
this.queen = queen;
this.king = king;

is equivalent to:

this.spades = this.spades;
this.diamonds = this.diamonds;
....

or even

spades = spades;
diamonds = diamonds;
......

Basically you are doing nothing.

May be you wanted to say:

spades = SPADES;
diamonds = DIAMONDS;
.....
but then why are you duplicating your definition ?
The class has a static variable name DIAMONDS which is shared by all instances (all the object) of Card no need to duplicate this value in all your occurence.

This post has been edited by pbl: 14 April 2008 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Missing return statements?

Posted 14 April 2008 - 07:38 PM

Your compareValue method is also wrong you reference the same value twice

I guess that this is what you want to do (after some cleaning up)

import java.util.Random;

public class Card
{
    //suits
    private int suit;
    //values
    private int value;

    //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);
        value = gen.nextInt(13) + 1;   // might returns 0
   }

   //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.value = value;
   }

   //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;
            return ((value == crd.value) && (suit == crd.suit));
        }
        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(suit, value);
        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)
   {
        return value >= other.value;
   }
   //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 ACE: return "Ace";
            case JACK: return "Jack";
            case QUEEN: return "Queen";
            case KING: return "King";
            // so it is between 2 and 10
            default: return "" + value;
       }
   }

   public String toString()
   {
        //Return a String representation of this card
        return getValueAsString() + " of " + getSuitAsString();
   }

   //Getter method for the value
   // don't see really the difference between this method
   // and the getValueAsString() method
   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 "" + 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";
    }    

    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() + "." );
    }

}



Edited to correct the random.nextInt() for the value

This post has been edited by pbl: 14 April 2008 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 KoreyAusTex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-July 07

Re: Missing return statements?

Posted 14 April 2008 - 08:24 PM

[quote name='pbl' date='14 Apr, 2008 - 07:17 PM' post='340932']

View PostKoreyAusTex, on 14 Apr, 2008 - 06:56 PM, said:

ok got everything and used a cardtester program:


   //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;
   }
[/quote]

What are you trying to do here ?

this.suit = suit;

is the only line that makes sense. You assign to the variable suite of your object card the value of suite passed as parameter
That is why you use the this.suite to say the suite of this object, not the parameter.

I guess you have a 

this.value = value;

missing.

Now for the other there are no parameter spades, diamonds, clubs, ace, jack, queen, king
So
		this.spades = spades;
		this.diamonds = diamonds;
		this.clubs = clubs;

		this.ace = ace;
		this.jack = jack;
		this.queen = queen;
		this.king = king;

 is equivalent to:

this.spades = this.spades;
this.diamonds = this.diamonds;
....

 or even

spades = spades;
diamonds = diamonds;
......

Basically you are doing nothing.

 May be you wanted to say:

spades = SPADES;
diamonds = DIAMONDS;
.....
but then why are you duplicating your definition ?
The class has a static variable name DIAMONDS which is shared by all instances (all the object) of Card no need to duplicate this value in all your occurence.
[/quote]

[CODE]
public Card(int suit, int value)
   {
		this.suit = suit;
		this.value = value;
   }



is this better?
So then this would be better?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Missing return statements?

Posted 14 April 2008 - 08:32 PM

We cross-posted...

2 topics before I put you all the code....

As you seem to like the switch statement may be I should have done:

    public String getSuit()
    {
    	switch(suit) {
    	case SPADES:
    		return "Spades";
    	case HEARTS:
    		return "Hearts";
    	case DIAMONDS:
    		return "Diamonds";
    	case CLUBS:
    		return "Clubs";
    	}
    	return "Invalid Value";
    }    

 

This post has been edited by pbl: 14 April 2008 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Missing return statements?

Posted 14 April 2008 - 08:45 PM

View Postpbl, on 14 Apr, 2008 - 08:32 PM, said:

We cross-posted...

2 topics before I put you all the code....

As you seem to like the switch statement may be I should have done:

    public String getSuit()
    {
    	switch(suit) {
    	case SPADES:
    		return "Spades";
    	case HEARTS:
    		return "Hearts";
    	case DIAMONDS:
    		return "Diamonds";
    	case CLUBS:
    		return "Clubs";
    	}
    	return "Invalid Value";
    }    

 


If you want to spend another 15 minutes on this I can simplify more the code and get rid of all the swicth and if statements.

BTW your comments are wrong in your main() method... getValue() does not return the numeric value but a String that is not defined for the values 2 to 10
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Missing return statements?

Posted 14 April 2008 - 08:59 PM

Here is the re-vamp version...

No switch, no if

getValue() and getSuit() return an int as they should....

The static int are gone... code a lot shorter...

import java.util.Random;

public class Card
{
    //suits
    private int suit;
    //values
    private int value;

    private static final String[] suitStr = {"Clubs", "Diamonds", "Hearts", "Clubs"};
    private static final String[] valueStr = {"??","Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};

    private static Random gen = new Random();
    //constructor that creates a random, valid card
    public Card() {
        suit = gen.nextInt(4);
        value = gen.nextInt(13) + 1;
   }

   //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.value = value;
   }

   //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;
            return ((value == crd.value) && (suit == crd.suit));
        }
        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(suit, value);
        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) {
        return value >= other.value;
   }
   //Getter method
   public String getSuitAsString() {
	   return suitStr[suit];
    }

   public String getValueAsString() {
	   return valueStr[value];
   }

   public String toString()
   {
        //Return a String representation of this card
        return getValueAsString() + " of " + getSuitAsString();
   }

   //Getter method for the value as a int
   private int getValue() {
	   return value;
   }
  
    //Getter method for the suit
    public int getSuit() {
    	return suit;
    }    

    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() + "." );
    }

}



Enjoy

Edited add forgotten "Ace" in valueStr.

This post has been edited by pbl: 14 April 2008 - 09:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1