practice final cis 113
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Practice Final CIS 113
Spring 2005
Problem 1 – threeOfAKindIn 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 returnstrue if and only if there are at least three cards of the same rankin the hand.
Solution to hasThreeOfAKindstatic 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;}
Problem 2 - DivisorsWrite 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.
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;}
Problem 3 - mostVowelsWrite 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.
Solution to mostVowelsstatic 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;}
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.
Solution to even length stringsstatic 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;}
Problem 5 – boolean matrixA 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 an int 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;
}
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.
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.
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.