Results 1 to 5 of 5
- 08-20-2011, 02:31 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Need help with get/set & arrayList in multiple methods
I'm currently doing a project for my course and got totally lost with the object interaction part, read my teaching material couple of times but still couldn't get the codes right. The question goes:
and heres my code:1:Write a class Card.java which represents one playing card. It should have
a constructor to create a card from a suit and a rank. You should provide appropriate get/set methods.
a method String toString() to return the name of the card, e.g. "King Diamond"
2: Write a class Pack.java which represents a pack of 52 playing cards. Every card in the pack must be unique. It should have
a constructor to create a new pack of 52 cards. It should have 4 suits, each suit should have 13 cards of different ranks.
a method void shuffle() to randomise the sequence of cards in the pack.
a method Card dealCard() to return the card from the top of the pack.
a method getCard(int n) to return the nth card from the pack.
3: Write a main program PackTest to test your class Pack. The program should create a new pack and print the names of all 52 cards one per line:
1: Ace Heart
2: Two Heart
...
52: King Spade
Card
Pack: This method will setup an ArrayList consist of 52 unique card with a value and shapepublic class Card {
static String cardIdentity;
public Card(String cardValue){
cardIdentity=cardValue;
}
public String showCard(){
return cardIdentity;
}
}
Pack Testimport java.util.ArrayList;
public class Pack {
ArrayList<Card> cardList=new ArrayList<Card>();
public Pack() {
String[] value={"Ace","Two","Three","Four","Five","Six","Se ven","Eight","Nine","Ten","Jack","Queen","King"};
String[] rank={"Spade","Heart","Diamond","Club"};
for(int v=0;v<52;v++){
int r=0;
if(v==12||v==25||v==38){
r++;
}
cardList.add(new Card(v+" "+r));
}
} //End of Pack method
public Card getCard(int g){
return cardList.get(g); // My method for getting the card name from a specific card, but it seemed wrong
}
}
public class PackTest {
public static void main(String[]args) {
Pack deck = new Pack();
for(int i=0;i<52;i++){
System.out.println((deck.getCard(i))); My problem starts here, it won't print the name of card instead it will print some wierd values eg.Card@3f317f
}
}
}Last edited by FiasseKrystella; 08-20-2011 at 02:36 PM.
- 08-20-2011, 03:09 PM #2
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
needs to be changed to...Java Code:cardList.add(new Card(v+" "+r));
You are only passing the indexes into the array as your string, you need to resolve the array values with the syntax there. I would make a nested loop. Your outer loop would run from 0 to value.length, counted by v. Your inner loop would run from 0 to rank.length and be counted by r.Java Code:cardList.add(new Card(value[v]+" "+rank[r]));
Then you can remove...
Java Code:int r=0; if(v==12||v==25||v==38){ r++;Needs to beJava Code:System.out.println((deck.getCard(i)));
You are just printing out a memory location, you need to further specify which part of a "Card" you want to print, in this case the string "cardIdentity".Java Code:System.out.println((deck.getCard(i).showCard()));
Should beJava Code:static String cardIdentity;
The static will make it so that every time you change one object, you change all of the objects and you'll have the same card, so we remove the "static" tag.Java Code:private String cardIdentity;
You want to make it a private variable because otherwise your accessor method (showCard method) is redundant. Making it private preserves encapsulation.Last edited by R-J; 08-20-2011 at 03:55 PM.
- 08-20-2011, 04:02 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Thank you very much for your help, R-15. I've managed to get it right and working from your assistance.
- 08-20-2011, 04:08 PM #4
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
No problem. I looked at your requirements and it looks like you need to rename your showCard() method to toString() as well.
- 08-20-2011, 08:39 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Multiple private methods
By java4deepak in forum New To JavaReplies: 4Last Post: 01-05-2011, 01:05 PM -
problems with multiple methods and paramaters...
By silafirion in forum New To JavaReplies: 13Last Post: 12-07-2010, 01:10 AM -
Multiple lines in arrayList
By Lund01 in forum Advanced JavaReplies: 6Last Post: 10-15-2010, 12:34 PM -
using multiple methods for graphics
By jforce93 in forum Advanced JavaReplies: 3Last Post: 04-25-2010, 06:05 PM -
JDBC that multiple methods will use
By evermore in forum JDBCReplies: 3Last Post: 03-16-2010, 07:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks