CS 100, Summer 2001 Wednesday, 7/18 Lecture 13 ----------------------------------------------------------------------- Announcements: + Assignment 4 due 7/19 + Prelim 2 on 7/20 + reading: Skip text file I/O: will do next week; instead, bring written question (will collect this time!) + Ben and Kiri's office hours moved to Thursday! (from Friday) ----------------------------------------------------------------------- Topics: + debugging, part 2 + exceptions + arrays + matrices in MATLAB ----------------------------------------------------------------------- ---------------------------------------------------------------------- + Assignment 4 - Planet constructor should NOT check for valid input (it's a good thought, but what are you going to do if it isn't valid??) Plus, NoPlanet is *supposed* to be invalid. - Planet: keeping track of where it is - I don't actually care how you do it (integers for x and y, vs. Position variable, vs... something else?). This is encapsulation! You're designing the class - do whatever you want. BUT: 1) do it with good style. 2) the INTERFACE shouldn't change (so SpaceTester.java should work no matter what you're doing internally) - Default constructor: you get to pick the default values for a new Planet. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Debugging, part 2 - design first. then code. then debug. - "save now, suffer later"; "suffer now, save later" - design algorithms, then implement them - design classes, then implement them - Debugger on example (ArrayOfTemperatures.java) - see debugging guide on Java page off course website - debugger program window - class browser window - breakpoints (set, clear) - step over, into, out - this is a great tool! It's almost, but not quite, as good as having your own personal consultant. Allows you to "peek" into the computer and watch what it's really doing. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Exceptions - way to handle unusual (invalid) cases - when something bad happens, the program can "throw" an exception - you can also provide code to "catch" the exception (handle it in a graceful way) EXAMPLE: ExceptionDemo.java - syntax: try { // some code throw new Exception("Message"); } catch (Exception e) { // how to handle the exception } - note catch is NOT a method definition (more like an if-else) - analogy from Savitch: line of people tossing rocks with messages - flow of control: - if no exception thrown, catch block is ignored - if an exception is thrown, the rest of the try block is ignored - see p. 522 in Savitch - sometimes you'll explicitly "throw" the exception; sometimes you call methods which might throw exceptions, so you need to have a way to catch them. You'll see this when we do File I/O. - EXAMPLE: PrimeNumbersSoln.java - list of useful exceptions? - useful Exception: ArrayOutOfBoundsException ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Arrays - Review: arrays are objects. They store multiple items, all of the same type. - syntax for array declaration type[] name = new type[howMany]; - EXAMPLE: ArrayOfTemperatures.java - EXAMPLE: the same thing in MATLAB: aot.m - initializing arrays: for base types, you can assign values all at once: double[] myArray = {1.2, 34.6, 23.6}; is the same as double[] myArray = new double[3]; myArray[0] = 1.2; myArray[1] = 34.6; myArray[2] = 23.6; but much shorter. - indexed array elements as method arguments Spaceship2000: System.out.println(fleet[i]); - arrays as method arguments Spaceship2000: moveFormation(fleet, 3, 0); - testing equality cannot use == on two arrays (just like with references) Instead, must check each element of the two arrays. - methods that return arrays e.g., Spaceship2000[] createFormation(int howMany, int x, int y) - what would this method look like? - need to create a new array of Spaceship2000's to return - then create each one, probably inside a loop - then return the new array ---------------------------------------------------------------------- PICK UP graded Exercise 7 hand in your Exercise 8!