CS106 : Lab Exam : 1:15pm-2:30pm


Resources:
Warnings:
  • During this exam you may access your past labs and project, as well as the three documents above.
  • You may not access other internet sites (which can be verified by timestamps of the log) but are allowed to access moodle to submit your programs.
  • After the exam, network traffic history will be analyzed to check if you have complied with this requirement.
  • Do not discuss this exam with others until after Tuesday, 2:30pm.

Notes:
  • If you have difficulty making the programs work, don't give up. You may still get significant partial credit for what you submit.
  • If there are any ambiguities in the question, be sure to state your assumptions at the top of the method as a comment.
  • Use your time wisely. Some questions may be easier for you than others---if you get stuck, consider moving on to the next problem.
  • The method runAll will be used by Matt to test your program. It calls four methods, one per exam problem, and should not be modified. You may use it to help you test your code, or you may call the methods directly via BlueJ.
  • Here is an outline for problems in this exam.


Exam:
  1. Loops (25 points total):
  2. The method printSquare takes an int as a parameter and should print out a pattern in the terminal.
    • If the parameter num is less than 1 or greater than 9, the method should print an error message.
    • If num is between 0 and 10, you should print out a square with num rows and columns.
      • The first number in each row should start at num and decrease by one per row.
      • The numbers in the columns should decrease by one per column, up to the half-way mark, at which point they start increasing.
      • However, no negative numbers should be displayed. If the above rules produce a negative number, zero should be printed.
      • Example for num = 5:
        5 4 3 4 5
        4 3 2 3 4
        3 2 1 2 3
        2 1 0 1 2
        1 0 0 0 1
        
      • Example for num=6:
        6 5 4 4 5 6
        5 4 3 3 4 5
        4 3 2 2 3 4
        3 2 1 1 2 3
        2 1 0 0 1 2
        1 0 0 0 0 1
        
      • Note the (slightly) different behavior for when num is odd and when num is even.
      You will get:
    1. +5 points for printing a num by num square (e.g., if num was 5, you correctly printed out 25 numbers).
    2. +5 points for getting the first column of numbers correct (e.g., if num was 5, you correctly printed out the first column in the above example).
    3. +5 points for correctly generating the row pattern.
    4. +5 for working with all admissible values of num.
    5. +5 for no other errors.


  3. Arrays (25 points total):
  4. The analyzeSensors method current calls the readSensors method, which Matt created to simulate reading sensors on the robot. The readSensors method returns an array with three elements, corresponding to reading the left, center, and right IR sensors.
    1. +10 points:
      You will create three arrays. Make nine calls to the readSensors method, storing the results in the arrays. One array will hold nine readings for the left sensor, one will hold nine readings for the center, and one will hold nine readings for the right sensor. (If you are unable to correctly use the readSensors method, you can fill your three arrays with random numbers, enabling you to do the next two parts of the problem.)
    2. +10 points:
      Calculate and print the average reading for each of the three sensors.
    3. +5 points:
      Calculate and print the median reading for each of the three sensors. (Note: this would be easy if the array was sorted, but you can find the median even without sorting.)


  5. Graphics (25 points total):
  6. Using the MyroLine and MyroCircle objects, fill in the drawPicture method to create the following picture:

    1. +15 points:
      • The canvas is 600 by 600.
      • The center of the drawing is at 300, 300.
      • The upper left corner is at 100,100.
      • Each circle has a radius of 100 and is initially red.
      • Each square is initially blue.
    2. +10 points:
      Using the MyroListener.isKeyPressed() method, change the program so that the colors of the circles and squares are randomized every time a button is pressed.
    3. +5 points (extra credit):
      Store your MyroLine and MyroCircle objects in two arrays. Change the colors (when a key is pressed) by iterating through all elements in the two arrays.


  7. Methods/Classes (25 points total):
  8. In a new file, create a new class named NewClass. Inside this class, you will create a constructor, a public method called getValue and a private method called calcValue.
    1. +5 points:
      The NewClass constructor will have one parameter, a double named initialValue. The constructor will store this value in a private class variable named myValue.
    2. +5 points:
      The method getValue has no parameters but will return an int. The method should call calcValue and return the result as an int.
    3. +5 points:
      The method calcValue has no parameters. It returns the square of the value in the class variable myValue as a double.
    4. +5 points:
      In the testNewClass method, create a new object of type NewClass. Print out the value returned when the getValue method is called.