This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CompSci 6 Test 1 Fall 2009 PROBLEM 1 : ( Short and To the Point (14 points) ) A. For each of the following objectoriented programming terms, summarize the distinction between the two terms. Your answer should be brief . 1. class vs. object 2. the constructor vs. any other class method B. Write a method majority that takes three boolean inputs and returns the most com mon value. For example, majority(false, false, true) should return false , while majority(false, true, true) returns true. C. The following method, floorRoot , was designed to compute the largest integer whose square is no greater than N, where N is assumed to be a positive number. (If N is 5, then the procedure should report the value 2.) Find and correct the error. /* returns the largest integer whose square is no greater than n */ public int floorRoot(int n) { int x = 0; while (x * x <= n) { x = x + 1; } return x; } D . What is the value of name after the following code executes? String name = "Richard H. Brodhead" name = name.substring(name.indexOf(" ")+1) + ", " + name.substring(0,name.indexOf(" ")); PROBLEM 2 : ( Loopdeloopdeloop (20 points) ) Consider solving the problem of finding all the maximum values in an array and moving them to the front of the array, while keeping all the other elements in the array in the same order. For example if the array was 7 8 9 3 2 9 5 1 Then after moving the maximum values (in this case two 9’s) to the front of the array, the array values would be 9 9 7 8 3 2 5, note the nonmax items are still in the same order. We will solve this problem in two parts. PART A. First, given an array numbers (assume it has been created and initialized with values), compute max , the maximum value in the array, and maxCount , the number of occur rences of max in the array. Do not modify the array for this part. int numbers; // code to initialize not shown int max; // maximum value in array numbers int maxCount = 0; // number of occurences of max in numbers // TODO: compute max and maxCount for the array numbers PART B. Consider the following code that assumes you have computed max and maxCount correctly in part A. This code puts all the max values in the front of the array, with all other elements still in the same order. // move all max’s to the front of the array numbers, // keeping nonmax elements in the same order // LOOP 1 int index = numbers.length  1; for (int k=numbers.length1; k>= 0; k) { if (numbers[k]!= max) { numbers[index] = numbers[k]; index = index 1; } } 2 // LOOP 2 for (int k=0; k<maxCount; k++) { numbers[k] = max;...
View
Full
Document
This document was uploaded on 01/17/2012.
 Fall '09

Click to edit the document details