Hello! I'm working off of a problem in my "How To Think Like A Computer Scientist" textbook. The exercise in the book is having me work with a deck of cards.
I receive two errors in my current code. The first is that every method after Class Card {...} says "Exporting non-public type through public API". Is this something to worry about?
The second error is in the method buildDeck. I get "non-static variable this cannot be referenced from a static content". I highlighted the line with this error in bold.
Any advice?
package handscore;
/**
*
* @author Josh
*/
public class Main {
class Card {
int suit, rank;
public Card () {
this.suit = 0;
this.rank = 0;
}
public Card (int suit, int rank) {
this.suit = suit;
this.rank = rank;
}
}
// prints all cards
public static void printCard (Card c) {
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
String[] ranks = {"empty", "Ace", "2", "3", "4" ,"5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
}// compares two cards and determines whether they are equal
public static boolean sameCard (Card c1, Card c2) {
return (c1.suit == c2.suit && c1.rank == c2.rank);
}
// compares two cards and determines whether or not the first is greater
// than the second
public static int compareCard (Card c1, Card c2) {
if (c1.suit > c2.suit) return 1;
if (c1.suit < c2.suit) return -1;
if (c1.rank > c2.rank) return 1;
if (c1.rank < c2.rank) return -1;
return 0;
}
public static Card[] buildDeck () {
Card[] deck = new Card [52];
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
[B] deck[index] = new Card (suit, rank);[/B]
index++;
}
}
return deck;
}
public static void main(String[] args) {
// TODO code application logic here
}
}