I would like my dealer class in a BlackJack project to print the top two "cards" of an arrayList "theDeck". I am getting a compiler error (shown later) and can't figure out what it means. This is my code for a class "Deck" that makes a deck of cards, shuffles them, and puts them in an arrayList "theDeck":
* Compilation: javac Deck.java
* Execution: java Deck
* Author: Aidan Hill
* Status: Working
* Priority: None
*
******************************************************************************/
import java.util.ArrayList;
/******************************************************************************/
public class Deck extends Game {
int n;
//Deck deck = new Deck();
String[] deckArray = new String[n];
String[] SUITS = {
"Diamonds", "Clubs", "Spades", "Hearts"
};
String[] RANKS = {
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
};
public Deck() {
// constructor
n = SUITS.length * RANKS.length;
deckArray = new String[n];
for (int i = 0; i < RANKS.length; i++) {
for (int j = 0; j < SUITS.length; j++) {
deckArray[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
}
}
}
public String shuffleDeck() {
// shuffle the deck
int i;
for (i = 0; i < n; i++) {
int r = i + (int) ((n - i) * Math.random());
String temporary = deckArray[r];
deckArray[r] = deckArray[i];
deckArray[i] = temporary;
}
return deckArray[i - 1];
}
public ArrayList<String> theDeck() {
// save shuffled deck in an arraylist
ArrayList<String> theDeck = new ArrayList<String>();
for (int i = 0; i < n; i++) {
theDeck.add(deckArray[i]);
}
return theDeck;
}
}
This is my code for another class "Dealer" that will take the top two elements of arrayList "theDeck" and print them to the console:
/******************************************************************************
* Compilation: javac Dealer.java
* Execution: java Dealer
* Author: Aidan Hill
* Status: Under Construction
* Priority: !
*
******************************************************************************/
import java.util.ArrayList;
import java.util.ArrayList;
/******************************************************************************/
public class Dealer extends Game {
public ArrayList<String> dealSumCards() {
ArrayList<String> hand = new ArrayList<String>();
hand.add(Deck.theDeck().toString());
System.out.println("The first hand is: " + hand);
return hand;
}
}
When I attempt to compile, I get this error message:
----jGRASP exec: javac -encoding UTF-8 -g @BlackJack_source_files_1638219710914506414jgr
Dealer.java:20: error: non-static method theDeck() cannot be referenced from a static context
hand.add(Deck.theDeck().toString());
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Whats my error? Neither method is static... Am I big stupid?
I tried changing dealer to extend game instead of deck and vise versa, same problem.
(If you need any more code, let me know, and I will edit the post.)
Edit: I realize now that there are many many related questions. Sorry lol. Please be patient with me, new to coding and this website.