incompatible types

Deck Class for war card game simulatuin

Page 1 of 1

10 Replies - 569 Views - Last Post: 08 March 2010 - 03:28 PM Rate Topic: -----

#1 Guest_Bryce Miller*


Reputation:

incompatible types

Posted 08 March 2010 - 01:49 PM

Deck class CODE

/*
 * War Simulation
 * Deck Class
 * contains 52 cards 13 of each suit
 *
 * 3/6/10							
 * 
 * @author D. Bryce Miller
 */
 
 import java.util.*;
 
 public class Deck
 {
 	 //the deck
 	 private LinkedList m_theDeckOfCards;
 	 
 	 // suits
 	 private Card m_spades;
 	 private Card m_hearts;
 	 private Card m_diamonds;
 	 private Card m_clubs;
 	 
 	 //card removed when dealing
 	 private Card removedCard = new Card();
 	 
 	 
 	 //constructor - creates an empty linked list
 	 public Deck()
 	 {
 	 	 m_theDeckOfCards = new LinkedList();
 	 }
 	 
 	 //adds all the cards to the deck
 	 public void fillDeck()
 	 {
 	 	 for (int i=2; i<15; ++i)
 	 	 {
 	 	 	 m_theDeckOfCards.addLast(m_spades = new Card("spades", i));
 	 	 	 m_theDeckOfCards.addLast(m_hearts = new Card("hearts", i));
 	 	 	 m_theDeckOfCards.addLast(m_diamonds = new Card("diamonds", i));
 	 	 	 m_theDeckOfCards.addLast(m_clubs = new Card("clubs", i));
 	 	 }
 	 	 System.out.println(m_theDeckOfCards);
 	 }
 	 
 	 /*picks a random card from the deck to deal
 	 **by removing a random card from within the deck
 	 */
 	 public Card deal()
 	 {
 	 	 Random generate = new Random();
 	 	 removedCard = m_theDeckOfCards.remove(generate.nextInt(m_theDeckOfCards.size()));
 	 	 System.out.println(removedCard);
 	 	 return removedCard;
 	 	 
 	 }
 	 
 	 
 	// Issue here 
 	 public static void main(String[] args)
 	 {
 	 	 Deck test = new Deck();
 	 	 test.fillDeck();
 	 	 test.deal();
 	 }
 }


This post has been edited by Dogstopper: 08 March 2010 - 03:25 PM
Reason for edit:: Post between code tags and no smilies in code...


Is This A Good Question/Topic? 0

Replies To: incompatible types

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: incompatible types

Posted 08 March 2010 - 01:51 PM

can you please post your code correctly

:code:

also state your question in the post
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Bryce Miller*


Reputation:

Re: incompatible types

Posted 08 March 2010 - 01:51 PM

http://paste.ubuntu.com/391272/

a link to my code in a pastebin
Was This Post Helpful? 0

#4 Guest_Bryce Miller*


Reputation:

Re: incompatible types

Posted 08 March 2010 - 01:54 PM

/*
 * War Simulation
 * Deck Class
 * contains 52 cards 13 of each suit
 *
 * 3/6/10							
 * 
 * @author D. Bryce Miller
 */
 
 import java.util.*;
 
 public class Deck
 {
 	 //the deck
 	 private LinkedList m_theDeckOfCards;
 	 
 	 // suits
 	 private Card m_spades;
 	 private Card m_hearts;
 	 private Card m_diamonds;
 	 private Card m_clubs;
 	 
 	 //card removed when dealing
 	 private Card removedCard = new Card();
 	 
 	 
 	 //constructor - creates an empty linked list
 	 public Deck()
 	 {
 	 	 m_theDeckOfCards = new LinkedList();
 	 }
 	 
 	 //adds all the cards to the deck
 	 public void fillDeck()
 	 {
 	 	 for (int i=2; i<15; ++i)
 	 	 {
 	 	 	 m_theDeckOfCards.addLast(m_spades = new Card("spades", i));
 	 	 	 m_theDeckOfCards.addLast(m_hearts = new Card("hearts", i));
 	 	 	 m_theDeckOfCards.addLast(m_diamonds = new Card("diamonds", i));
 	 	 	 m_theDeckOfCards.addLast(m_clubs = new Card("clubs", i));
 	 	 }
 	 	 System.out.println(m_theDeckOfCards);
 	 }
 	 
 	 /*picks a random card from the deck to deal
 	 **by removing a random card from within the deck
 	 */
 	 public Card deal()
 	 {
 	 	 Random generate = new Random();
 	 	 removedCard = m_theDeckOfCards.remove(generate.nextInt(m_theDeckOfCards.size()));
 	 	 System.out.println(removedCard);
 	 	 return removedCard;
 	 	 
 	 }
 	 
 	 // THIS METHOD IS THE ISSUE ^
 	 
 	 public static void main(String[] args)
 	 {
 	 	 Deck test = new Deck();
 	 	 test.fillDeck();
 	 	 test.deal();
 	 }
 }



I get an incompatible types error in my deal() method. i dont know what to do to fix this because it returns a card and the removedCard variable is a Card type
Was This Post Helpful? 0

#5 Guest_Bryce Miller*


Reputation:

Re: incompatible types

Posted 08 March 2010 - 02:06 PM

