right I am trying to practice with my programming taking my lectures advice and breaking the problem down into steps, then when I code, code a small amount, compile it check it works and then code the next bit. So I have an exercise I am trying out. Part of what I need to do I am not sure of. I know I have to convert an int to a string but I am not sure which one.

Card.Java
This is a simple class that represents a playing card.

-rank: String
-suit: String
+card(int,String)
+getSuit(): String
+getRank(): String
Card has two attributes: rank which is a String that represents the value of a card. It takes the values “ACE,”2”,”3”,…,”JACK”,”QUEEN”,”KING”; suit a String which takes the values “SPADES”,”DIAMONDS”,”CLUBS”,”HEARTS”.

The class has a single constructor which takes two parameters: the first is an int that represents the rank in the range 1 to 13; the second a String representing the suit. The constructor must convert the int into an appropriate String value.

Additionally there are two methods that return String representations of the suit and rank respectively.

here is what I have planned

I need 2 arrays 1 for suit and 1 for rank which should contain all the card types numbers on cards and suits.
converting an int to a string can be done using toString. however I am not sure what String I am converting the int 2 I think it is converting it to rank but I am not sure. The int seems to me to be representative of the rank.

So I have basic code looking something like this

public class Card
{
getSuit();
getRank();

publlic Card(int rankNum, String suit)
{
       for (int i=1; i<14; i++)
       {
      rank = Integer.toString(i);

      }
}

public String getSuit()
{
String  suit [4]={"spades","hearts","diamonds","clubs"};
return suit;
}

public String getRank()
{
String  suit [13]={"ACE","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
return rank;
}

The biggest problem I have is that this seems only possible using arrays but the problem has suit and rank not as arrays but needs to be able to take those values. I am seriously confused. Any correction on my planning would be much appreciated particularly with the bit i highlighted in bold.

No, no, no. The way I see it each Card object must represent a single card the suit, rank must not be arrays. The constructor will take the 2 values and you will pass these values to your class attributes.
Both attributes will be Strings. You will have no problem with suit. Just remember at the constructor to check: if the input suit is not “SPADES”,”DIAMONDS”,”CLUBS”,”HEARTS”, then throw an Exception or print an error message.
As for rank:
Now remember the constructor's rank will be int.
if the argument is between 2 and 10 just turn the int into a String

this.rank = String.valueOf(rank); //it will the int rank(argument) into a String and store it into the this.rank(String attribute of the class)

if it is 1,11,12,13 then put the necessary if-statements in order to store what is needed to rank. Then create 52 different Card objects (use 4 for-loops) and you have yourself a deck

commented: thnx 4 ur help +1
public class Card
{
[B]getSuit();  // These are invalid calls - you cannot do this here
getRank();[/B]

[B]// you have not declared rank and suit member variables

// you can have a static array for the rank values here and initialize it with 
// a static block.[/B]

publlic Card(int rankNum, String suit)
{
      [B] // this won't really do anything for you at all and will
       // not compile because rank is not defined[/B]
       for (int i=1; i<14; i++)
       {
      rank = Integer.toString(i);

      }

     [B] // with the static rank value array you can assign the value of the rank string
      // with a simple array lookup
      // rank = rankValues[ rankNum ];[/B]
     
}

public String getSuit()
{
[B]// just needs to return the suit string - no array here[/B]
String  suit [4]={"spades","hearts","diamonds","clubs"};
return suit;
}

public String getRank()
{
[B]// Same here - return rank - no array needed here[/B]
String  suit [13]={"ACE","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
return rank;
}
commented: you have helped me a lot with java thnx +1

thanks both of you. Ezzaral you said I couldn't call the getter methods in the Card class. Is that because getter methods can only be called in a main method?

Those calls were not in a method, they were in the class body, which the compiler will read as attempt to declare the method with an invalid signature.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.