DOC PREVIEW
Duke CPS 006 - Test 1

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Compsci 6 Test 1 Spring 2006NAME (print):Honor Acknowledgement (signature):DO NOT SPEND MORE THAN 10 MINUTES ON ANY QUESTION! If you don’t see thesolution to a problem right away, move on to another problem and come back to it later.Before starting, make sure your test contains 18 pages. The last two pages are blank andcan be used as scratch paper, but must be turned in.Pages 15 and 16 have the Java classes String, ArrayList, and ScannerDo not discuss this test with anyone until the test is handed back.None of the Java programs or program segments should have syntax errors unless stated. Ifyou think there is a syntax error, then ask. Import statements may not always be shown.value gradeProblem 1 12 pts.Problem 2 12 pts.Problem 3 10 pts.Problem 4 12 pts.Problem 5 16 pts.Problem 6 32 pts.TOTAL: 94 pts.1PROBLEM 1 : (What if?: (12 pts))Assume that value and other are declared as type integer, and consider the followingcode segment.if ((value > 7) && (other < 4))System.out.print("ok ");For each code segment below, state whether or not it is equivalent to the above code segment.If it is not equivalent, then give input values for value and other for which the code segmentsgenerate different output.A.if (value > 7)System.out.print("ok ");if (other < 4)System.out.print("ok ");B.if ( ! ((value <= 7) && (other >= 4)))System.out.print("ok ");C.if (! ((value <= 7) || (other >= 4)))System.out.print("ok ");D.if (value > 7){if (other < 4){System.out.print("ok ");}}2PROBLEM 2 : (Output: (12 pts))Consider the following Mystery method. Note that there are preconditions on the argumentspassed to the method. First, number must be greater than 0 and contain no 0 digits, valuemust be greater than 0 and less than 10.// precondition: number contains no 0’s, number > 0// 0 < value < 10public int Mystery(int number, int value){int count = 0;int dig = number % 10;while (dig > 0){if (dig == value){count++;}number = number/10;dig = number % 10;}return count;}A. What type is the return value for the method Mystery?B. How many local variables does Mystery have and what are their names?C. What is the return value for the call Mystery(376646, 6)?D. What is the return value for the call Mystery(376646, 5)?E. Describe in words what the method Mystery does.3PROBLEM 3 : (Need a Place to Stay?: (10 pts))Consider the following two classes.public class House {private String myAddress;private int myNumberOfBedrooms;public House(String address, int numberOfBedrooms) {// code not shown}public int getNumberOfBedrooms() {// code not shown}public String getAddress() {// code not shown}public void setNumberOfBedrooms(int number) {// code not shown}}public class BeachHouse extends House{private int myDistanceToBeach; // distance from house to beachpublic BeachHouse(String addr, int numBdrms, int distance) {// code not shown}public String getAddress() {// code not shown}// increase number of bedrooms by 1public void additionalBedRoom(){setNumberOfBedrooms(getNumberOfBedrooms()+1);}}4A : Which of the two classes is the superclass?B : Give the name of a method that is overridden.C : In the code for the BeachHouse method additionalBedRoom, suppose the code is replacedby:myNumberOfBedrooms++;Explain why there is now a compile error.D : In order for the code change made in part C to compile, what is the best other changewithin the code to make? Explain.5PROBLEM 4 : (The equalizer: (12 pts))Write the method equalize which has one parameter for an ArrayList of Strings. Eachentry in the ArrayList is either ”duke” or ”unc” (there are no other words in the ArrayList).The method equalize determines if there are more occurences of one type than the other.If there are, it adds additional strings to the end of the ArrayList so that both ”duke” and”unc” occur an equal number of times. Then it returns the ArrayList.For example, suppose schools has the entries shown on the left below. After the statementschools = equalize(schools); , then schools has the entries shown on the right below (2”unc”’s were added at the end of the ArrayList, now there are an equal number of occurencesof both ”duke” and ”unc”).dukedukedukeuncdukedukedukeuncuncuncComplete the method equalize.public ArrayList<String> equalize(ArrayList<String> schools){}6PROBLEM 5 : ( No More Spam (16 pts))PART A (8 pts):Write the method replaceAt whose header is given below. This method has one Stringparameter. It returns this String with all occurences of ”@” replaced with ” AT ” (note thetwo blanks, one before A and one after T).For example replaceAt("[email protected]") returns "rodger AT cs.duke.edu",replaceAt("@a@b@c") returns "ATaATbATc",andreplaceAt("go duke go") re-turns "go duke go" (unchanged since there are no @’s.).Complete the method replaceAt below.// returns a String equivalent to phrase with all occurrences of// "@" replaced with " AT "public String replaceAt(String phrase){}7PART B (8 pts):Write the method replaceAllAts whose header is given below. This method has one Scannerparameter named input that is already bound to a file for reading. It returns an ArrayListwhere each entry is a line from the file with all occurences of ”@” replaced with ” AT ” (notethe two blanks, one before A and one after T).For example, if the file bound to the Scanner input is shown on the left below, then afterthe statement data = replaceAllAts(input); is executed (where data is an ArrayList oftype String) the ArrayList data is shown below on the [email protected]@duke.edu [email protected]@@@@go duke gono more datarodger AT cs.duke.edua AT duke.edu b AT duke.edu AT AT AT AT go duke gono more dataIn writing replaceAllAts you should call the method replaceAt that you wrote in Part A.Assume that replaceAt is correct, regardless of what you wrote.Complete the method replaceAllAts below.public ArrayList<String> replaceAllAts(Scanner input){}8PROBLEM 6 : (When are we going to get snow? (32 pts))PART A (14 pts):The class Skier shown below represents a Skier who races and saves the following staterepresenting the Skier: the skier’s name, the skier’s number (each skier has a different numberthey wear), and the skier’s best time so far.A Skier will race down a run several times and only store the fastest run time. If the skierhasn’t completed a run yet, the time stored is -1.public class Skier {private String myName; // name of skierprivate int myNumber; // unique number of skierprivate int myTime; // best time in seconds// best


View Full Document

Duke CPS 006 - Test 1

Download Test 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Test 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Test 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?