4 Replies - 334 Views - Last Post: 29 November 2012 - 12:33 PM Rate Topic: -----

#1 KendraSullivan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-November 12

Card Class problem

Posted 28 November 2012 - 08:25 PM

I had to create a class called Card that represents a standard playing card. However, my teacher has now asked us to modify this card class so that the getFaceName() and getSuitName() accessors use direct indexing to retrieve the name from arrays with initializer lists. I'm just kind of confused on how to go about this, any help would be great! Thanks



import java.util.Random;

public class Card {
	private String suit;
	private String face;
	private int suitName;
	private int faceName;

public Card ()	{
	face = "";
 	suit = "";
 	suitName = 0;
 	faceName = 0;
}

public Card (String suitName, String faceName) {
 	face = faceName;
 	suit = suitName;
}

 public String Suit() {
 	return suit;
 }

public String Face() {
	return face;
}

public void setSuit (String suitIn){
	suit = suitIn;
}

public void setFace (String faceIn) {
	face = faceIn;
}

public String getSuit() {
	Random rng = new Random();
	suitName=rng.nextInt (3) + 1;
	switch (suitName){
		case 1:
			suit = "Clubs";
			break;
		case 2:
			suit = "Spades";
			break;
		case 3:
			suit  = "Diamonds";
			break;
		case 4:
			suit = "Hearts";
			break;
	}
	return suit;
 }
public String getFace () {
	Random rng = new Random();
	faceName=rng.nextInt (12) + 1;
	switch (faceName) {
		case 1:
			face = "Ace";
			break;
		case 2:
			face = "Two";
			break;
		case 3:
			face = "Three";
			break;
		case 4:
			face = "Four";
			break;
		case 5:
			face = "Five";
			break;
		case 6:
			face = "Six";
			break;
		case 7:
			face = "Seven";
			break;
		case 8:
			face = "Eight";
			break;
		case 9:
			face = "Nine";
			break;
		case 10:
			face = "Ten";
			break;
		case 11:
			face = "Jack";
			break;
		case 12:
			face = "Queen";
			break;
		case 13:
			face = "King";
			break;
	}
	return face;
}

}


MOD EDIT: Added code tags and moved to Java. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 28 November 2012 - 09:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Card Class problem

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: Card Class problem

Posted 28 November 2012 - 09:02 PM

Everyone else can use code tags. Why not you?

:code:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 KendraSullivan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-November 12

Re: Card Class problem

Posted 28 November 2012 - 09:08 PM

Sorry! I'm new to this, just trying to get the hang of how it all works.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Card Class problem

Posted 29 November 2012 - 06:28 AM

View PostKendraSullivan, on 28 November 2012 - 10:25 PM, said:

use direct indexing to retrieve the name from arrays with initializer lists.


Right, this would be an array with an initializer list:
String [] a = new String[]{ "Alice", "Bob", "Chuck" };



To get the second value in that array, you'd just call a[1] ( because arrays a zero indexed .) This is should give you some ideas.

Note, your class seems kind of backwards. Use name for the name. e.g.
public class Card {
	private int suit;
	private int face;
	// you don't need these
	// private int suitName;
	// private int faceName;
	// define a couple of arrays here

	public Card () { this(0,0); }

	// the name isn't going to do anything for you
	// public Card (String suitName, String faceName) {
	public Card (int suit, int face) {
	 	this.suit = suit;
	 	this.face = face;
	}

	// be consistent, use get
	// public String Suit() {
	public int getSuit() { return suit; }
	public int getFace() { return face; }

	// you may not need setters
	// just use the constructor
	// public void setSuit (String suitIn){
	// public void setFace (String faceIn) {


	public String getSuitName() {
		// this is where you'd call an array
		// NO random here
		// Random rng = new Random();
		// you will use suit
	}
	public String getFaceName() { /* your code here */ }
}



Hope this helps.

This post has been edited by baavgai: 29 November 2012 - 06:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

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

Re: Card Class problem

Posted 29 November 2012 - 12:33 PM

View PosttlhIn`toq, on 29 November 2012 - 12:02 AM, said:

Everyone else can use code tags. Why not you?


Come on tlhIn`toq it is the OP first post
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1