[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

stucking in dealing the cards of my CardDeck Class which takes Object as a parameter

Posted on 2004-08-07
2
Medium Priority
?
235 Views
Last Modified: 2010-03-31
Hello;

I tryign to continue in coding my game but have problems with my CardDeck class.
when I tried to code the Deal method in my CardDeck class, I couldn't use add and remove method from my LinkedList class to add a card to my new hand and then remove it from the deck( my add & remove methods are taking objects).


below is my program code with all classes.

///////////

/***
****** Card Clas
***/
public class Card implements Comparable{

  private int value;
  private int suit;
 
  public Card(int val, int s){
    value = val;
    suit = s;
  }
  public int getValue(){
    return value;
  }

  public int getSuit(){
    return suit;
  }

  public void setValue(int v){
    value = v;
  }

  public void setSuit(int s){
    suit = s;
  }
 
  public int compareTo (Object rhs) {
    Card card = (Card)rhs;
    if (this.suit < card.getsuit) return -1;
    if (this.suit > card.getsuit) return  1;
    if (this.value  < card.getvalue ) return -1;
    if (this.value  > card.getvalue ) return  1;
    return 0;
  }// end of CompareTo

  public String toString() {

      StringBuffer cardDescription = new StringBuffer();
     switch(value) {
         case 14 : cardDescription.append("Ace"); break;
         case 11 : cardDescription.append("Jack"); break;
         case 12 : cardDescription.append("Queen"); break;
         case 13 : cardDescription.append("King"); break;
         default :
             cardDescription.append( value + "" );
             break;
     }
     cardDescription.append(" of ");
     switch(suit) {
         case 1 : cardDescription.append("Clubs"); break;
         case 2 : cardDescription.append("Diamonds"); break;
         case 3 : cardDescription.append("Hearts"); break;
         case 4 : cardDescription.append("Spades"); break;
     }
     return cardDescription.toString();


  }//end of String
 
}

/***
****** Node Clas
***/
public class Node {


  private Object val;
  private  Node  next;
 
 
  // Constructors.
  public Node() {
   next = null;
   }
  public Node(Object v) {
   val = v; next = null;
    }
  public Node(Object v, Node n) {
   val = v; next = n;  
  }
 
 
  // Mutators.
  public void setData(Object v) {
   val = v;
    }
  public void setNext( Node  n) {
   next = n;
   }
  // Accessors.
  public int getData() {
   return val;
    }
  public Node getNext() {
   return next;
    }
}


/***
****** LinkedList Clas
***/
public class LinkedList {

      
  private Node head = null;
  private Node prev = null;
  private Node cur  = null;
 
 
  public boolean isEmpty() {
  return head==null;
 
 }
   
   
  public boolean contains(Object v) {
 
  boolean found = false;
  cur  = head;
  prev = null;
  while ((cur != null) && !found) {
   
    if (cur.getData().compareTo(v) == 0) //generic
    found = true;
    else {
      prev = cur;
      cur  = cur.getNext();
  }
  return found;
 
 }
   
   
  public int peek() {
 
  return cur.getData();
 
}
   
   public void insert(Object v)   {
  // Insert item (v) in list between previous (prev)
  //   and current (cur) nodes.
  // Pre-conditions : cur set to node immediately after
  //                    insertion point.
  //                  prev set to node immediately prior to cur.
  //                  If inserting at head of list,
  //                    prev == null and cur == head.
  //                  If inserting at tail of list,
  //                    prev set to last node and cur == null.
  // Post-conditions: item is in list.
  //                  cur set to the new node containing item.
  //                  prev set to immediate predecessor of item.
 
 
  cur = new Node(v, cur);   // Create, fill, & assign new node.
  if (prev == null) head = cur;  // Insert at head of list
  else prev.setNext(cur);        // or insert somewhere else.
}

   
   
 public void remove(<type> v) {
  // Pre-conditions: None.
  if (!contains(v))
    throw new NoSuchElementException();
  cur = cur.getNext()
  if (prev == null) head = cur;
  else prev.setNext(cur);
}

   
   
  public void print() {
  // Pre-conditions : None.
  // Post-conditions: All elements of list displayed.
  //                  cur is unchanged.
  Node n = head;
  while (n != null) {
    System.out.println(n.getData());
    n = n.getNext();
  }
}

   
   
  public void clear() {
 
  head =prev=cur=null;
   
 }
 
 
}//end of LinkedList


/***
****** CardDeck Clas
***/

public class CardDeck extends LinkedList {

      // instance variables
      private int numCards = 52;  //counter to keep track of the number of cards remaining in the deck
      
      
      private int idx;
      
      private Card[] deck;
      private LinkedList hand = new LinkedList();
      private Random generator = new Random();
            
      // constructor
      
      
      public void deal(Object v)
      {
            idx = generator.nextInt(numCards) + 1;
            deck.remove(idx);
//            hand.contains(idx)
            hand.insert(idx);
            
      }
      
      public void remove(Object v)
      {
            super(v);
      }
      public Object peek()
      {
            super();
      }
}


as you can see, the CardDeck class is a Linked list of the Card Class

any help please.
0
Comment
Question by:anosTekbot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 11744741
Card *is* an Object so you can add/remove them.
0
 
LVL 92

Expert Comment

by:objects
ID: 11750647
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question