How to Make a Deck of Playing Cards in NetBeans Using Arrays
Developers created the Java programming language to represent an object-oriented programming paradigm from the ground up. This means that Java inherently contains the structure to allow programmers to create data to represent anything as objects in code. Things such as cards, or a deck of cards, can be easily created by opening a new project in the NetBeans IDE and creating a Deck of Cards class.
Instructions
-
-
1
Start a new Project in NetBeans. Click on "File," then "New Project." Select "Java," and "Java Application." Click "Next," give the project a name such as "CardDeck," and click "Finish."
-
2
In the new project, create deck class:
public class CardDeck{
}
-
-
3
create a Card class inside the CardDeck class to represent individual cards:
public class CardDeck{
public static class Card{
String suit;
String value;
}}
-
4
Create the deck of cards data structure from a multidimensional array of cards:
public static Card[][] deck = new Card[4][13];
-
5
Create the constructor to fill the deck with the proper cards:
public CardDeck(){
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
String[] vals = {"A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};for (int i = 0; i < suits.length; i++){
for (int j = 0; j < vals.length; j++){
deck[i][j] = new Card();
deck[i][j].suit = suits[i];
deck[i][j].value = vals[j];
}
}
}
-
1