Skip to content
Open

A2 #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .classpath

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ $RECYCLE.BIN/
*.msp

# Windows shortcuts
*.lnk
*.lnk

.project
17 changes: 0 additions & 17 deletions .project

This file was deleted.

19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $ git remote add upstream https://github.com/mvhs-apcs/Elevens.git

Then create a branch that tracks with the original repo's activity 1 branch:
```
$ git fetch upstream a1
$ git checkout -b a1 upstream/a1
```

Expand All @@ -31,9 +32,21 @@ $ git push origin a#

## Switching to the next activity

The activities have not all been pushed at the start of the lab because each activity builds on the next and sometimes has working versions of the problems from the previous activities.
### Activities 1-5

To move to the next activity, show your completed solution to your instructor. He will then push a branch for you to use for the next activity. The branch name will be formatted like this: `<your github username>-a#`. For example, Abe Lincoln would move to activity 4 with this command:
Change the # to the number of the activity you want to grab.

```
$ git fetch upstream a#
$ git checkout -b a# upstream/a#
```

### Later Activities

The remaining activities have not all been pushed because each activity builds on the next. Sometimes activities have working versions of the problems from the previous activities.

To move to the next activity, show your completed solution to your instructor. He will then push a branch for you to use for the next activity. The branch name will be formatted like this: `<your github username>-a#`. For example, Abe Lincoln would move to activity 6 with this command:
```
$ git checkout -b a4 upstream/four_score-a4
$ git fetch upstream four_score-a6
$ git checkout -b a6 upstream/four_score-a6
```
24 changes: 24 additions & 0 deletions answers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
1. Explain in your own words the relationship between a deck and a card.
Cards make up the components of the deck. The deck is an arraylist of cards. In a single deck, there will be 52 cards. A card can be by itself, but for a deck to exist, there must be multiple cards.

2. Consider the deck initialized with the statements below. How many cards does the deck contain?
String[] ranks = {"jack", "queen", "king"};
String[] suits = {"blue", "red"};
int[] pointValues = {11, 12, 13};
Deck d = new Deck(ranks, suits, pointValues);

From the looks of it, the programmer was planning to create three separate cards. However, the fact there are only two suits, instead of three to correlate with the number of ranks and point values, would cause an index out of bounds error. Right now, there would be no cards in the deck because the code would not be able to compile. In order for the code to run, the programmer would need to add another cell to the suit array for the command line to say there are three separate cards.

3. The game of Twenty-One is played with a deck of 52 cards. Ranks run from ac (highest) down to 2 (lowest). Suits are spades, hearts, diamonds, and clubs as in many other games. A face card has point value 10; an ace has point value 11; point values for 2, …, 10 are 2, …, 10, respectively. Specify the
contents of the ranks, suits, and pointValues arrays so that the statement:
Deck d = new Deck(ranks, suits, pointValues);
initializes a deck for a Twenty-One game.

String[] ranks = {“2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “Jack”, “Queen”, “King”, “Ace”};
String[] suits = {“Spades”, “Diamonds”, “Clubs”, “Hearts”};
int [] pointValues = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};

4. Does the order of elements of the ranks, suits, and pointValues arrays matter?
Yes! The order of elements of the ranks, suits, and pointValues arrays do matter! The cells correlate with each other. All the information in cell 0 for ranks, suits, and pointValues would make one specific card. The programmer needs to make a pattern with the arrays to create the cards that he or she wants.

It might not matter as much when the user is first initializing the information. However, the cells for sure need to correlate when we are testing our code.
92 changes: 92 additions & 0 deletions src/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Card.java
*
* <code>Card</code> represents a playing card.
*/
public class Card {

/**
* String value that holds the suit of the card
*/
private String suit;

/**
* String value that holds the rank of the card
*/
private String rank;

/**
* int value that holds the point value.
*/
private int pointValue;


/**
* Creates a new <code>Card</code> instance.
*
* @param cardRank a <code>String</code> value
* containing the rank of the card
* @param cardSuit a <code>String</code> value
* containing the suit of the card
* @param cardPointValue an <code>int</code> value
* containing the point value of the card
*/
public Card(String cardRank, String cardSuit, int cardPointValue) {
//initializes a new Card with the given rank, suit, and point value
rank = cardRank;
suit = cardSuit;
pointValue = cardPointValue;
}


/**
* Accesses this <code>Card's</code> suit.
* @return this <code>Card's</code> suit.
*/
public String suit() {
return suit;
}

/**
* Accesses this <code>Card's</code> rank.
* @return this <code>Card's</code> rank.
*/
public String rank() {
return rank;
}

/**
* Accesses this <code>Card's</code> point value.
* @return this <code>Card's</code> point value.
*/
public int pointValue() {
return pointValue;
}

/** Compare this card with the argument.
* @param otherCard the other card to compare to this
* @return true if the rank, suit, and point value of this card
* are equal to those of the argument;
* false otherwise.
*/
public boolean matches(Card otherCard) {
return otherCard.suit().equals(this.suit())
&& otherCard.rank().equals(this.rank())
&& otherCard.pointValue() == this.pointValue();
}

/**
* Converts the rank, suit, and point value into a string in the format
* "[Rank] of [Suit] (point value = [PointValue])".
* This provides a useful way of printing the contents
* of a <code>Deck</code> in an easily readable format or performing
* other similar functions.
*
* @return a <code>String</code> containing the rank, suit,
* and point value of the card.
*/
@Override
public String toString() {
return rank + " of " + suit + " (point value = " + pointValue + ")";
}
}
130 changes: 130 additions & 0 deletions src/Deck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import java.util.List;
import java.util.ArrayList;

