DOC PREVIEW
Stanford CS 106A - Study Notes

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 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 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Eric Roberts Handout #37CS 106A February 5, 2010Practice Midterm Examination #2Please remember that the midterm is open-book.3:15–5:15 in McCullough 1157:00–9:00 in Hewlett 201Problem 1: Karel the Robot (10 points)Seeing as how it’s getting on toward Valentine’s Day, Karel has decided to makevalentines (represented by beepers, of course) for every student in the elementary schoolclass that is using Karel to learn about programming. Karel does not remember exactlyhow many desks there are in each horizontal row but does remember that there areprecisely four rows of desks and that the classroom looks something like the one shownin the following diagram:10 11 12 1312345678912345678910Karel may count on the following facts:1. Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number ofbeepers in its bag.2. There are four rows of student desks, positioned as shown in the diagram, just to thenorth of 2nd, 4th, 6th, and 8th Streets.3. Karel does not know how many desks there are in each row (which may not all be thesame), or how many blank spaces there are between the desks, or how many spacesexist between the desks at the ends of each row and the walls of the classroom. WhatKarel does know is that each of the desks is exactly one unit wide and that there are– 2 –no desks right up against the wall, which means that there is always a corridor along1st Avenue for Karel to walk from one row to the next.When Karel is done, all of the desks in the room should have a Valentine, as shown in thefollowing diagram:10 11 12 1312345678912345678910Karel may end up facing any direction on any convenient square.Write the Karel program necessary to deliver valentines to all of the desks in the room.As always, your task is to get them to the desks, and you need not try to be particularlyefficient about it.In your solution, you may use any Karel method defined in the course handouts just bygiving its name. For example, you may use turnRight or moveToWall without writingdown the complete definition.Problem 2: Simple Java expressions, statements, and methods (10 points)(2a) Compute the value of each of the following Java expressions. If an error occursduring any of these evaluations, write “Error” on that line and explain briefly whythe error occurs.5.0 / 4 - 4 / 57 < 9 - 5 && 3 % 0 == 3"E" - "A"– 3 –(2b) Assume that the method mystery has been defined as given below:private int mystery(int n) { while (n >= 10) { int k = 0; while (n > 0) { k += n % 10; n /= 10; } n = k; } return n;}What is the value ofmystery(1729)(2c) What output is printed by the following program:/* * File: Problem2c.java * -------------------- * This program doesn't do anything useful and exists only to * test your understanding of parameters and string methods. */import acm.program.*;public class Problem2c extends ConsoleProgram {public void run() {String s1 = "Heart";String s2 = valentine("candy", s1);println("s1 = " + s1);println("s2 = " + s2);}private String valentine(String s1, String s2) {int num = (s1.substring(1, 2)).length();s1 = s2.substring(num);s2 = cupid(s1, s2.charAt(0));return (s2);}private String cupid(String s1, char ch) {return (s1 + Character.toLowerCase(ch));}}– 4 –Problem 3: Simple Java programs (15 points)As you undoubtedly learned in school, the Pythagorean Theorem holds that the length ofthe hypotenuse (z) of a right triangle with sides x and y is given by the following formula:x2 + y2 = z2As it turns out, there are an infinite number of triangles in which all three of these edgelengths are integers, including the following examples:3451251332 + 42 = 5252 + 122 = 132Because of this connection to the Pythagorean Theorem, any set of integers x, y, and zthat 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 lessthan 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 yand seeing whether it works is perfectly acceptable.• The Math.sqrt method returns a double, which is only an approximation. It ispossible, for example, that Math.sqrt(25) returns a double that is ever so slightlydifferent from 5, which means that it might be come out as 4.9999999999999999 or5.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 inthe int domain where computation is exact. In case it comes in handy, there is amethod GMath.round(x) that rounds a double to the nearest int.– 5 –Problem 4: Using the graphics and random number libraries (15 points)You never see a frog so modest and straightfor’ard as he was, for all he was sogifted. And when it come to fair and square jumping on a dead level, he couldget over more ground at one straddle than any animal of his breed you ever see.—Mark Twain, “The Notorious Jumping Frog of Calaveras County,” 1865As you may recall, the winner in the aesthetic division of this year’s Karel Contest playedthe game of Frogger, in which the object is to guide a frog across a screen filled withmoving cars, floating logs, and alligators. Such a game is beyond the scope of an examproblem, but it is relatively straightforward to write the code that (1) puts an image of thefrog on the screen and (2) gets the frog to jump when the user clicks the mouse.Your first task in this problem is to place the frogat the bottom of the graphics window, as shown onthe right. The frog itself is the easy part becauseall you need to do is create a GImage object withthe appropriate picture, as follows:GImage frog = new GImage("frog.gif");SimpleFroggerThe harder part is getting the image in the appropriate place in the bottom of the window.In Frogger, the frog image cannot be just anywhere on the screen but must instead occupya position in an imaginary grid such as the oneshown on the right. The size of the grid iscontrolled by three named constants, which havethe following values for this grid:public static final int SQSIZE = 75;public static final int NCOLS = 7;public static final int NROWS = 3;SimpleFroggerThe SQSIZE constant indicates that each of the


View Full Document

Stanford CS 106A - Study Notes

Download Study Notes
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 Study Notes 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 Study Notes 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?