DOC PREVIEW
UVA CS 101 - CS 101

This preview shows page 1-2-3-20-21-40-41-42 out of 42 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 necessarynecessaryJava only painted the rectangle onceJava only painted the rectangle onceYou can tell Java to repaint it whenever You can tell Java to repaint it whenever necessarynecessaryThis 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 doubleWhen paint() was called, the previous When paint() was called, the previous rectangle was not erasedrectangle was not erasedThis is a simpler way of implementing thisThis is a simpler way of implementing thisPerhaps 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 classIn java.utilIn java.utilMust put “import java.util.*;” in the java fileMust put “import java.util.*;” in the java fileProbably 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 methodsConstructor: Vector()Constructor: Vector()Adding objects: add (Object o);Adding objects: add (Object


View Full Document

UVA CS 101 - CS 101

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

Classes

Classes

83 pages

Iteration

Iteration

92 pages

Classes

Classes

186 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

Load more
Download CS 101
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 101 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 101 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?