/**
* The Deck class represents a shuffled deck of cards.
* It provides several operations including
* initialize, shuffle, deal, and check if empty.
*/
public class Deck {

/**
* cards contains all the cards in the deck.
*/
private List<Card> cards;

/**
* size is the number of not-yet-dealt cards.
* Cards are dealt from the top (highest index) down.
* The next card to be dealt is at size - 1.
*/
private int size;


/**
* Creates a new <code>Deck</code> instance.<BR>
* It pairs each element of ranks with each element of suits,
* and produces one of the corresponding card.
* @param ranks is an array containing all of the card ranks.
* @param suits is an array containing all of the card suits.
* @param values is an array containing all of the card point values.
*/
public Deck(String[] ranks, String[] suits, int[] values) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
this.cards= new ArrayList<Card>();
for(int i=0;i<ranks.length; i++){
String rank=ranks[i];
int value=values[i];
String suit=suits[i];
Card added =new Card(rank, suit, value);
cards.add(added);
}
this.size=this.cards.size();
shuffle();

}


/**
* Determines if this deck is empty (no undealt cards).
* @return true if this deck is empty, false otherwise.
*/
public boolean isEmpty() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
if (size==0){
return true;
}
else{
return false;
}
}

/**
* Accesses the number of undealt cards in this deck.
* @return the number of undealt cards in this deck.
*/
public int size() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
return this.cards.size();
}

/**
* Randomly permute the given collection of cards
* and reset the size to represent the entire deck.
*/
public void shuffle() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
}

/**
* Deals a card from this deck.
* @return the card just dealt, or null if all the cards have been
* previously dealt.
*/
public Card deal() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */

if(size==0){
return null;
}
else{
size--;
return cards.get(size);
}
}

/**
* Generates and returns a string representation of this deck.
* @return a string representation of this deck.
*/
@Override
public String toString() {
String rtn = "size = " + size + "\nUndealt cards: \n";

for (int k = size - 1; k >= 0; k--) {
rtn = rtn + cards.get(k);
if (k != 0) {
rtn = rtn + ", ";
}
if ((size - k) % 2 == 0) {
// Insert carriage returns so entire deck is visible on console.
rtn = rtn + "\n";
}
}

rtn = rtn + "\nDealt cards: \n";
for (int k = cards.size() - 1; k >= size; k--) {
rtn = rtn + cards.get(k);
if (k != size) {
rtn = rtn + ", ";
}
if ((k - cards.size()) % 2 == 0) {
// Insert carriage returns so entire deck is visible on console.
rtn = rtn + "\n";
}
}

rtn = rtn + "\n";
return rtn;
}
}
37 changes: 37 additions & 0 deletions src/DeckTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This is a class that tests the Deck class.
*/
public class DeckTester {

/**
* The main method in this class checks the Deck operations for consistency.
* @param args is not used.
*/
public static void main(String[] args) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
String[] rank1 = {"Queen", "Ace", "2"};
String[] suit1 = {"Clubs", "Hearts", "Spades"};
int[] value1 = {12, 1, 2};
Deck deck1 = new Deck(rank1, suit1, value1);
System.out.println("Deck1 size should be 3: " + deck1.size());
System.out.println("Deck1 should not be empty: " + !deck1.isEmpty());
System.out.println("Deck1 dealt card is: " + deck1.deal());

String[] rank2 = {"5", "6", "7"};
String[] suit2 = {"Clubs", "Spades", "Diamonds"};
int[] value2 = {5, 6, 7};
Deck deck2 = new Deck(rank2, suit2, value2);
System.out.println("Deck2 size should be 3: " + deck2.size());
System.out.println("Deck2 should not be empty: " + !deck2.isEmpty());
System.out.println("Deck2 dealt card is: " + deck2.deal());

String[] rank3 = {"1", "Jack", "Queen"};
String[] suit3 = {"Diamonds", "Spades", "Clubs"};
int[] value3 = {1, 11, 12};
Deck deck3 = new Deck(rank3, suit3, value3);
System.out.println("Deck3 size should be 3: " + deck3.size());
System.out.println("Deck3 should not be empty: " + !deck3.isEmpty());
System.out.println("Deck3 dealt card is: " + deck3.deal());

}
}