15-100:
Introductory/Intermediate Programming
(Section F) |
Fall 2006 |
Relevant Reading:
Assignment: (worth 10 exercise points) You are encouraged to work together on this exercise. However, each person should write up their own answers to the questions and hand in their own sheet. Please put your answers on the answer sheet provided. For each of the following (Qs 1-10) tell whether the two snippets of code are execution equivalent. If they are not, explain why not. (For instance, describe a starting situation where the two snippets do something different, and state what each does.) 1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11) Write code that has a robot named karel move one block away from its current position and put down a beeper at its new location. Which direction? Any direction that is not blocked. If ALL directions are blocked (ie, karel is in a tiny box), the robot should remain where it is and not put down a beeper. 12) Write a code that has a robot named karel put down 12 beepers along the bottom street of the world. THEN, it returns to the origin, goes up to street 2 and picks up 10 beepers on the corner of street 2 and avenue 1. If there are not 10 beepers on that corner, the robot should pick up the beepers that ARE there and NOT have an error shutoff. 13) What do each of the following expressions evaluate to in Java? Feel free to type them in and find out. a) 22 /
7
b) 22 %
7
c) 7 /
22
d) 7 % 22 14) Assume the following skeleton of a program. public class Application { public static void main(String [] args) { int i = 10; double pi = 3.14159; int a = 0, b=0; char c = 'X'; double f = 0.0, g = 0.0; boolean x = true, y = true; // snippets go here } } For each of the following snippets to be inserted where indicated
above, indicate the ouput (in the console/terminal window) when the
program is run. If the snippet does not compile, say so and
why. Each snippet is intended to be taken
SEPARATELY.
EXTRA CREDIT: Do problem 4 on page 170 of the Karel J Robot text. Handin:
|