Jump to content

Card program, help!!!

- - - - -

  • Please log in to reply
4 replies to this topic

#1
chubaka212

chubaka212

    Newbie

  • Members
  • Pip
  • 3 posts
I am stuck guys... so this assignment


Write a program that takes user input describing a playing card in the following shorthand notation:

[FONT=Courier New]Notation    Meaning
A           Ace
2 … 10      Card values
J           Jack
Q           Queen
K           King
D           Diamonds
H           Hearts
S           Spades
C           Clubs[/FONT]
Your program should print the full description of the card. For example,
Enter the card notation:
4S
Four of spades
so far i have this

public class Card
{
    private String notation;
    
    public Card (String not)
    {
        not = notation;
    }
    
    public String getDescription()
    {
        String c;
        String number = notation.substring (0, 0);
        String type = notation.substring(1);
        
        if (number == "A")
            c = "Ace of";
        else if (number == "2")
            c = "Two of";
        else if (number == "3")
            c = "Three of";
        else if (number == "4")
            c = "Four of";
        else if (number == "5")
            c = "Five of";
        else if (number == "6")
            c = "Six of";
        else if (number == "7")
            c = "Seven of";
        else if (number == "8")
            c = "Eight of";
        else if (number == "9")
            c = "Nine of";
        else if (number == "10")
            c = "Ten of";
        else if (number == "J")
            c = "Jack of";
        else if (number == "Q")
            c = "Queen of";
        else if (number == "K")
            c = "King of";
        else if (type == "S")
            c = "Spade";
        else if (type == "H")
            c = "Heart";
        else if (type == "D")
            c = "Diamond";
        else if (type == "C")
            c = "Club";
        else
            c = "unknown";
        return c;
    }
}


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());
   }
}
i guess there's a problem with substring. can anyone help me out how to straight it up? i am not too familar with substring. I tried out textbook method but it won't work for some reason

Edited by Alexander, 03 November 2010 - 07:03 PM.
Added code formatting


#2
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
Haha, well I'm actually in your class, I think. AP comp. sci with Mr. Volger?

You have to create a sum to hold the value of the card value, and it's notation.


public String getDescription()

	{

		String value = input.substring(0,input.length() -1);

		String notation = input.substring(input.length() -1);

		String sum = "";

		if(value.equals("A"))

			sum = "Ace " + "of ";

		else if((value.equals("2"))|| (value.equals("3")) || (value.equals("4")) || (value.equals("5")) || (value.equals("6")) || (value.equals("7")) || (value.equals("8")) || (value.equals("9")) || (value.equals("10")))

		{

			sum = value + " of ";

		}

		else if(value.equals("J"))

			sum = "Jack " + "of ";

		else if(value.equals("Q"))

			sum = "Queen " + "of ";

		else if(value.equals("K"))

			sum = "King " + "of ";

		if(notation.equals("D"))

			sum += "Diamonds";

		else if(notation.equals("H"))

			sum += "Hearts";

		else if(notation.equals("S"))

			sum += "Spades";

		else if(notation.equals("C"))

			sum += "Clubs";

		else 

			sum = "";

		return sum;



#3
chubaka212

chubaka212

    Newbie

  • Members
  • Pip
  • 3 posts
hmm idk, i keep getting error msg. I basically copied and pasted your method see if it works



public class Card
{
private String input;

public Card (String not)
{
not = input;
}

public String getDescription()
{
String value = input.substring(0,input.length() -1);
String notation = input.substring(input.length() -1);
String sum = "";
if(value.equals("A"))
sum = "Ace " + "of ";
else if((value.equals("2"))|| (value.equals("3")) || (value.equals("4")) || (value.equals("5")) || (value.equals("6")) || (value.equals("7")) || (value.equals("8")) || (value.equals("9")) || (value.equals("10")))
{
sum = value + " of ";
}
else if(value.equals("J"))
sum = "Jack " + "of ";
else if(value.equals("Q"))
sum = "Queen " + "of ";
else if(value.equals("K"))
sum = "King " + "of ";
if(notation.equals("D"))
sum += "Diamonds";
else if(notation.equals("H"))
sum += "Hearts";
else if(notation.equals("S"))
sum += "Spades";
else if(notation.equals("C"))
sum += "Clubs";
else
sum = "unknown";
return sum;
}
}

but apparently main function is not reading the substring correctly

#4
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
Change the order of you constructor, input should come first.

#5
chubaka212

chubaka212

    Newbie

  • Members
  • Pip
  • 3 posts
O wow... stupid me. thank you so much, now it works




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users