Sie sind auf Seite 1von 5

CS106A Spring 2011

Handout 20 April 22nd, 2011

CS106A Practice Midterm


Midterm exam: Thursday, April 28th, 7:00-10:00 P.M. Last Names A through P: Hewlett 200 Last Names Q through Z: Braun Auditorium

This handout is intended to give you practice solving problems that are comparable in format and difficulty to those which will appear on the midterm examination next Thursday evening. Coverage The exam covers the material presented in class through todays lecture. That means youre responsible for all material in the Karel reader and in The Art and Science of Java through Chapter 6. The exam will include questions that exercise your understanding of the RandomGenerator and Rational classes, but I will not test you on the implementation of the Rational class (which comprises a good chunk of todays lecture material.) Note: To conserve trees, I have cut back on answer space for the practice midterm. The actual exam will have much more room for your answers and for any scratch work. General instructions Answer each of the questions included in the exam. Write all of your answers directly on the examination paper, including any work that you wish to be considered for partial credit. In all questions, you may include methods or definitions that have been developed in the course, either by writing the import line for the appropriate package or by giving the name of the method and the handout or chapter number in which that definition appears. Unless otherwise indicated as part of the instructions for a specific problem, comments will not be required on the exam. Uncommented code that gets the job done elegantly and properly will be sufficient for full credit on the problem. In general, functionally correct code received full credit, although particularly convoluted implementations may lose some credit. The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind unless you have permission to do so.

2 Problem 1: Karel the Beeper Sweeper Assume Karel stands before a long corridorwith beepers everywhere. Karel wont stand for this level of filth, so he plans to walk the length of the corridor, picking up all beepers, and neatly placing them all the way to the left (and then moving one square to the right so we can take in all of his good work.) The corridor is always one street high and at least two avenues wide, and there may be any number of beepers present at any spot in the corridor. Karel starts off at the leftmost corner, facing east. Initially, Karel has no beepers in his beeper bag. Write a program that has Karel the robot scan the corridor as described above. So, if Karel is presented with the following world:

your program would transform that world into the following:

Your program should subclass the SuperKarel class so that you have access to turnLeft, turnRight, and turnAround. Your program should only use constructs that were taught during the Karel segment of the course.
/** * Implemented the program where Karel is initially facing east * and to the left of a 1 by n corridor littered with beepers (where * n >= 2). Karel runs the length of the corridor, collecting all * beepers and placing them where he started out. Assume that * * Karel starts out at the corner of 1st and 1st. * Initially, Karel has no beepers in his bag. */ public class BeeperSweeper extends SuperKarel { public void run() {

3 Problem 2: Expressions and Methods a. Compute the value of each of the following Java expressions. If an error occurs during any of these evaluations, write "Error" on that line and explain briefly why the error occurs.

4 + 5.5 / 1.1 8 / 3 "BE" "BA" (4 > 6) && (4 / 0 == 8) b. Assume that the method enigma has been defined as below: private int k int a int b int enigma(int n) { = 12 - n; = 2; = 11;

while (a < b) { if (b % a < 2) { k += a + b; } else if (a >= 6) { k *= 10; } a += 4; b++; } return k; } What does enigma(5) return?

4 Problem 3: Pythagorean Theorem As you undoubtedly learned in school, the Pythagorean Theorem holds that the length of the hypotenuse (z) of a right triangle with sides x and y is given by the following formula: x2 + y2 = z2 As it turns out, there are an infinite number of triangles in which all three of these edge lengths are integers, including the following examples:

13

5 4

3
12

32 + 42 = 52

5 2 + 12 2 = 13 2

Because of this connection to the Pythagorean Theorem, any set of integers x, y, and z that meets this condition is called a Pythagorean triple. Write a Java program that prints out all Pythagorean triples in which both x and y are less than or equal to a named constant LIMIT and x is less than y For example, if LIMIT is 25, your program should generate the following sample run:

In writing this problem, you should keep the following points in mind: You should not worry at all about efficiency. Trying every possible pairing of x and y and seeing whether it works is perfectly acceptable. The Math.sqrt method returns a double, which is only an approximation. It is possible, for example, that Math.sqrt(25) returns a double that is ever so slightly different from 5, which means that it might be come out as 4.9999999999999999 or 5.0000000000000001. The important point is that it might not be equal to an integer. Thus, to check whether an integer is a perfect square, you have to make the final test in the int domain where computation is exact. In case it comes in handy, there is a method GMath.round(x) that rounds a double to the nearest int.

5 Problem 4: SuperHarmonic Numbers The nth Harmonic number is defined to be the sum of the first n unit reciprocals. So, the 4th Harmonic numberdenoted by mathematicians as H 4 is equal to:
1 1 1 H4 =1+ + + 2 3 4 25 = 12

The nth Superharmonic numberdenoted as SH n is the product of the first n Harmonic numbers, so that SH 4 is:
SH 4 = H1 2 H 3 H 4 H 1 1 1 1 1 1 = (1)1 + 1 + + 1 + + + 2 2 3 2 3 4 275 = 48

Using the Rational class covered in lecture and in Section 6.5 of the textbook, implement the run method of a program that prints out the first LIMIT Superharmonic numbers, where LIMIT is understood to be a small positive integer. For instance, if LIMIT is specified to be 8, then your program would print out the following: 1.) 2.) 3.) 4.) 5.) 6.) 7.) 8.) 1 3/2 11/4 275/48 7535/576 73843/2304 1276429/15360 138766067/614400

Use the next page to present the implementation of your run method (dont worry about defining LIMITjust refer to it as if its defined somewhere else in the class). You must use the Rational class, so that you get exact fractions as output.

Das könnte Ihnen auch gefallen