practice final cis 113

13
Practice Final CIS 113 Spring 2005

Upload: vincent-warner

Post on 31-Dec-2015

28 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Practice Final CIS 113

Practice Final CIS 113

Spring 2005

Page 2: Practice Final CIS 113

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.

Page 3: Practice Final CIS 113

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;}

Page 4: Practice Final CIS 113

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.

Page 5: Practice Final CIS 113

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;}

Page 6: Practice Final CIS 113

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.

Page 7: Practice Final CIS 113

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;}

Page 8: Practice Final CIS 113

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.

Page 9: Practice Final CIS 113

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;}

Page 10: Practice Final CIS 113

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;

}

Page 11: Practice Final CIS 113

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.

Page 12: Practice Final CIS 113

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.

Page 13: Practice Final CIS 113

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.