COMP1000 Week 8

Recap of Skill Demonstration

  • Task 1: Green rectangles to blue circles - mostly successful.
  • Task 2: Mouse press toggling sun - most difficult due to and vs. or confusion and event handler usage.
  • Task 3: For loops - generally okay.
  • Task 4: Local variables in setup - identify variables declared within curly brackets.

Objects and Classes

  • Classes: Blueprints for objects (e.g., Ship class).
  • Objects: Instances of classes (e.g., Alice and Bob as Ship objects).
  • Attributes: Variables within a class that describe the object's properties (e.g., position, speed).
  • Constructor: Method used to create new objects from a class.
  • Methods: Functions within a class that define the object's behavior (e.g., move, steer).

Recommended Program Structure (Processing)

  • Main Tab:
    • Configuration (global variables, setup).
    • Display (draw elements without changing variables).
    • Animation (update attributes like position and angle).
    • Event Handling (mouse presses, key presses).
  • Class Tab:
    • Attributes and constructor.
    • Display methods.
    • Animation/movement methods.
    • Event handling methods.

Arrays

  • Arrays are containers for variables of the same type (e.g., float[], int[]).
  • Arrays store multiple values of the same type in a contiguous memory location.
  • Arrays can contain: int, float, character, string, color, boolean, Ship.
  • Elements accessed using an index, starting from 0 (e.g., array[0] is the first element).
  • Common error: Array index out of bounds.
  • Arrays have a fixed length determined upon creation.
  • Syntax:
    • Declaration : float[] starsX;
    • Initialization : starsX = new float[100];

Common Array Algorithms

  • Summing Values: Iterate through the array, adding each element to an accumulator variable.

    sum=sum+data[i];sum = sum + data[i];

  • Inserting a New Value: Shift existing elements to make space, then insert the new value.

  • Storing values: Increment a counter to keep tracking of how many values are being used.

Common Errors

  • Accessing an array element outside of its bounds (index out of range).
  • Offset errors: Incorrectly calculating index offsets.

Assignment: "Hopper"

  • Create a program with a creature that hops.
  • Focus on well-organized code and the use of objects/classes.
  • Consider various interpretations of "hopper" (e.g., insect, frog, Grace Hopper).