Even even more on being classyConsider this sequence of events…What happened?Seeing doubleSlide 5Code from last classSlide 7Slide 8The Vector classVector methodsVector code exampleWhat we wish computers could doThe usefulness of VectorsAbout that exception…A Vector of intsSlide 16More on wrapper classesSlide 18Even more on wrapper classesA disclaimerAn optical illusionDesign an air conditioner representationSlide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Ugh…Slide 33Static variables and constantsInstance variablesConstructorsSimple mutatorsSimple accessorsParametric mutatorsFacilitator toString()Sneak peek facilitator toString()What computers were made for11Even even more Even even more on being classyon being classyAaron BloomfieldAaron BloomfieldCS 101-ECS 101-EChapter 4+Chapter 4+22Consider this sequence of events…Consider this sequence of events…33What happened?What happened?Java didn’t “repaint” the rectangles when Java didn’t “repaint” the rectangles when necessarynecessaryJava only painted the rectangle onceJava only painted the rectangle onceYou can tell Java to repaint it whenever You can tell Java to repaint it whenever necessarynecessaryThis is beyond the scope of this class, though!This is beyond the scope of this class, though!44Seeing doubleSeeing doubleimport java.awt.*;import java.awt.*;public class SeeingDouble {public class SeeingDouble {public static void main(String[] args) {public static void main(String[] args) {ColoredRectangle r = new ColoredRectangle();ColoredRectangle r = new ColoredRectangle();System.out.println("Enter when ready");System.out.println("Enter when ready");Scanner stdin = new Scanner (System.in);Scanner stdin = new Scanner (System.in);stdin.nextLine();stdin.nextLine();r.paint();r.paint();r.setY(50);r.setY(50);r.setColor(Color.RED);r.setColor(Color.RED);r.paint();r.paint();}}}}55Seeing doubleSeeing doubleWhen paint() was called, the previous When paint() was called, the previous rectangle was not erasedrectangle was not erasedThis is a simpler way of implementing thisThis is a simpler way of implementing thisPerhaps clear and repaint everything Perhaps clear and repaint everything when a rectangle paint() is calledwhen a rectangle paint() is called66// Purpose: Create two windows containing colored rectangles.// Purpose: Create two windows containing colored rectangles.import java.util.*;import java.util.*;public class BoxFun {public class BoxFun {//main(): application entry point//main(): application entry pointpublic static void main (String[] args) {public static void main (String[] args) {ColoredRectangle r1 = new ColoredRectangle();ColoredRectangle r1 = new ColoredRectangle();ColoredRectangle r2 = new ColoredRectangle();ColoredRectangle r2 = new ColoredRectangle();System.out.println("Enter when ready");System.out.println("Enter when ready");Scanner stdin = new Scanner (System.in);Scanner stdin = new Scanner (System.in);stdin.nextLine();stdin.nextLine();r1.paint(); // draw the window associated with r1r1.paint(); // draw the window associated with r1r2.paint(); // draw the window associated with r2r2.paint(); // draw the window associated with r2}}}}Code from last classCode from last class77public class ColoredRectangle {public class ColoredRectangle {// instance variables for holding object attributes// instance variables for holding object attributesprivate int width; private int width; private int x; private int x;private int height; private int height; private int y; private int y;private JFrame window; private JFrame window; private Color color; private Color color;// ColoredRectangle(): default constructor// ColoredRectangle(): default constructorpublic ColoredRectangle() {public ColoredRectangle() {color = Color.BLUE;color = Color.BLUE;width = 40; width = 40; x = 80;x = 80;height = 20;height = 20;y = 90;y = 90;window = new JFrame("Box Fun");window = new JFrame("Box Fun");window.setSize(200, 200);window.setSize(200, 200);window.setVisible(true);window.setVisible(true);}}// paint(): display the rectangle in its window// paint(): display the rectangle in its windowpublic void paint() {public void paint() {Graphics g = window.getGraphics();Graphics g = window.getGraphics();g.setColor(color);g.setColor(color);g.fillRect(x, y, width, height);g.fillRect(x, y, width, height);}}}}88String- text = “Box Fun” - ...+ length() : int + ...+ setVisible (boolean status) : void+ getGraphics () : Graphics+ setSize (int w, int h) : void+ …JFrame- width = 200- height = 200- title =- grafix =- ...Graphics- … + setColor(Color) : void + ...+ fillRect() : voidr+ paint() : void ColorRectangle- width = 0- height = 0- x = 0- y = 0- color =- window =Color- color =- ...+ brighter() : Color+ ... ColoredRectangle r = new ColoredRectangle();+ paint() : void ColorRectangle- width = 40- height = 20- x = 80- y = 90- color =- window =public class public class ColoredRectangle {ColoredRectangle {private int width;private int width;private int x, y;private int x, y;private int height;private int height;private int y;private int y;private JFrame window;private JFrame window;private Color color;private Color color;public ColoredRectangle() {public ColoredRectangle() { color = Color.BLUE;color = Color.BLUE; width = 40;width = 40; height = 20;height = 20; y = 90;y = 90; x = 80;x = 80; window = new window = new JFrameJFrame("Box Fun");("Box Fun"); window.setSizewindow.setSize(200, 200);(200, 200); window.setVisiblewindow.setVisible(true);(true);}}public void paint() {public void paint() { Graphics g = Graphics g = window.getGraphics();window.getGraphics(); g.setColor(color);g.setColor(color); g.fillRect (x, y, g.fillRect (x, y, width, height);width, height);}}g99The Vector classThe Vector classIn java.utilIn java.utilMust put “import java.util.*;” in the java fileMust put “import java.util.*;” in the java fileProbably the most useful class in the Probably the most useful class in the library (in my opinion)library (in my opinion)A Vector is a collection of “things” (objects)A Vector is a collection of “things” (objects)It has nothing to do with the geometric vectorIt has nothing to do with the geometric vector1010Vector methodsVector methodsConstructor: Vector()Constructor: Vector()Adding objects: add (Object o);Adding objects: add (Object
View Full Document