2 Replies - 10122 Views - Last Post: 03 March 2009 - 09:11 PM Rate Topic: -----

#1 Citadel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-February 09

Help with describing deck of cards?

Posted 03 March 2009 - 07:36 PM

Hey all,

Our assignment was to

"Write a program that takes user input describing a playing card in the following shorthand notation:
A = Ace
2 . . . 10 = Card Values
J = Jack
Q = Queen
K = King
D = Diamonds
H = Hearts
S = Spades
C = Clubs

Your program should print the full description of the card. For example,

Quote

Enter the card notation:
4S
Four of spades


"


So, this is what I have for the Card Class
public class Card  
{ 
  
	private String rank; 
	private String suit; 
	public String card;
	public String description;
	
	
 
	public Card(int rank, String suits) 
	{		
		this.suit = "Diamonds"; 
		this.suit = "Hearts";
		this.suit = "Spades";
		this.suit = "Clubs";
  
		if (rank == 1) 
		{ 
			this.rank = "Ace"; 
		} 
		else if (rank == 11) 
		{ 
			this.rank = "Jack"; 
		} 
		else if (rank == 12) 
		{ 
			this.rank = "Queen"; 
		} 
		else if (rank == 13) 
		{ 
			this.rank = "King"; 
		} 
		else 
		{ 
			this.rank = "" + rank; 
		  } 
	} 
	public Card(String input) {
		
	}
	public String getSuit()  
	{ 
		return suit; 
	} 
	public String getRank()  
	{ 
		return rank; 
	} 
	public String toString() { return rank + " " + suit; } 
	
	public String getCard()
	{
		return suit + rank;
	}
	public String getDescription()
	{
		return suit + rank;
	}
	
	{ 
		
	} 
  
} 


And this is the given tester class:
import java. util.Scanner;

/**
   This is a test for the Card class, which outputs the full
   description of a deck of cards.
*/
public class CardPrinter
{
   public static void main(String[] args)
   {
	  Scanner in = new Scanner(System.in);

	  System.out.println("Enter the card notation:");
	  String input = in.nextLine();
	  Card card = new Card(input);
	  System.out.println(card.getDescription());
   }
}




What am I doing wrong? I keep getting 'null' once running. I'm new to java so I apologize for any novice mistakes.

Thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with describing deck of cards?

#2 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with describing deck of cards?

Posted 03 March 2009 - 08:17 PM

This is obviusly wrong

	public Card(int rank, String suits) 
	{		
		this.suit = "Diamonds"; 
		this.suit = "Hearts";
		this.suit = "Spades";
		this.suit = "Clubs";



you just put "Diamonds" in suit
then erase the just stored value with "Hearts"
then erase...
anywhow suit will always ends up by holding "Clubs"

what is the use of the String suits parameter ?
you probably want:

this.suit = suit;



Anyhow, this does not really matter as this constructor is never called.

The only constructor called is that one

Card card = new Card(input);



and the code of this constructor is

   public Card(String input) {
		
	}


which basically does nothing... does not set the value of this.suit and this.rank

so... normal that the print method returns null

If we want to seriously respect your teacher requirement you should input according his notation so:
input will equals AD (Ace Diamonds) or 3H (3 of Hearts) or KC (King of Clubs) .....

Then your constructor should be:

Card(String input) {
   // isolate the 2 characters
   rank = input.subString(0,1);
   suit = input.subString(1,2);
}


Was This Post Helpful? 1
  • +
  • -

#3 Citadel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-February 09

Re: Help with describing deck of cards?

Posted 03 March 2009 - 09:11 PM

Thanks for your feedback pbl.

I've edited my code now:

public class Card  
{ 
  
	private String rank; 
	private String suit; 
	public String card;
	public String description;
	
	
 
	public Card(int rank, String suits) 
	{		
		if (suit == "D")
		{
			this.suit = "Diamonds";
		}
		else if (suit == "H")
		{
			this.suit = "Hearts";
		}
		else if (suit == "S")
		{
			this.suit = "Spades";
		}
		else if (suit == "C")
		{
			this.suit = "Clubs";
		}
		else 
		{ 
			this.suit = "" + rank; 
		  } 
		if (rank == 1) 
		{ 
			this.rank = "Ace"; 
		} 
		else if (rank == 11) 
		{ 
			this.rank = "Jack"; 
		} 
		else if (rank == 12) 
		{ 
			this.rank = "Queen"; 
		} 
		else if (rank == 13) 
		{ 
			this.rank = "King"; 
		} 
		else 
		{ 
			this.rank = "" + rank; 
		  } 
	} 
	Card(String input) {
		   // isolate the 2 characters
		   rank = input.substring(0,1);
		   suit = input.substring(1,2);
		   
		}

	
	public String getDescription()
	{
		return this.suit + this.rank;
	}
	
}



The only issue I face now is for the Card(String input), I don't quite understand what exactly to do with. If ran, the program will simply give you a reversed input.

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1