1 / 13

Practice Final CIS 113

Practice Final CIS 113. Spring 2005. Problem 1 – threeOfAKind. In the card game Match a hand consists of 10 cards. The ranks of the cards are the numbers 0-9. A card is represented by a single int that is the rank of the card. A hand is an array of int ’s.

Download Presentation

Practice Final CIS 113

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. Practice Final CIS 113 Spring 2005

  2. Problem 1 – threeOfAKind In the card game Match a hand consists of 10 cards. The ranks of the cards are the numbers 0-9. A card isrepresented by a single int that is the rank of the card. A hand is an array of int’s. Write a method named hasThreeOfAKind. The method takes an int array named hand as a parameter. The method returns true if and only if there are at least three cards of the same rankin the hand.

  3. Solution to hasThreeOfAKind static boolean hasThreeOfAKind(int[] hand) { // loop through each rank of card for (int rank = 0; rank < 10; ++rank) { // count the number of cards of the given rank int count = 0; for (int card = 0; card < hand.length; ++card) { if (hand[card] == rank) { count++; } } if (count >= 3) { return true; } } return false; }

  4. Problem 2 - Divisors Write a method named modCount. The method takes apositive int n as a parameter. It returns a non-negativeint that is the number of divisors of n.

  5. Solution to modCount static int[] modCount(int N) { // create an array to hold the counts int[] modArr = new int[N]; // test each possible divisor for (int i = 1; i <= N; ++i) { // increment the appropriate count modArr[N%i]++; } return modArr; }

  6. Problem 3 - mostVowels Write a method called mostVowels. The method takesan array of strings as a parameter. It returns the stringthat has the most vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’). You may assume that no two strings have the same numberof vowels.

  7. Solution to mostVowels static String mostVowels(String[] strs) { String most = strs[0]; int mostCount = vowelCount(strs[0]); for (int i = 1; i < strs.length; ++i) { if (vowelCount(strs[i]) > mostCount) { mostCount = vowelCount(strs[i]); most = strs[i]; } } return most; } static int vowelCount(String str) { int count = 0; for (int i = 0; i < str.length(); ++i) { if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') count++; } return count; }

  8. Problem 4 – Even length strings Write a method named evenLengthStrings. The method takesan array of strings named words as a parameter. It returns anarray of strings that consists of all the strings in words thatare of even length.

  9. Solution to even length strings static String[] evenLengthStrings(String[] words) { // find out how many strings are of even length int evenCount = 0; for (int i = 0; i < words.length; ++i) { if (words[i].length()%2 == 0) { evenCount++; } } // create an array and put the even length strings in it String[] evenArray = new String[evenCount]; int arrIdx = 0; for (int i = 0; i < words.length; ++i) { if (words[i].length()%2 == 0) { evenArray[arrIdx] = words[i]; arrIdx++; } } return evenArray; }

  10. Problem 5 – boolean matrix A matrix is a two dimensional array. In a square matrix,the two dimensions have the same size. You are giventhe declarations of the data members of a boolean matrixbelow. Write a constructor for this class that takes anint n as the size of the matrix and initializes the elementsof the array to false, except that the elements of the maindiagonal, in which the row and column are the same, areinitialized to true. public class BooleanMatrix { boolean[][] matrix; int size; }

  11. Problem 6- isSorted Implement a method called isSorted() that takes an array of int's as a parameter. The array may have duplicate values. The method returns true if the array is sorted in nondescending order, and returns false otherwise.

  12. Problem 7 – same truth value Write a method with the following signature: public static boolean uniform(boolean[] arr) This method should return true if all of the elements of arrare true or all of the elements of arr are false. Otherwise, uniform should return false.

  13. Problem 8 – second largest Write a method named secondLargest. The methodtakes an array of int’s with at least two elements asa parameter. You may assume that there are noduplicate values among the int’s. The method returns the next to largest element.

More Related