473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Missing return statements?

KoreyAusTex
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()?

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class Card
  3. {
  4.     // instance variables
  5.     //suits
  6.     private int suit;
  7.     private int spades;
  8.     private int hearts;
  9.     private int diamonds;
  10.     private int clubs;
  11.  
  12.     //values
  13.     private int value;
  14.     private int ace;
  15.     private int jack;
  16.     private int queen;
  17.     private int king;
  18.  
  19.     // constants representing the suits and values
  20.     public static final int SPADES = 3;
  21.     public static final int HEARTS = 2;
  22.     public static final int DIAMONDS = 1;
  23.     public static final int CLUBS = 0;
  24.  
  25.     public static final int ACE = 1;
  26.     public static final int JACK = 11;
  27.     public static final int QUEEN = 12;
  28.     public static final int KING = 13;
  29.  
  30.     //constructors
  31.     //constructor that creates a random, valid card
  32.     public Card()
  33.     {
  34.         Random gen = new Random();
  35.         suit = gen.nextInt(4);
  36.         spades = 3;
  37.         hearts = 2;
  38.         diamonds = 1;
  39.         clubs = 0;
  40.  
  41.         value = gen.nextInt(13);
  42.         ace =1;
  43.         jack = 11;
  44.         queen = 12;
  45.         king = 13;
  46.    }
  47.  
  48.    //constructor that accepts two parameters.
  49.    //First parameter is the suit
  50.    //Second parameter is the card value
  51.  
  52.    public Card(int suit, int value)
  53.    {
  54.         this.suit = suit;
  55.         this.spades = spades;
  56.         this.diamonds = diamonds;
  57.         this.clubs = clubs;
  58.  
  59.         this.ace = ace;
  60.         this.jack = jack;
  61.         this.queen = queen;
  62.         this.king = king;
  63.    }
  64.  
  65.    //override equals() method inherited from object
  66.    public boolean equals(Object obj)
  67.    {
  68.        //check to see if object is a reference to the cards
  69.        //and if so, check if its value and suit equals the currecnt value and suit
  70.         if(obj instanceof Card)
  71.         {
  72.             Card crd = (Card) obj;
  73.             if ((value == crd.value) && (suit == crd.suit)) return true;
  74.             else return false;
  75.         }
  76.             else return false;
  77.    }
  78.  
  79.    // override close () method from the object class
  80.    public Object clone()
  81.    {
  82.         //create card that has the same instant values as the current card
  83.         Card newCard = new Card();
  84.         newCard.value = value;
  85.         newCard.suit = suit;
  86.         return newCard;
  87.    }
  88.  
  89.    //a compareValue() method that takes another card object and
  90.    //returns true if the current object has a greater value and
  91.    //false otherwise.
  92.    public boolean compareValue(Card other)
  93.    {
  94.         if(value >= value)
  95.         {
  96.             return true;
  97.         }
  98.         else return false;
  99.    }
  100.    //Getter method
  101.    public String getSuitAsString()
  102.    {
  103.        //Return a String representing the card's suit
  104.        switch ( suit )
  105.        {
  106.             case SPADES: return "Spades";
  107.             case HEARTS: return "Hearts";
  108.             case DIAMONDS: return "Diamonds";
  109.             case CLUBS: return "Clubs";
  110.             default: return "??";
  111.        }
  112.    }
  113.  
  114.    public String getValueAsString()
  115.    {
  116.        //Return a String representing the card's value
  117.        switch ( value )
  118.        {
  119.             case 1: return "Ace";
  120.             case 2: return "2";
  121.             case 3: return "3";
  122.             case 4: return "4";
  123.             case 5: return "5";
  124.             case 6: return "6";
  125.             case 7: return "7";
  126.             case 8: return "8";
  127.             case 9: return "9";
  128.             case 10: return "10";
  129.             case 11: return "Jack";
  130.             case 12: return "Queen";
  131.             case 13: return "King";
  132.             default: return "??";
  133.        }
  134.    }
  135.  
  136.    public String toString()
  137.    {
  138.         //Return a String representation of this card
  139.         return getValueAsString() + " of " + getSuitAsString();
  140.    }
  141.  
  142.    //Getter method for the value
  143.    public String getValue()
  144.    {
  145.         if (value == ace)
  146.         return "Ace";
  147.         else if (value == jack)
  148.         return "Jack";
  149.         else if (value == queen)
  150.         return "Queen";
  151.         else if (value == king)
  152.         return "King";
  153.    }
  154.  
  155.  
  156.    //Getter method for the suit
  157.    public String getSuit()
  158.    {
  159.         if (suit == spades) return "Spades";
  160.         else if (suit == hearts) return "Hearts";
  161.         else if (suit == diamonds) return "Diamonds";
  162.         else if (suit == clubs) return "Clubs";
  163.    }
  164.  
  165. }
  166.  