/*
 * War Simulation
 * Deck Class
 * contains 52 cards 13 of each suit
 *
 * 3/6/10							
 * 
 * @author D. Bryce Miller
 */
 
 import java.util.*;
 
 public class Deck
 {
 	 //the deck
 	 private LinkedList m_theDeckOfCards;
 	 
 	 // suits
 	 private Card m_spades;
 	 private Card m_hearts;
 	 private Card m_diamonds;
 	 private Card m_clubs;
 	 
 	 //card removed when dealing
 	 private Card removedCard = new Card();
 	 
 	 
 	 //constructor - creates an empty linked list
 	 public Deck()
 	 {
 	 	 m_theDeckOfCards = new LinkedList();
 	 }
 	 
 	 //adds all the cards to the deck
 	 public void fillDeck()
 	 {
 	 	 for (int i=2; i<15; ++i)
 	 	 {
 	 	 	 m_theDeckOfCards.addLast(m_spades = new Card("spades", i));
 	 	 	 m_theDeckOfCards.addLast(m_hearts = new Card("hearts", i));
 	 	 	 m_theDeckOfCards.addLast(m_diamonds = new Card("diamonds", i));
 	 	 	 m_theDeckOfCards.addLast(m_clubs = new Card("clubs", i));
 	 	 }
 	 	 System.out.println(m_theDeckOfCards);
 	 }
 	 
 	 /*picks a random card from the deck to deal
 	 **by removing a random card from within the deck
 	 */
 	 public Card deal()
 	 {
 	 	 Random generate = new Random();
 	 	 removedCard = (Card)m_theDeckOfCards.remove(generate.nextInt(m_theDeckOfCards.size()));
 	 	 System.out.println(removedCard);
 	 	 return removedCard;
 	 	 
 	 }
 	 
 	 // THIS METHOD IS THE ISSUE ^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^
 	 //issue resolved i just had to cast the m_theDeckOfCards with (Card)   ^
 	 
 	 public static void main(String[] args)
 	 {
 	 	 Deck test = new Deck();
 	 	 test.fillDeck();
 	 	 test.deal();
 	 }
 }

Was This Post Helpful? 0

#6 Guest_Bryce Miller*


Reputation:

Re: incompatible types

Posted 08 March 2010 - 02:19 PM

/*
 * War Simulation
 * Deck Class
 * contains 52 cards 13 of each suit
 *
 * 3/6/10										
 * 
 * @author D. Bryce Miller
 */
 
 import java.util.*;
 
 public class Deck
 {
 	 //the deck
 	 private static LinkedList m_theDeckOfCards;
 	 
 	 // suits
 	 private Card m_spades;
 	 private Card m_hearts;
 	 private Card m_diamonds;
 	 private Card m_clubs;
 	 
 	 //card removed when dealing
 	 private Card removedCard = new Card();
 	 
 	 
 	 //constructor - creates an empty linked list
 	 public Deck()
 	 {
 	 	 m_theDeckOfCards = new LinkedList();
 	 }
 	 
 	 //adds all the cards to the deck
 	 public void fillDeck()
 	 {
 	 	 for (int i=2; i<15; ++i)
 	 	 {
 	 	 	 m_theDeckOfCards.addLast(m_spades = new Card("spades", i));
 	 	 	 m_theDeckOfCards.addLast(m_hearts = new Card("hearts", i));
 	 	 	 m_theDeckOfCards.addLast(m_diamonds = new Card("diamonds", i));
 	 	 	 m_theDeckOfCards.addLast(m_clubs = new Card("clubs", i));
 	 	 }
 	 	 System.out.println(m_theDeckOfCards);
 	 }
 	 
 	 /*picks a random card from the deck to deal
 	 **by removing a random card from within the deck
 	 */
 	 public Card deal()
 	 {
 	 	 Random generate = new Random();
 	 	 removedCard = (Card)m_theDeckOfCards.remove(generate.nextInt(m_theDeckOfCards.size()));
 	 	 System.out.println(removedCard);
 	 	 return removedCard;
 	 	 
 	 }
 	 
 	 /*
 	 public static void main(String[] args)
 	 {
 	 	 Deck test = new Deck();
 	 	 test.fillDeck();
 	 	 while (m_theDeckOfCards.size() > 0)
 	 	 {
 	 	 test.deal();
 	 	 }
 	 }
 	 */
 }
 


Note: Deck.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

how do i go about doing this check so i dont get this warning?
thanks a lot
Was This Post Helpful? 0

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: incompatible types

Posted 08 March 2010 - 02:32 PM

you need to specify the type of object you are storing in your LinkedList
Was This Post Helpful? 0
  • +
  • -

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: incompatible types

Posted 08 March 2010 - 03:11 PM

mostyfriedman is correct.
you specify it that way: (in your case a LinkedList):
//LinkedList of String Onbjects
LinkedList<String> strList = new LinkedList<String>();
//LinkedList of Integer:
LinkedList<Integer> intList = new LinkedList<Integer>();
//in your case, it holds Card Objects:
LinkedList<Card> cardList = new LinkedList<Card>();


*Typo edited

This post has been edited by japanir: 08 March 2010 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_Bryce Miller*


Reputation:

Re: incompatible types

Posted 08 March 2010 - 03:14 PM

thank you very much! got it!
Was This Post Helpful? 0

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,490
  • Joined: 27-December 08

Re: incompatible types

Posted 08 March 2010 - 03:18 PM

The term for what japanir is demonstrating is called Generics, which have been a part of the Java platform since Java 1.5. In a nutshell, Generics allow you to specify type for reference datatypes to work with, which is very important when dealing with Collections, as well as other tools in the JDK. So before Java 5, if you wanted to store a specific datatype in a Collection, you would subclass that Collection or write a class to wrap around it enforcing this type. Because Generics solves this problem (ie., ArrayList<Integer> to store only Integers in your ArrayList), referring to a Collection without giving it a Generic type has been deprecated, which is why you are getting the warning.
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2695
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: incompatible types

Posted 08 March 2010 - 03:28 PM

4 Topics merged.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1