Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default 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:


    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
    and heres my code:

    Card
    public class Card {
    static String cardIdentity;
    public Card(String cardValue){
    cardIdentity=cardValue;
    }
    public String showCard(){
    return cardIdentity;
    }
    }
    Pack: This method will setup an ArrayList consist of 52 unique card with a value and shape
    import 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
    }

    }
    Pack Test
    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.

  2. #2
    R-J
    R-J is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    cardList.add(new Card(v+" "+r));
    needs to be changed to...
    Java Code:
    cardList.add(new Card(value[v]+" "+rank[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.

    Then you can remove...
    Java Code:
    int r=0;
    if(v==12||v==25||v==38){
    r++;
    Java Code:
    System.out.println((deck.getCard(i)));
    Needs to be
    Java Code:
    System.out.println((deck.getCard(i).showCard()));
    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:
    static String cardIdentity;
    Should be
    Java Code:
    private 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.

    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.

  3. #3
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default

    Thank you very much for your help, R-15. I've managed to get it right and working from your assistance.

  4. #4
    R-J
    R-J is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    No problem. I looked at your requirements and it looks like you need to rename your showCard() method to toString() as well.

  5. #5
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default

    Yes. I was still thinking of how to create the toString() method not realizing that I've already created the same method of a diff. name. Lol

Similar Threads

  1. Multiple private methods
    By java4deepak in forum New To Java
    Replies: 4
    Last Post: 01-05-2011, 01:05 PM
  2. problems with multiple methods and paramaters...
    By silafirion in forum New To Java
    Replies: 13
    Last Post: 12-07-2010, 01:10 AM
  3. Multiple lines in arrayList
    By Lund01 in forum Advanced Java
    Replies: 6
    Last Post: 10-15-2010, 12:34 PM
  4. using multiple methods for graphics
    By jforce93 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2010, 06:05 PM
  5. JDBC that multiple methods will use
    By evermore in forum JDBC
    Replies: 3
    Last Post: 03-16-2010, 07:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •