Java Compile Error with Multiple Clases

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 876 Views - Last Post: 13 January 2011 - 08:32 PM Rate Topic: -----

#1 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Java Compile Error with Multiple Clases

Posted 12 January 2011 - 12:41 PM

Hello,

This is my first java program that involves multiple classes. This is a casino war program. Everything looks good(to me), but I cannot get it to compile.

Here is the code:

//Card.java

public class Card
{
   private int face; 
	private int suit; 
	
	public static final int Ace = 1;
	public static final int Jack = 11;
	public static final int Queen = 12;
	public static final int King = 13;
	public static final int Spades = 1;
	public static final int Clubs = 2;
	public static final int Hearts = 3;
	public static final int Diamonds = 4;
			
   
   public Card( int cardFace, int cardSuit )
   {
      face = cardFace; 
		suit = cardSuit; 
   } 

   public String toString() 
   { 
		String faces[] = {"", "Ace", "Two", "Three", "Four", "Five", "Six", 
			"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
		String suits[] = {"Spades", "Clubs", "Hearts", "Diamonds"};
				
      return faces[face] + " of " + suits[suit];
   }
	public boolean beats(Card c)
	{	if(face > c.face)
			return true;
		else if (face == c.face && suit > c.suit)return true;
		else
			return false;
	}
}


// DeckOfCards.java
import java.util.Random;

public class DeckOfCards
{
   private Card[] deck; 
   private int currentCard; 
	private static final int NUMBER_OF_CARDS = 52; 
	private static final Random randomNumbers = new Random();

   public DeckOfCards()
   {
		String[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six", 
         "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
      String[] suits = { "Diamonds", "Hearts", "Clubs", "Spades" };

      deck = new Card[ NUMBER_OF_CARDS ];      
		currentCard = 0;

      for ( int count = 0; count < deck.length; count++ ) 
         deck[ count ] = 
             new Card( faces[ count % 13 ], suits[ count / 13 ] );
   } 
   public void shuffle()
   {
      currentCard = 0; 
      for ( int first = 0; first < deck.length; first++ ) 
      {
         int second =  randomNumbers.nextInt( NUMBER_OF_CARDS );
         
         Card temp = deck[ first ];        
         deck[ first ] = deck[ second ];   
         deck[ second ] = temp;            
      }   } 

     public Card dealCard()
   {
      if ( currentCard < deck.length )
         return deck[ currentCard++ ];
      else        
         return null; 
   } 
}


//DeckOfCardsTest.java


public class DeckOfCardsTest
{
	static int dealerWins, playerWins;
   // execute application
   public static void main( String[] args )
   {	dealerWins = 0;
		playerWins = 0;
      DeckOfCards myDeckOfCards = new DeckOfCards();  		    
		myDeckOfCards.shuffle(); 
		
		Card playerCard = myDeckOfCards.dealCard();
		Card dealerCard = myDeckOfCards.dealCard();
		System.out.println("\nYour Card is " + playerCard);
		System.out.println("\nDealer Card is " + dealerCard);
		if ( playerCard.beats(dealerCard) )
			System.out.println("You win!");
		else
			System.out.println("You lose!");
					
   } 
}


It appears to be a very simple fix, but I'm stuck.

thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Compile Error with Multiple Clases

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 12:47 PM

What is the problem?
Do you get anyerrors? if yes please post them.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10392
  • View blog
  • Posts: 38,458
  • Joined: 27-December 08

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 12:49 PM

Post the errors.
Was This Post Helpful? 0
  • +
  • -

#4 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 12:58 PM

I apologize.

Here is the error:

DeckOfCards.java:22: cannot find symbol
symbol : constructor Card(java.lang.String,java.lang.String)
location: class Card
new Card( faces[ count % 13 ], suits[ count / 13 ] );
^
1 error
Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 01:06 PM

You declared your card constructor to accept two int parameters:
public Card( int cardFace, int cardSuit ) 

However in DeckOfCards, you call it and pass two String Objects:
new Card( faces[ count % 13 ], suits[ count / 13 ] ); 

faces and suits are String arrays.

You will have to make a change, either change your Card Counstructor to accept String Objects, or change the way you call it in DeckOfCards and pass it int values.
Was This Post Helpful? 1
  • +
  • -

#6 visierrat  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 13
  • Joined: 03-January 11

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 01:55 PM

View PostBadgers, on 12 January 2011 - 11:41 AM, said:

Hello,

This is my first java program that involves multiple classes. This is a casino war program. Everything looks good(to me), but I cannot get it to compile.

Here is the code:

It appears to be a very simple fix, but I'm stuck.

thanks in advance!


Totally agree with japanir, remember that whenever you create an object, you must match the class constructor's order, type and number. By this is meant that you must pass in 2 parameters to your object as in your constructor (in this case), of type int both (in this case), in the same order (always).
Was This Post Helpful? 0
  • +
  • -

#7 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 02:10 PM

View Postjapanir, on 12 January 2011 - 12:06 PM, said:

You declared your card constructor to accept two int parameters:
public Card( int cardFace, int cardSuit ) 

However in DeckOfCards, you call it and pass two String Objects:
new Card( faces[ count % 13 ], suits[ count / 13 ] ); 

faces and suits are String arrays.

You will have to make a change, either change your Card Counstructor to accept String Objects, or change the way you call it in DeckOfCards and pass it int values.


Thank you. I am still confused on how to do this. Can you point me in the right direction or give me an example? Thank you so much for your help.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 02:12 PM

You simply need to change the type of the parameter:
public Card( int cardFace, int cardSuit )


Was This Post Helpful? 0
  • +
  • -

#9 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 02:51 PM

View PostDogstopper, on 12 January 2011 - 01:12 PM, said:

You simply need to change the type of the parameter:
public Card( int cardFace, int cardSuit )



I changed them to strings, but it still isn't working..
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 02:53 PM

Did you change the instance variables in the Card class to String?
Was This Post Helpful? 0
  • +
  • -

#11 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 02:58 PM

How would I change the way I call it in DeckOfCards and pass it int values. I think that would be the best way to do so for this program.
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 03:20 PM

You could change the targets to be integers that represent the indexes of the array...
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10392
  • View blog
  • Posts: 38,458
  • Joined: 27-December 08

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 03:48 PM

So you're using code that isn't yours. Chances are if it isn't your code, then you're not supposed to modify it. You should probably brush up on the basics before proceeding any further. The Java tutorials section is a great place to start out.
Was This Post Helpful? 1
  • +
  • -

#14 Badgers  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 29
  • Joined: 16-December 10

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 04:15 PM

View Postmacosxnerd101, on 12 January 2011 - 02:48 PM, said:

So you're using code that isn't yours. Chances are if it isn't your code, then you're not supposed to modify it. You should probably brush up on the basics before proceeding any further. The Java tutorials section is a great place to start out.


We were given the bare code from our professor. The original code just displayed all 52 cards in the deck. Every other modification has been done by me. It is ny assignment to go from there to create a casino war game. Sorry for any confusion. I assure you I didn't download it from online and am trying to pass it on as my own. I am just stuck.
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10392
  • View blog
  • Posts: 38,458
  • Joined: 27-December 08

Re: Java Compile Error with Multiple Clases

Posted 12 January 2011 - 04:17 PM

I understand that. But if the code is prewritten, you really shouldn't need to nor should you modify it. You can quite feasibly complete your assignment without modifying your instructor's code.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2