473,779 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Missing return statements?

KoreyAusTex
36 New Member
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 2410
sukatoa
539 Contributor
@ 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
KoreyAusTex
36 New Member
@ 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 Contributor
Would that also apply to getSuit, that is what I was thinking.
...........Yep. ...........

Update us,
sukatoa
Apr 15 '08 #4
KoreyAusTex
36 New Member
...........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 Contributor
Could you post your expected correct output?

I like to test your code.....

sukatoa
Apr 15 '08 #6
sukatoa
539 Contributor
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
3650
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 (val2 == -1)
102
5718
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 can't tell where a bracket is missing.... a few weeks have past, I requested a feature for the delphi ide/editor "automatic identation of code in begin/end statements etc" and today when I woke up I suddenly released a very simple solution for this...
15
2808
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
14
1837
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 command and results are: ************************************************************************ C:\Inetpub\wwwroot\Development>vbc /t:library emailClass.vb
12
2063
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 was reparsed and compiled again. Here's an example: Create table t1 (f1 integer);
2
1376
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 columns from each other, are missing in the output. What am I doing wrong?
9
1957
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 language="javascript">
3
2832
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 breaks the schema. There are missing tags everywhere, on purpose I'm told, because we don't need them. I'm getting a "System.Data: There is no row at position <row number>" error. Hopefully I can workaround this issue. I need to find a way to...
1
3627
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() { printf("%d", a()); return 0;
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9930
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.