简体   繁体   中英

trouble with using a class constructor to initialize an object within another class

I posted a question earlier pertaining to the same problem but my question is completely different this time so please hear me out before you mark this as a duplicate or downvote it.

So for my assignment I was supposed to create a class called "Card" that would represent a standard playing card with a suit and face represented by numbers(1-4 for suit, 1-13 for face), and also constructors to initialize the cards, and mutator and accessor functions to change a card object and output its string representation(for example, King of Hearts). Then I had to create another class called DeckOfCards that represents a deck of 52 cards, storing 52 Card objects. This class has a constructor to the initialize a deck with the standard 52 cards, and member functions to shuffle the deck, deal a card, and print all the cards in the deck. My problem is trying to get the constructor for this DeckOfCards class to work. Here's the code for the entire class and my particular problem is with the constructor method:

public class DeckOfCards
{
/*Class that stores 52 objects of the Cards class.  Include methods to shuffle the deck, 
 * deal a card, and report the number of cards left in the deck, and print all the cards in
   the deck.*/
 private Card[] deck = new Card[52];
 private int count = 52, j = 0;

 public DeckOfCards()/*Constructor initializes the deck with 52 cards*/
 {
     int i = 0;

     for (int suit = 0;suit < 4;suit++)
     {
         for (int face = 0;face < 13;face++)
         {
             deck[i] = Card(suit, face);
             i++;
         }
     }
 }

 public String toStringDeck()//Prints all the cards in the deck
 {
     String deckPrint = "";
     for (int i = 0; i < 52;i++)
     {
         deckPrint += deck[i].toString() + "\n";
     }

     return deckPrint;
 }

 public void shuffle()//Shuffles the deck
 {
     Random generator = new Random();
     int rand1, rand2;
     Card temp;

     for (int i = 0;i < 100;i++)
     {
         rand1 = generator.nextInt(52);
         rand2 = generator.nextInt(52);
         temp = deck[rand1];
         deck[rand1] = deck[rand2];
         deck[rand2] = temp;
     }
 }

 public void deal()/*Deals a card from the deck and prints it as its dealt. Reports the number
 of cards remaining in the deck.*/
 {
     String deal;

     if (j < 52)
     {
         deal = deck[j].toString();
         j++;
         System.out.println(deal);
         count--;
         System.out.println("There are " + count + " cards remaining in the deck.");
     }
     else
     {
         System.out.println("There are no cards remaining in the deck.");
     }
 }

}

When I try to compile this, I get the error: "Cannot find symbol - method Card(int,int)."

I can't understand why, in the constructor, I'm not able to create a Card object and initialize it here. Please let me know if I need to give more details. Thanks.

You forgot the new keyword. Change

deck[i] = Card(suit, face);

to

deck[i] = new Card(suit, face);

& make sure that you have defined a 2-args constructor in your Card class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM