Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. In this part, we're going to create cards and decks of cards.
  2. public class Card in package oving3 (10 pt.)
  3. Create a class Card
  4.  
  5. String field suit in public class Card in package oving3
  6. 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.
  7.  
  8. int field face in public class Card in package oving3
  9. 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.
  10.  
  11. public String method toString in public class Card in package oving3
  12. The toString method returns the suit and face values as one String with no space in between.
  13.  
  14. Example: The Ace of Spades should return "S1" as the value of the toString method.
  15. public class CardDeck in package oving3 (20 pt.)
  16. Create the class CardDeck. The card deck will obviously hold some cards later on.
  17.  
  18. ArrayList<Card> field cards in public class CardDeck in package oving3
  19. A CardDeck is effectively a list of Cards. Create such an list field!
  20.  
  21. public void method init in public class CardDeck in package oving3
  22. 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").
  23.  
  24. public Card method getCard in public class CardDeck in package oving3
  25. Create a method that takes an int argument and returns the card in that position in the Card list.
  26.  
  27. Part 4: Drawing the deck of cards
  28. 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.
  29. For the final 15 points, demonstrate this program to your student assistant.
  30.  
  31.  
  32.  
  33.  
  34. package oving3;
  35.  
  36. /*
  37.  * @startuml
  38.  * class Card {
  39.  *  String suit
  40.  *  int face
  41.  * }
  42.  * @enduml
  43.  */
  44. public class Card {
  45.     String suit;
  46.     int face;
  47.    
  48.     public Card (String s, int f) {
  49.         suit = s;
  50.         face = f;
  51.     }
  52.     public String toString(){
  53.         return suit+face;
  54.     }
  55.    
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  63.  * class CardDeck {
  64.  *  +Card getCard(int i)
  65.  * }
  66.  * CardDeck --> "*" Card : cards
  67.  * @enduml
  68.  */
  69.  
  70. public class CardDeck extends GraphicsProgram {
  71.     ArrayList<Card> cardDeck = new ArrayList<Card>();
  72.     Card kort = new Card();
  73.    
  74.     public void init() {
  75.         for(int i = 0;i<4;i++) {
  76.             for(int j=1;j<=13;j++) {
  77.                 switch(i) {
  78.                     case 0: {
  79.                         kort.suit= "S";
  80.                         kort.face= j;
  81.                         cardDeck.add(kort);
  82.                         break;
  83.                     }
  84.                     case 1: {
  85.                         kort.suit= "H";
  86.                         kort.face= j;
  87.                         cardDeck.add(kort);
  88.                         break;
  89.                     }
  90.                     case 2: {
  91.                         kort.suit= "D";
  92.                         kort.face= j;
  93.                         cardDeck.add(kort);
  94.                         break;
  95.                     }
  96.                     case 3: {
  97.                         kort.suit= "C";
  98.                         kort.face= j;
  99.                         cardDeck.add(kort);
  100.                         break;
  101.                     }
  102.                     default: {
  103.                         break;
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.     }
  109.    
  110.     public Card getCard() {
  111.        
  112.     }
  113.  
  114.     GImage createGImage(String suit, int value) {
  115.         String name="";
  116.         switch(suit.charAt(0)){
  117.         case 'H':
  118.             name+="hearts";
  119.             break;
  120.         case 'D':
  121.             name+="diamonds";
  122.             break;
  123.         case 'C':
  124.             name+="clubs";
  125.             break;
  126.         case 'S':
  127.             name+="spades";
  128.             break;
  129.         }
  130.         name+="-";
  131.         switch(value) {
  132.         case 1:
  133.             name+="a";
  134.             break;
  135.         case 11:
  136.             name+="j";
  137.             break;
  138.         case 12:
  139.             name+="q";
  140.             break;
  141.         case 13:
  142.             name+="k";
  143.             break;
  144.         default:
  145.             name+="" + value;
  146.         }
  147.        
  148.         name+="-150.png";
  149.         return new GImage("oving3/img/" + name);
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement