Even even more on being classy Aaron Bloomfield

  • Slides: 42
Download presentation
Even even more on being classy Aaron Bloomfield CS 101 -E Chapter 4+ 1

Even even more on being classy Aaron Bloomfield CS 101 -E Chapter 4+ 1

Consider this sequence of events… 2

Consider this sequence of events… 2

What happened? l Java didn’t “repaint” the rectangles when necessary l Java l only

What happened? l Java didn’t “repaint” the rectangles when necessary l Java l only painted the rectangle once You can tell Java to repaint it whenever necessary l This is beyond the scope of this class, though! 3

Seeing double import java. awt. *; public class Seeing. Double { public static void

Seeing double import java. awt. *; public class Seeing. Double { public static void main(String[] args) { Colored. Rectangle r = new Colored. Rectangle(); System. out. println("Enter when ready"); Scanner stdin = new Scanner (System. in); stdin. next. Line(); r. paint(); r. set. Y(50); r. set. Color(Color. RED); r. paint(); } } 4

Seeing double l When paint() was called, the previous rectangle was not erased l

Seeing double l When paint() was called, the previous rectangle was not erased l This l is a simpler way of implementing this Perhaps clear and repaint everything when a rectangle paint() is called 5

Code from last class // Purpose: Create two windows containing colored rectangles. import java.

Code from last class // Purpose: Create two windows containing colored rectangles. import java. util. *; public class Box. Fun { //main(): application entry point public static void main (String[] args) { Colored. Rectangle r 1 = new Colored. Rectangle(); Colored. Rectangle r 2 = new Colored. Rectangle(); System. out. println("Enter when ready"); Scanner stdin = new Scanner (System. in); stdin. next. Line(); r 1. paint(); r 2. paint(); } } // draw the window associated with r 1 // draw the window associated with r 2 6

public class Colored. Rectangle { // instance variables for holding object attributes private int

public class Colored. Rectangle { // instance variables for holding object attributes private int width; private int height; private JFrame window; private int x; private int y; private Color color; // Colored. Rectangle(): default constructor public Colored. Rectangle() { color = Color. BLUE; width = 40; x = 80; height = 20; y = 90; window = new JFrame("Box Fun"); window. set. Size(200, 200); window. set. Visible(true); } // paint(): display the rectangle in its window public void paint() { Graphics g = window. get. Graphics(); g. set. Color(color); g. fill. Rect(x, y, width, height); } } 7

r public class public void paint(){ { Colored. Rectangle Graphicsint g =width; private window.

r public class public void paint(){ { Colored. Rectangle Graphicsint g =width; private window. get. Graphics (); private int x, y; g. set. Color(color ); private int height; g. fill. Rect private int (x, y; y, width, JFrame height); private window; } private Color color; Colored. Rectangle r = new Colored. Rectangle(); Color. Rectangle - width = 40 0 - height = 20 0 - x = 80 0 - y = 90 0 - color = - window = Color - color = -. . . + brighter() : Color +. . . + paint() : void public Colored. Rectangle() { color = Color. BLUE; width = 40; JFrame height = 20; - width = 200 y = 90; - height = 200 x = 80; - title = window = new - grafix = JFrame -. . . ("Box Fun"); + set. Visible (boolean status) : void window. set. Size + get. Graphics () : Graphics (200, 200); + set. Size (int w, int h) : void window. set. Visible +… (true); g } String - text = “Box Fun” -. . . + length() : int +. . . Graphics -… + fill. Rect() : void + set. Color(Color) : 8 void +. . .

The Vector class l In java. util l Must put “import java. util. *;

The Vector class l In java. util l Must put “import java. util. *; ” in the java file l Probably the most useful class in the library (in my opinion) l A Vector is a collection of “things” (objects) l It has nothing to do with the geometric vector 9

Vector methods Constructor: Vector() l Adding objects: add (Object o); l Removing objects: remove

Vector methods Constructor: Vector() l Adding objects: add (Object o); l Removing objects: remove (int which) l Number of elements: size() l Element access: element. At() l Removing all elements: clear() l 10

Vector code example Vector v = new Vector(); System. out. println (v. size() +

Vector code example Vector v = new Vector(); System. out. println (v. size() + " " + v); 0 [] v. add ("first"); System. out. println (v. size() + " " + v); 1 [first] v. add ("second"); v. add ("third"); System. out. println (v. size() + " " + v); 3 [first, second, third] String s = (String) v. element. At (2); System. out. println (s); third String t = (String) v. element. At (3); System. out. println (t); (Exception) v. remove (1); System. out. println (v. size() + " " + v); 2 [first, third] v. clear(); System. out. println (v. size() + " " + v); 0 [] 11

What we wish computers could do 12

What we wish computers could do 12

The usefulness of Vectors l You can any object to a Vector l Strings,

The usefulness of Vectors l You can any object to a Vector l Strings, l Colored. Rectanges, JFrames, etc. They are not the most efficient for some tasks l Searching, in particular 13

About that exception… l The exact exception was: Exception in thread "main" java. lang.

About that exception… l The exact exception was: Exception in thread "main" java. lang. Array. Index. Out. Of. Bounds. Exception: 3 >= 3 at java. util. Vector. element. At(Vector. java: 431) at Vector. Test. main(Vector. Test. java: 15) Where the problem occured 14

A Vector of ints l Consider the following code: Vector v = new Vector();

A Vector of ints l Consider the following code: Vector v = new Vector(); v. add (1); l Causes a compile-time error l Most of the time - see disclaimer later C: Documents and SettingsAaronMy DocumentsJCreatorVector. Test. java: 7: cannot resolve symbol : method add (int) location: class java. util. Vector v. add (1); 15

What happened? l The Vector add() method: l boolean add(Object o) l Primitive types

What happened? l The Vector add() method: l boolean add(Object o) l Primitive types are not objects! l Solution: use wrapper classes! 16

More on wrapper classes l A wrapper class allows a primitive type to act

More on wrapper classes l A wrapper class allows a primitive type to act as an object l Each primitive type has a wrapper class: l l l Boolean Character Byte Short l l Integer Long Float Double Note that char and int don’t have the exact same name as their wrapper classes 17

Vector code example Vector v = new Vector(); System. out. println (v. size() +

Vector code example Vector v = new Vector(); System. out. println (v. size() + " " + v); 0 [] v. add (new Integer(1)); System. out. println (v. size() + " " + v); 1 [1] v. add (new Integer(2)); v. add (new Integer(3)); System. out. println (v. size() + " " + v); 3 [1, 2, 3] Integer s = (Integer) v. element. At (2); System. out. println (s); 3 Integer t = (Integer) v. element. At (3); System. out. println (t); (Exception) v. remove (1); System. out. println (v. size() + " " + v); 2 [1, 3] v. clear(); System. out. println (v. size() + " " + v); 0 [] 18

Even more on wrapper classes l They have useful class (i. e. static) variables:

Even more on wrapper classes l They have useful class (i. e. static) variables: l Integer. MAX_VALUE l Double. MIN_VALUE l They have useful methods: l String s = “ 3. 14159”; l double d = Double. parse. Double (s); 19

A disclaimer l Java 1. 5 (which we are not using) has a new

A disclaimer l Java 1. 5 (which we are not using) has a new feature called “autoboxing/unboxing” l This will automatically convert primitive types to their wrapper classes (and back) 20

An optical illusion 21

An optical illusion 21

Design an air conditioner representation l Context - Consumer l l l Repair person

Design an air conditioner representation l Context - Consumer l l l Repair person Sales person Consumer l Behaviors l Attributes 22

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on Turn off Get temperature and fan setting Set temperature and fan setting Build – construction Debug Attributes 23

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on Turn off Get temperature and fan setting Set temperature and fan setting Build – construction Debug – to stringing Attributes 24

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on Turn off Get temperature and fan setting Set temperature and fan setting – mutation Build – construction Debug – to stringing Attributes 25

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on Turn off Get temperature and fan setting – accessing Set temperature and fan setting – mutation Build – construction Debug – to stringing Attributes 26

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on – mutation Turn off – mutation Get temperature and fan setting – accessing Set temperature and fan setting – mutation Build – construction Debug – to stringing Attributes 27

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on – mutation Turn off – mutation Get temperature and fan setting – accessing Set temperature and fan setting – mutation Build – construction Debug – to stringing Attributes 28

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on Turn off Get temperature and fan setting Set temperature and fan setting Build -- construction Debug Attributes l l l Power setting Fan setting Temperature setting 29

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on Turn off Get temperature and fan setting Set temperature and fan setting Build -- construction Debug Attributes l l l Power setting Fan setting Temperature setting – integer 30

Design an air conditioner representation l Context - Consumer l Behaviors l l l

Design an air conditioner representation l Context - Consumer l Behaviors l l l l Turn on Turn off Get temperature and fan setting Set temperature and fan setting Build -- construction Debug Attributes l l l Power setting – binary Fan setting – binary Temperature setting – integer 31

Ugh… 32

Ugh… 32

Design an air conditioner representation // Represent an air conditioner – from consumer view

Design an air conditioner representation // Represent an air conditioner – from consumer view point public class Air. Conditioner { // instance variables // constructors // methods } l Source Air. Conditioner. java 33

Static variables and constants // shared resource for all Air. Conditioner objects static public

Static variables and constants // shared resource for all Air. Conditioner objects static public final int OFF = 0; Static public final int ON = 1; static public final int LOW = 0; Static public final int HIGH = 1; static public final int DEFAULT_TEMP = 72; l Every object in the class has access to the same static variables and constants l A change to a static variable is visible to all of the objects in the class l Examples Static. Demo. java and Demo. Static. java 34

Instance variables // individual object attributes int power. Setting; int fan. Setting; int temperature.

Instance variables // individual object attributes int power. Setting; int fan. Setting; int temperature. Setting; l Instance variables are always initialized as soon the object comes into existence l If no value is specified l l 0 used for numeric variables false used for logical variables null used for object variables Examples Initialize. Demo. java 35

Constructors // Air. Conditioner(): default constructor public Air. Conditioner() { this. power. Setting =

Constructors // Air. Conditioner(): default constructor public Air. Conditioner() { this. power. Setting = Air. Conditioner. OFF; this. fan. Setting = Air. Conditioner. LOW; this. temperature. Setting = Air. Conditioner. DEFAULT_TEMP; } // Air. Conditioner(): specific constructor public Air. Conditioner(int my. Power, int my. Fan, int my. Temp) { this. power. Setting = my. Power; this. fan. Setting = my. Fan; this. temperature. Setting = my. Temp; } l Example Air. Conditioner. Construction. java 36

Simple mutators // turn. On(): set the power setting to on public void turn.

Simple mutators // turn. On(): set the power setting to on public void turn. On() { this. power. Setting = Air. Conditioner. ON; } // turn. Off(): set the power setting to off public void turn. Off() { this. power. Setting = Air. Conditioner. OFF; } l Example Turn. Demo. java 37

Simple accessors // get. Power. Status(): report the power setting public int get. Power.

Simple accessors // get. Power. Status(): report the power setting public int get. Power. Status() { return this. power. Setting; } // get. Fan. Status(): report the fan setting public int get. Fan. Status() { return this. fan. Setting; } // get. Temperature. Status(): report the temperature setting public int get. Temperature. Status () { return this. temperature. Setting; } l Example Air. Conditioner. Accessors. java 38

Parametric mutators // set. Power(): set the power setting as indicated public void set.

Parametric mutators // set. Power(): set the power setting as indicated public void set. Power(int desired. Setting) { this. power. Setting = desired. Setting; } // set. Fan(): set the fan setting as indicated public void set. Fan(int desired. Setting) { this. fan. Setting = desired. Setting; } // set. Temperature(): set the temperature setting as indicated public void set. Temperature(int desired. Setting) { this. temperature. Setting = desired. Setting; } l Example Air. Conditioner. Set. Mutation. java 39

Facilitator to. String() // to. String(): produce a String representation of the object public

Facilitator to. String() // to. String(): produce a String representation of the object public String to. String() { String result = "[ power: " + this. power. Setting + ", fan: " + this. fan. Setting + ", temperature: " + this. temperature. Setting + " ] "; return result; } 40

Sneak peek facilitator to. String() public String to. String() { String result = "[

Sneak peek facilitator to. String() public String to. String() { String result = "[ power: " ; if ( this. power. Setting == Air. Conditioner. OFF ) { result = result + "OFF"; } else { result = result + "ON " ; } result = result + ", fan: "; if ( this. fan. Setting == Air. Conditioner. LOW ) { result = result + "LOW "; } else { result = result + "HIGH"; } result = result + ", temperature: " + this. temperature. Setting + " ]"; return result; } 41

What computers were made for l NASA’s World. Wind l See http: //learn. arc.

What computers were made for l NASA’s World. Wind l See http: //learn. arc. nasa. gov/worldwind/ 42