Apr 14 '08 #1
6 2381
sukatoa
539 512MB
@ 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....
Apr 15 '08 #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....
Would that also apply to getSuit, that is what I was thinking.
Apr 15 '08 #3
sukatoa
539 512MB
Would that also apply to getSuit, that is what I was thinking.
...........Yep............

Update us,
sukatoa
Apr 15 '08 #4
...........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:

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Short program that uses the Card class.
  3.  * NOTE: Not a comprehensive test!
  4.  * @version (10/1/04)
  5.  */
  6. public class CardTester
  7. {
  8.     public static void main (String [] args) {
  9.         Card c1 = new Card(); //creates a random card
  10.         Card c2 = new Card(1,12); //creates the Queen of Diamonds
  11.  
  12.         System.out.println("Card 1 value is - " + c1.getValue()); //note: this will print the numeric value
  13.         System.out.println("Card 2 suit is - " + c2.getSuit()); //note: this will print the numeric value
  14.  
  15.         System.out.println ("Card 1 is the " + c1.toString() + "." );
  16.                 System.out.println ("Card 2 is the " + c2.toString() + "." );
  17.     }
  18.  
  19. }
  20.  
Here is the fixed file:

Expand|Select|Wrap|Line Numbers
  1. public class Card
  2. {
  3.     //suits
  4.     private int suit;
  5.     private int spades;
  6.     private int hearts;
  7.     private int diamonds;
  8.     private int clubs;
  9.  
  10.     //values
  11.     private int value;
  12.     private int ace;
  13.     private int jack;
  14.     private int queen;
  15.     private int king;
  16.  
  17.     //constants representing the suits and values
  18.     public static final int SPADES = 3;
  19.     public static final int HEARTS = 2;
  20.     public static final int DIAMONDS = 1;
  21.     public static final int CLUBS = 0;
  22.  
  23.     public static final int ACE = 1;
  24.     public static final int JACK = 11;
  25.     public static final int QUEEN = 12;
  26.     public static final int KING = 13;
  27.  
  28.     //constructors
  29.     //constructor that creates a random, valid card
  30.     public Card()
  31.     {
  32.         Random gen = new Random();
  33.         suit = gen.nextInt(4);
  34.         spades = 3;
  35.         hearts = 2;
  36.         diamonds = 1;
  37.         clubs = 0;
  38.  
  39.         value = gen.nextInt(13);
  40.         ace = 1;
  41.         jack = 11;
  42.         queen = 12;
  43.         king = 13;
  44.    }
  45.  
  46.    //constructor that accepts two parameters.
  47.    //First parameter is the suit
  48.    //Second parameter is the card value
  49.  
  50.    public Card(int suit, int value)
  51.    {
  52.         this.suit = suit;
  53.         this.spades = spades;
  54.         this.diamonds = diamonds;
  55.         this.clubs = clubs;
  56.  
  57.         this.ace = ace;
  58.         this.jack = jack;
  59.         this.queen = queen;
  60.         this.king = king;
  61.    }
  62.  
  63.    //override equals() method inherited from object
  64.    public boolean equals(Object obj)
  65.    {
  66.        //check to see if object is a reference to the cards
  67.        //and if so, check if its value and suit equals the currecnt value and suit
  68.         if(obj instanceof Card)
  69.         {
  70.             Card crd = (Card) obj;
  71.             if ((value == crd.value) && (suit == crd.suit)) return true;
  72.             else return false;
  73.         }
  74.             else return false;
  75.    }
  76.  
  77.    // override close () method from the object class
  78.    public Object clone()
  79.    {
  80.         //create card that has the same instant values as the current card
  81.         Card newCard = new Card();
  82.         newCard.value = value;
  83.         newCard.suit = suit;
  84.         return newCard;
  85.    }
  86.  
  87.    //a compareValue() method that takes another card object and
  88.    //returns true if the current object has a greater value and
  89.    //false otherwise.
  90.    public boolean compareValue(Card other)
  91.    {
  92.         if(value >= value)
  93.         {
  94.             return true;
  95.         }
  96.         else return false;
  97.    }
  98.    //Getter method
  99.    public String getSuitAsString()
  100.    {
  101.        //Return a String representing the card's suit
  102.        switch (suit)
  103.        {
  104.             case SPADES: return "Spades";
  105.             case HEARTS: return "Hearts";
  106.             case DIAMONDS: return "Diamonds";
  107.             case CLUBS: return "Clubs";
  108.             default: return "??";
  109.        }
  110.     }
  111.  
  112.    public String getValueAsString()
  113.    {
  114.        //Return a String representing the card's value
  115.        switch (value)
  116.        {
  117.             case 1: return "Ace";
  118.             case 2: return "2";
  119.             case 3: return "3";
  120.             case 4: return "4";
  121.             case 5: return "5";
  122.             case 6: return "6";
  123.             case 7: return "7";
  124.             case 8: return "8";
  125.             case 9: return "9";
  126.             case 10: return "10";
  127.             case 11: return "Jack";
  128.             case 12: return "Queen";
  129.             case 13: return "King";
  130.             default: return "??";
  131.        }
  132.    }
  133.  
  134.    public String toString()
  135.    {
  136.         //Return a String representation of this card
  137.         return getValueAsString() + " of " + getSuitAsString();
  138.    }
  139.  
  140.    //Getter method for the value
  141.    public String getValue()
  142.     {
  143.         if (value == ace) 
  144.         return "Ace";
  145.         else if (value == jack) 
  146.         return "Jack";
  147.         else if (value == queen) 
  148.         return "Queen";
  149.         else if (value == king) 
  150.         return "King";
  151.         return "Invalid Value";
  152.  
  153.     }
  154.  
  155.     //Getter method for the suit
  156.     public String getSuit()
  157.     {
  158.         if (suit == spades) 
  159.         return "Spades";
  160.         else if (suit == hearts) 
  161.         return "Hearts";
  162.         else if (suit == diamonds) 
  163.         return "Diamonds";
  164.         else if (suit == clubs) 
  165.         return "Clubs";
  166.         return "Invalid Value";
  167.     }    
  168.  
  169. }
  170.  
