Problem Solving: Karel and Elementary Graphics Final Exam Review Exam Date: ______________ Exam Time: ______________ Overview Topics for the exam will include anything that is in the book, in your class notes, or from your exercises. That means, READ and STUDY your book, notes, and exercises. The exam includes both objective questions and free-response questions. You will be asked to write and analyze code. For the free response, you will be permitted to use a “template” to begin your work. Broad Topics Problem solving strategies Methods and classes Conditional statements Loops Parameters Variables and user input Arithmetic and Logical Operators Review Questions 1. Describe the problem solving process. - Specification, design, implementation, testing/debugging 2. Explain the reason why making new methods is important. What are some good strategies for determining when a new method is necessary? - Takes long code and breaks it up; allows for divide and conquer strategy; allows for reusability of code 3. What kinds of conditional tests can be used with Karel the Robot? What kind of conditional tests can be used with graphics? - Robot: evaluate the robot’s environment or the robot itself (eg. frontIsClear, anyBeepersInBeeperBag) Graphics: evaluate state of a variable or shape (e.g. if (slope > 0), while(radius >0) 1 4. When is it appropriate to use an if/else statement? When is it appropriate to use nested conditions? - if/else is for choosing between two options of code, based on one condition - nested: when one condition is dependent on another 5. What are the benefits of looping mechanisms? - Makes code easier to read, easier to modify 6. When is it appropriate to use an iterative loop? When is it appropriate to use a conditional loop? - Iterative: known number of repetitions; conditional: continue until condition is met 7. What are parameters and how do they benefit problem solving? - Data “sent” to a method; customize a method to make it adaptable 8. When writing a method that uses parameters, how is the formal parameter list set up? - Data type / identifier: void drawDiamond(int x, int y, int radius) 10. What is the difference between a “double” and an “int”? - Int truncates decimals (no decimal point); doubles have “real” number precision 11. Given the following declarations, determine the result of each statement. If you think the statement causes an error, say why. int x1 = 50, y1 = 10, x2 = 90, y2 = 38, radius = 24; a. int Answer = radius / 10; 2 b. int Answer = radius * 0.5; error c. double Answer = radius / 4.0; 6.0 d. double Answer = 1 / 2 * x2; 0.0 e. double Answer = radius / 2.0; 12.0 f. double Answer = (y2 – y1)*1.0 / (x2 – x1); 0.7 2 12. For the following block of code, a) correct the syntax errors, and b) draw the resulting window: import cs.ssa.*; class Unknown1 { static public void main (String[] args) { SSAWindow canvas = new SSAWindow (500, 300); int a, b, c; a = 100; B = 150 c = 50; While (c > 0); { if (c == 20) { canvas.drawOval(a, b, c, c); } canvas.drawRect(a, b, c, c) c = c – 10; } } } 13. For the following block of code, a) correct the syntax errors, and b) draw the resulting window: import cs.ssa.*; class Unknown2 { static public void main (String[] args) { SSAWindow calcWindow = new SSAWindow (500); int b, e, r; b = 2; e = 8 r = 1; for ( int i = 1; i <= e i++ ) { r = r * b; } GraphicsWindow.drawString( "Here is the result: ", 20, 50 ); calcWindow.drawString(b + " to the " + e + " = " + r, 20, 70 ); } 3 13. If the center of the upper oval is x, y and the radius is “rad”, label the coordinates of the significant points for drawing the figure: 14. For the following shape, imagine that the center point is at (xC, yC). You are given “size” as the width of the largest square. Label the coordinates of the significant points for drawing the figure: 4