CS 100, Summer 2001 Monday, 7/16 Lecture 11 ----------------------------------------------------------------------- Announcements: + Assignment 4 due 7/19 - note Spaceship.java has changed (again) - download new version + Prelim 2 on 7/20 (topics posted to Web soon) + reading: Savitch 6.1, 8.1 ----------------------------------------------------------------------- Topics: + encapsulation + graphics, part 1 ----------------------------------------------------------------------- ---------------------------------------------------------------------- + Online submission: did you get a password via email? - Use for Exercise 7 and Assignment 4 - Allows us to run our own tests on your code + Minute paper: - most clear: references (good!) - most confusing: encapsulation and abstraction (more today!) - how to get rid of objects when you're done with them? - Java is "garbage collected": no need for the programmer to explicitly delete objects that have been created + Methods miscellanea - for void methods, "return" takes no arguments - .equals() lets you compare two objects of the same class type - must return a boolean - you'll be writing one of these to compare Planets - .toString() lets you define how to display objects - must return a String - you'll be writing one of these for the Planet class, too + Assignment 4 - how to scroll output window: see instructions on Assignments page ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Encapsulation: hiding the details, separating the interface from the implementation - interface: what a programmer needs to know, to be able to use your class - implementation: how you actually wrote the class (all the details that whoever is using your class need not know) - see Encapsulation guidelines (style rules!) on p. 257-8. - EXAMPLE: Complex.java - interface: a + bi - implementation: two integers - EXAMPLE: Money - interface: $x.yz - implementation: String? Double? Two integers? - EXAMPLE: cars (mechanics know the details, but you probably don't need (or want) to know the miniscule ones; you just use the "interface" (steering wheel, clutch, gearshift, etc.)) - main point: you want to be able to use a class at the INTERFACE level, and leave the details to someone you trust (mechanic, programmer) + private (non-static) methods in a class: "helping" methods + static methods cannot call non-static methods (or use instance (non- static) variables) - this makes sense: no object for them to refer to! + when to use static vs. non-static? - EXAMPLE: two versions of Complex.add() public static Complex add(Complex c, Complex d) { return new Complex(c.real + d.real, c.imag + d.imag); } To call: Complex c = new Complex(1, 2); Complex d = new Complex(3, 4); Complex sum = Complex.add(c, d); public Complex add(Complex d) { return new Complex(this.real + d.real, this.imag + d.imag); } To call: Complex c = new Complex(1, 2); Complex d = new Complex(3, 4); Complex sum = c.add(d); Because addition is a symmetric (commutative) operation, the first version is preferred (it treats both arguments the same). However, both are correct (they do the same thing). This is a design choice. - In other languages (e.g., C++) you are allowed to overload operators (like methods), so you could define "+" for the Complex class to do the right thing, then do Complex sum = c + d; But Java doesn't allow you to do this. + "this": the current object - so, inside Spaceship, these are equivalent: move(); this.move(); - sometimes you might want to write the "this" part explicitly, for clarity (up to your personal style) ----------------------------------------------------------------------- ---------------------------------------------------------------------- + Graphics! (Fun break from OOP) - Savitch says you need to have read chapter 12 (Swing), but this is false. Just ignore the parts that refer to Swing. We'll return to it later. - screen coordinate system: - (0,0) is upper left - remember, for Spaceship, we had (1,1) at upper left - coordinates here refer to individual pixels (anyone know what that stands for?), in (x,y) order - therefore, they must be INTEGERS (can't have half a pixel!) + EXAMPLE: Madeleine.java from Savitch - note: must have import javax.swing.*; import java.awt.*; at the beginning of your Java file, to use graphics - remove the WindowDestroyer() line (generates compiler error) - play with different values for the named constants at the beginning of the file + new useful class: Graphics - for graphics applications, you must define a paint() method - this is called by the computer every time the window is redrawn (displayed) - to see this in action, uncomment the "painting" line in Madeleine's paint() method - 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 + Examples: - g.drawLine(), g.fillOval(), g.drawOval(), etc. - useful methods on pages 957-8 - p. 962 has methods that require array inputs. We start arrays tomorrow! + 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(). + Colors! - each drawing method uses a "pen" that indicates the current color - you can change this with the setColor() method - see list of predefined colors on p. 778 - can also define your own colors, using RGB notation (see p. 970) + EXAMPLE: MadeleineColor.java from Savitch + Text! - you can add text strings to your image - use drawString(String text, int x, int y) - (x,y) indicate the starting point for the text - you can also change fonts (see p. 980 if interested) ---------------------------------------------------------------------- PICK UP graded Assignment 3, handed out Exercise 7