Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In this part, we're going to create cards and decks of cards.
- public class Card in package oving3 (10 pt.)
- Create a class Card
- String field suit in public class Card in package oving3
- A card has a suit. The suit can either be Clubs, Hearts, Spades or Diamonds. We let each be denoted by a capital letter; "C", "H", "S" or "D", respectively.
- int field face in public class Card in package oving3
- The face is the value of the card. For our purposes, let Ace have the value 1, and then let the rest of the cards have their conventional values.
- public String method toString in public class Card in package oving3
- The toString method returns the suit and face values as one String with no space in between.
- Example: The Ace of Spades should return "S1" as the value of the toString method.
- public class CardDeck in package oving3 (20 pt.)
- Create the class CardDeck. The card deck will obviously hold some cards later on.
- ArrayList<Card> field cards in public class CardDeck in package oving3
- A CardDeck is effectively a list of Cards. Create such an list field!
- public void method init in public class CardDeck in package oving3
- The init() method fills the deck with the 52 cards that are in a standard (Norwegian) deck. Fill it in the order S,H,D,C and in increasing face value, i.e. the first card is the Ace of Spades ("S1"), the second 2 of Spades ("S2"), and the last is King of Clubs ("C13").
- public Card method getCard in public class CardDeck in package oving3
- Create a method that takes an int argument and returns the card in that position in the Card list.
- Part 4: Drawing the deck of cards
- Create a new program which draws the card deck in some way of your choosing. The CardDeck class already has a method called createGImage for returning a returning a GImage for a given suit and face (String and int, respectively). This simply means that if you call createGImage("H", 3), you will get a GImage object which will be an image of the card H3, namely the three of hearts. Add one such GImage for each card in the deck , and the whole deck will be shown in the application window. Note you also must set the location of each card appropriately, so they won't cover each other.
- For the final 15 points, demonstrate this program to your student assistant.
- package oving3;
- /*
- * @startuml
- * class Card {
- * String suit
- * int face
- * }
- * @enduml
- */
- public class Card {
- String suit;
- int face;
- public Card (String s, int f) {
- suit = s;
- face = f;
- }
- public String toString(){
- return suit+face;
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- * class CardDeck {
- * +Card getCard(int i)
- * }
- * CardDeck --> "*" Card : cards
- * @enduml
- */
- public class CardDeck extends GraphicsProgram {
- ArrayList<Card> cardDeck = new ArrayList<Card>();
- Card kort = new Card();
- public void init() {
- for(int i = 0;i<4;i++) {
- for(int j=1;j<=13;j++) {
- switch(i) {
- case 0: {
- kort.suit= "S";
- kort.face= j;
- cardDeck.add(kort);
- break;
- }
- case 1: {
- kort.suit= "H";
- kort.face= j;
- cardDeck.add(kort);
- break;
- }
- case 2: {
- kort.suit= "D";
- kort.face= j;
- cardDeck.add(kort);
- break;
- }
- case 3: {
- kort.suit= "C";
- kort.face= j;
- cardDeck.add(kort);
- break;
- }
- default: {
- break;
- }
- }
- }
- }
- }
- public Card getCard() {
- }
- GImage createGImage(String suit, int value) {
- String name="";
- switch(suit.charAt(0)){
- case 'H':
- name+="hearts";
- break;
- case 'D':
- name+="diamonds";
- break;
- case 'C':
- name+="clubs";
- break;
- case 'S':
- name+="spades";
- break;
- }
- name+="-";
- switch(value) {
- case 1:
- name+="a";
- break;
- case 11:
- name+="j";
- break;
- case 12:
- name+="q";
- break;
- case 13:
- name+="k";
- break;
- default:
- name+="" + value;
- }
- name+="-150.png";
- return new GImage("oving3/img/" + name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement