Assignment 9

Due Thursday March 28, 2013 at 6pm.

This assignment will be accepted up to 24 hours late without penalty

For this assignment there will be two submissions:


Part 1: Write your answer in plain English. Put all of your answers in one single text file (.txt) or a single PDF file (.pdf). Your work will not be graded if it is submitted in another format..
  1. We have seen function overloading in class. Essentially, we are allowed to have more than one function (or method in a class) with the same name as long as the list of input parameters is different. For example,

    int     foo(){...}
    int     foo(int x){...}
    boolean foo(String s){...}
    int     foo(int x, char c){...}
    
    can all exists in the same program (or in the same class). In Processing (and Java), a function (or method) is uniquely defined by its name and list of input arguments (where the order of the inputs matters). The return type of the function/method is not considered. So, int foo(){...} and char foo(){...} are not allowed in the same program (or in the same class), because Processing has no way of knowing which function you actually wanted to call.

    Explain one reason why function overloading is good and one reason why it might be dangerous (or how it might be misused).

  2. You have been busy writing lots of code for COMP1005/1405 for several weeks now. Take a step back and think about what computer science means to you. Draw a picture that reflects this. (You will not be graded for "artistic quality", but we may post* some pictures for the class to see if something interesting is submitted.)

    * If permission is granted from the student.

    Note: Please submit this question separately from the rest of the assignment. (See top of assignment for instructions.)

  3. Consider the following function:

    int mystery(int x){
       if (x < 1){
          return 0;
       }else{
          return 1 + mystery(x-2);
       }
    }
    
    In words, explain what this function does. That is, given some input value what is the output value (with respect to the input). Is this function equivalent to any other functions/operators?

    What is the result of calling mystery(14) and calling mystery(17).

    Note: The intention of this question is not for you to simply cut and paste it into a Processing sketch. You should think about what is happening and understand what is happening. Running it on Processing should be a tool to help you confirm your own thoughts about it.


Part 2: Write a Processing sketch as an answer for each of the following questions or tasks, unless otherwise stated.

Note: For each question, add the processing folder with your sketch to your ZIP file, not just your sketch (.pde file). Label each folder/sketch as Question4, Question5, etc. Do not put a space between "Question" and the number.


Note: For all of your sketches, the output window should have width and height at least 300 (but do not make them extremely large).

Note: For this and the remaining assignments, it is expected that you WILL use setup() and draw() unless specifically asked not to.

  1. Write a sketch that repeatedly draws small squares or circles at random locations in the output window with different colours so that, after many squares/circles are drawn, your output looks similar to this picture. (the Canadian flag.)

    To solve this problem, write a function called getColor() that inputs two integers numbers (int type), which are x and y coordinates, and outputs the color that a square should be drawn at that position. Use this function to determine the colour of each square you draw.

    Your draw() loop should look exactly like

    void draw(){
       x = random(width);
       y = random(height);
       fill( getColor(x,y) );
       ellipse(x,y,5,5);  // or use rect in CENTER mode
    }
    

    Note: You can do what you want in your getColor() function and in your setup() function (and outside of any function). In particular, use the image (png file) to your advantage.

  2. Create a class called Thing. Your class should have four attributes (fields) in it: two integers, called x and y, another integer called number and a boolean called negative. It should also have a constructor that looks like

    Thing(int x, int y, int n, boolean neg){...};
    
    that initializes the FOUR attributes to be equal to the values passed into the constructor.

    Add your class definition to the sketch found here (making sure it works properly) and submit this combined code as your solution.

  3. Draw three boxes on the screen (not overlapping). Inside each box should be a number from 0 to 9 (inclusive). Each box should have a random number in this range to start.

    Whenever the mouse is over a given box, and you press the 'i' key, the number in that box should be incremented by 1. (That is, it's number is increased by 1.) Whenever the mouse is over a given box, and you press the 'd' key, the number in that box should be decremented by 1. (That is, it's number is decreased by 1.)

    Make sure your program can scale up to many boxes with ease. In particular, have a global variable called num in your sketch you will set to 3 in your setup. We will change this value when testing your code, you program should then draw num boxes with the same properties stated above.

  4. Start with this code (found here) and add a function find() so that the program works correctly.

    The function should look like

    int find(Box [] box, int target){...}
    
    The function looks through the elements in the array passed as input for a Box with number equal to target. If there is a Box in the array with number that is target, the function returns the index position of this Box in the array. (If there are more than one match, the function returns the first Box it finds that is a match.) If there is no Box in the array with number equal to target, the function returns -1.

  5. We will repeat problem 7 but this time wrapping our array of Box objects in another new class: Boxes. Add a method to the Boxes class, called find, that accomplishes the same things as the find() function did in problem 7.

    Start with the code here. This code has been updated on March 23rd. The code uses two classes, Box and Boxes. Make sure you are using this one.


Bonus Problem: Repeat either problem 7 or 8, but make the find function/method return an array of integers. The array should have the index values of all boxes that have number equal to target. If there are no matches, the array should be empty (size zero).