Skip to content

Commit 50d24e7

Browse files
committed
Initial commit
0 parents  commit 50d24e7

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

.vscode/launch.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "java",
9+
"name": "Launch Current File",
10+
"request": "launch",
11+
"mainClass": "${file}"
12+
},
13+
{
14+
"type": "java",
15+
"name": "Launch Card",
16+
"request": "launch",
17+
"mainClass": "Card",
18+
"projectName": "java-oop-example_a38d6e27",
19+
"vmArgs": "-enableassertions"
20+
},
21+
{
22+
"type": "java",
23+
"name": "Launch DisplayDeck",
24+
"request": "launch",
25+
"mainClass": "DisplayDeck",
26+
"projectName": "java-oop-example_a38d6e27"
27+
}
28+
]
29+
}

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Video Link
2+
https://youtu.be/Cp5L_YFjaD4
3+
4+
## Getting Started
5+
6+
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
7+
8+
## Folder Structure
9+
10+
The workspace contains two folders by default, where:
11+
12+
- `src`: the folder to maintain sources
13+
- `lib`: the folder to maintain dependencies
14+
15+
## Dependency Management
16+
17+
The `JAVA DEPENDENCIES` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-pack/blob/master/release-notes/v0.9.0.md#work-with-jar-files-directly).

src/Card.class

2.36 KB
Binary file not shown.

src/Card.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Arrays;
2+
3+
public class Card {
4+
private final String suit;
5+
private final String rank;
6+
7+
// ranks
8+
public final static String[] cardRanks = {"Ace", "Deuce", "Three", "Four", "Five" , "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
9+
10+
// suits
11+
public final static String[] cardSuits = {"Diamonds", "Clubs", "Hearts", "Spades"};
12+
13+
public Card(String rank, String suit) {
14+
assert isValidRank(rank);
15+
assert isValidSuit(suit);
16+
this.rank = rank;
17+
this.suit = suit;
18+
}
19+
20+
public String getSuit() {
21+
return suit;
22+
}
23+
24+
public String getRank() {
25+
return rank;
26+
}
27+
28+
// we have to run the compiled java class with options '-ea' in order to enable assertions and be able to throw exceptions on invalid parame. example: 'java -ea Card'
29+
30+
public static boolean isValidRank(String rank) {
31+
return Arrays.stream(cardRanks).anyMatch(rank::equals);
32+
}
33+
34+
public static boolean isValidSuit(String suit) {
35+
return Arrays.stream(cardSuits).anyMatch(suit::equals);
36+
}
37+
38+
public static void main(String[] args) {
39+
System.out.println(isValidSuit("Diamond"));
40+
// wrong card suit
41+
Card c1 = new Card("Ace", "Diamond");
42+
System.out.println(c1);
43+
}
44+
}
45+

src/Deck.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Deck {
2+
3+
public static int numSuits = 4;
4+
public static int numRanks = 13;
5+
public static int numCards = numSuits * numRanks;
6+
7+
private Card[][] cards;
8+
9+
public Deck() {
10+
cards = new Card[numSuits][numRanks];
11+
for (int suit = 0; suit < Card.cardSuits.length; suit++) {
12+
for (int rank = 0; rank < Card.cardRanks.length; rank++) {
13+
cards[suit][rank] = new Card(Card.cardRanks[rank], Card.cardSuits[suit]);
14+
}
15+
}
16+
}
17+
18+
public Card getCard(int suit, int rank) {
19+
return cards[suit][rank];
20+
}
21+
}

src/DisplayDeck.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class DisplayDeck {
2+
public static void main(String[] args) {
3+
Deck deck = new Deck();
4+
for (int suit = 0; suit < Card.cardSuits.length; suit++) {
5+
for (int rank = 0; rank < Card.cardRanks.length; rank++) {
6+
Card card = deck.getCard(suit, rank);
7+
System.out.format("%s of %s%n",
8+
card.getRank(),
9+
card.getSuit());
10+
}
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)