{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# q1soln - CS 302 Fall 2006 Quiz 1(I 0 points Solution This...

This preview shows pages 1–2. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 302 Fall 2006 Quiz 1 (I 0 points) Solution This quiz is due no later than 2 pm on Friday, September 29. You may turn the quiz in in~class on Thursday, September 28 or drop it off in Rebecca Hasti‘s mailbox on the 5th ﬂoor of the CS building no later than 2 pm, Friday, September 29. You may use your lecture notes, the textbook, the CS 302 website, CodeLab, and Eclipse to answer these questions. You may not get help on this quiz from any other person except a CS 302 lab instructor or lecturer. Answers must be hand-written. Show all your work on the quiz. Multiple Choice [2 questions, 2 points each, 4 total points] For each question circle the one best answer after reading all of the choices. Note: no partial credit is given for multiple choice questions. 1) Given the following incomplete deﬁnition of the Bug class used to represent bug objects each of which has a name and a position on the x-axis: public class Bug { private String name; private int position; public Bug(String myName, int myPos) { name = myName; position = myPos; } public Bug(int myPos) { name = "none"; position = myPos; } public String getName() { return name; } public int getPosition() { return position; } public void hop(int amount) { // body of hop // l l The hop method is used to move the bug along the x—axis by the speciﬁed amount. A positive amount corresponds to the bug moving to the right and a negative amount corresponds to the bug moving to the left. Which of the following lines of code should be used to replace / / body of hop / / so that the position of the bug is appropriately updated? Answer: D. position = position + amount; Continues on back 2) What is the output for the following code segment? Assume that the code provided compiles and runs correctly without errors and that the Bug class has the correct code for its hop method. int X = 4,. USE; Ox memorj Almog‘mm “it; int y = x; ‘i’t‘mm/ w‘nmt‘: \$0im3 0qu Bug bugl = new Bug(x); Bug 131192 = bugl; X L El 1 y—2; bug2.hop(l); \l \yl/li System. out .print (x); System.out.println (bugl . getPosition () ); bug/L L______ Answer: E. 45 Written Answer [I question, 6p01'nts] 3) Suppose the Bug class has the correct code for its hop method and that the Bug class is in the simulation package. Write a complete Java program that does the following 0 creates a bug named ant that starts out at position 7 on the X—axis, 0 makes the bug hop 8 positions to the right and then makes the bug hop 5 spaces to the left, and 0 then prints out the bug's name and ﬁnal position in the format bug_nazne is at position bug_position where bug_name is the name of the bug and bug_posi tion is the bug's position and these have been obtained by calling the appropriate methods with your Bug object. import simulation.Bug; public class BugApp { public static void main(String args) { Bug ant = new Bug("ant", 7); ant.hop(8); ant.hop(—5); System.out.print(ant.getName()); System.out.print(" is at position "); System.out.println(ant.getPosition()); ...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

q1soln - CS 302 Fall 2006 Quiz 1(I 0 points Solution This...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online