CS 100, Summer 2001 Tuesday, 7/17 Lecture 12 ----------------------------------------------------------------------- Announcements: + Assignment 4 due 7/19 + Prelim 2 on 7/20 + reading: Savitch 6.2, Chapman 2.3, 2.4, 2.5, 2.7, 2.0 ----------------------------------------------------------------------- Topic: + arrays ----------------------------------------------------------------------- ---------------------------------------------------------------------- + Online submission: did it work? - Allows us to run our own tests on your code + Assignment 4 - update to Spaceship.java - how to scroll output window: see instructions on Assignments page ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Summary from last time - encapsulation (remaining questions?) - graphics ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Graphics - WindowDestroyer - use Savitch's class, if you like - paint() takes in a Graphics object, g - what does it look like? you don't need to know! (remember encapsulation?) - just use it to call graphics methods + to explicitly force a redraw (e.g., if you've updated some variables that will cause a change in the image), call repaint(). This will do some stuff and then call your paint(). ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Arrays - new kind of object: a group of objects that are all the same type - these are numbered from 0 up - EXAMPLE: ArrayOfTemperatures.java - want to read in 7 temperatures, output the average, and indicate which ones are above and below average - could have 7 doubles to store them, but this gets impractical if we want to increase the number of days we're analyzing - solution: use an array! - syntax for array declaration type[] name = new type[howMany]; - Three uses for square brackets: 1. To indicate a type, e.g. int[] myArray; 2. To indicate the size of an array, e.g. myArray = new int[26]; 3. To index an element of an array, e.g. int x = myArray[5]; - note the "new"! Arrays are objects. This means they can have variables and methods. A useful one is: array.length stores how long the variable is. (Compare with String.length()) - Java keeps track of array bounds for you - it will "throw an exception" if you use an invalid index, for example: array[-1] array[24387534] // if array doesn't have that many elements - more about exceptions tomorrow - TRY IT YOURSELF: Draw pictures to follow what's happening with the arrays in ArrayOfTemperatures.java EXAMPLE: Spaceship2000.java - implement the moveFormation() method as an example of how to use arrays - solution is posted on the Syllabus page for today ---------------------------------------------------------------------- ---------------------------------------------------------------------- PICK UP graded Exercise 7, download Exercise 8 from webpage