Apr 15 '08 #5
sukatoa
539 512MB
Could you post your expected correct output?

I like to test your code.....

sukatoa
Apr 15 '08 #6
sukatoa
539 512MB
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
Apr 15 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: sureshjayaram | last post by:
In some functions where i need to return multiple error codes at multiple places, I use multiple return statements. Say for ex. if (Found == 1) { if (val == -1) return error1; } else { if...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
14
by: tshad | last post by:
I am trying to set up a reusable library (business component) that I can use in a bunch of my pages. I built the file and it almost compiles, but I am not sure what I am missing. The vbc...
12
by: John Sidney-Woollett | last post by:
<rant> Please can someone explain why Postgres cannot recognize that objects (referenced by pl/pgsql functions) whose OID no longer exists could in fact be found (as new objects) if the function...
2
by: fritz-bayer | last post by:
Hi, in a shell script of mine I'm executing a mysql SELECT in batch mode. I want to use the return value for other sql statements. The problem I'm facing is that the tabs, which seperate the...
9
by: DavidB | last post by:
Hi all I have a script that works perfectly in IE but not in FF. I am sure that the problem is easy to resolve, but I seem to be too dumb to figure it out. <html> <head> <script...
3
by: Fred Chateau | last post by:
Still working on my XML DataSet... Having moved on past difficult and complex problems, resolved with the assistance of everyone here, I find myself facing yet another problem. My XML document...
1
by: macracan | last post by:
I'm wondering what was the rationale that caused gcc to only have a warning (disabled by default!!) to signal the following: int a() {}; // intentionally no statements int main() {...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.