1 / 17

Midterm 2 Practice

Midterm 2 Practice. CIS 113 Spring 2005. Problem 1. A card game called Twenty uses a special deck with 20 cards. The deck has two suits - Red and Black. In each suit there are cards with rank 0 through 9. The definition of Card is below.

ringo
Download Presentation

Midterm 2 Practice

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Midterm 2 Practice CIS 113 Spring 2005

  2. Problem 1 A card game called Twenty uses a special deck with 20 cards. The deck has two suits - Red and Black. In each suit there arecards with rank 0 through 9. The definition of Card is below. Write the member declarations and a constructor for the class CardDeck to create a deck of cards for Twenty. You may use “R” for “Red” and “B” for “Black”. Hint: use an array of type Card and of size 20. public class Card { public int rank; public String suit; Card(){rank = -1; suit = "-";} Card(Card card){rank = card.rank; suit = card.suit;} void show(){System.out.print(rank + suit);} }

  3. Solution public class CardDeck { final static int DECK_SIZE = 20; Card[] deck = new Card[DECK_SIZE]; CardDeck(){ for (int i = 0; i < DECK_SIZE; ++i){ deck[i] = new Card(); } for (int r = 0; r < DECK_SIZE/2; ++r){ (deck[r]).rank = r; deck[r + DECK_SIZE/2].rank = r; deck[r].suit = "B"; deck[r + DECK_SIZE/2].suit = "R"; } }}

  4. Key points in the solution • Card[] deck says deck will be an array of Cards • new Card[20]allocates memory for the array of Cards • The for loop enclosing deck[i] = new Card();creates 20 Cards and puts pointers to them into deck.Very important! Until this loop is executed deck is filled with null pointers! • The remaining loops give the Cards the proper ranks and and suits

  5. Next Problem (2) Assume that the class Card and the constructor for CardDeck are given. Write a method for the class CardDeck with the following signature: void shuffle(int times) Each ‘time’ in the shuffle is accomplished by choosingtwo cards in the 20-Card deck at random and swappingthem.

  6. Solution void shuffle(int times){ for (int i = 0; i < times; ++i){ // select the two cards to be swapped int a = (int)(Math.random() * DECK_SIZE); int b = (int)(Math.random() * DECK_SIZE); // save the value of the first card Card temp = deck[a]; // do the swap deck[a] = deck[b]; deck[b] = temp; } } Question: What happens if you don’t create a newCard to save the value of deck[a]?

  7. Next Problem (3) Write a method for CardDeck with the following signature: void show() show should display the cards, 10 to a line, with a spacebetween cards. A sample output is given below. Hint: thereis a method show() in the class Card. 3R 2B 4R 1R 9B 9R 1B 6B 7R 2R 4B 6R 8B 5B 3B 7B 8R 0B 0R 5R

  8. Solution void show(){ // print out 10 cards on the first line for (int i = 0; i < DECK_SIZE/2; ++i){ deck[i].show(); System.out.print(" "); } System.out.println(); // print out 10 cards on the second line for (int i = DECK_SIZE/2; i < DECK_SIZE; ++i){ deck[i].show(); System.out.print(" "); } System.out.println(); }

  9. Next Problem (4) • Write a class CardStuff using the methods of the classCardDeck. CardStuff should do the following: • create a deck of cards for the game of Twenty • display the deck of cards • shuffle the cards 100 times • display the shuffled deck

  10. Solution public class CardStuff { public static void main(String[] args) { CardDeck deckA = new CardDeck(); deckA.show(); System.out.println(); deckA.shuffle(100); deckA.show(); } }

  11. Next Problem (5) Write a method with the following signature: public static boolean AtLeastHalfEven(int[] intArr) This method should return true if and only if at least half of the int’s in the intArr are even. You may assume that intArr is fully populated, that is, there are no nulls in it.

  12. Solution public static boolean AtLeastHalfEven(int[] intArr){ int evenCount = 0; for (int i = 0; i < intArr.length; ++i){ if (intArr[i]%2 == 0){ evenCount++; } } if (evenCount >= intArr.length/2){ return true; } else { return false; } } }

  13. Next Problem (6) • Write a class named CheckHalfEven. This class shouldhave a main method that does the following: • creates an array of 20 random int’s in the range 0-99 • calls AtLeastHalfEven to determine if half or more of the numbers in the array are even • prints out the result

  14. Solution public static void main(String[] args) { final int MAX_NUM = 20; int[] iArr = new int[MAX_NUM]; for (int i = 0; i < MAX_NUM; ++i){ iArr[i] = (int)(Math.random()*100); } if (AtLeastHalfEven(iArr)){ System.out.println("“At least half even"); } else { System.out.println(“Less than half even"); } }

  15. Next Problem (7) • Write a method with the following signature: • static int AboveThreshold(int[] arr, int threshold) • This method should: • determine how many int’s in arr[] strictly greater than threshold • return that number • You may assume that all the elements of arr[] are int’s.

  16. Next Problem (8) • Write a class AboveN with a main method. This method should: • create a array of 100 random int’s in the range 20-50 • call AboveThreshold to determine how many of them are greater than 45 • print this result to the standard output

  17. Next Problem(9) It is a fact of geometry that there are only five regularsolids. A regular solid is one in which all angles arethe same and all faces are the same. For example, acube is a regular solid. The number of faces on the fiveregular solids are 4, 6, 8, 12 and 20. The regular (Platonic) solids make good dice because each sidehas a equal probability of coming up. Write a class PlatonicDie. The class should include two methods with signatures: Die(int sides) int roll() If a die has 4, 6, 8, 12 or 20 sides, roll() should returna random number from 1 to the number of sides. Otherwise, roll() should return –1. (Hint: modify theDie class in the homework.)

More Related