ClassesAnnouncementsPreparationObject-oriented programmingFirst class – ColoredRectangleBackgroundSome Java Swing componentsExampleSlide 9Class ColoredRectangle – initial versionColoredRectangle in actionSlide 12ColoredRectangle.java outlineInstance variables and attributesSlide 15ColoredRectangle default constructorSlide 17Color constantsSlide 19Another possible ConstructorSlide 21Graphical contextJava coordinate systemMethod invocationSlide 25The Ig Nobel PrizesWednesday, 6 October, 2004Improving ColoredRectangleSlide 29A mutator methodMutator setWidth() evaluationA bit of humor…Java parameter passingSlide 34Slide 35Slide 36Slide 37Slide 38SubtletiesOther mutatorsSlide 41Mutator usageAccessorsAccessor usageSpecific constructionSlide 46Seeing doubleSlide 48An optical illusionCastingCasting, take 2Casting, take 3Casting, take 41ClassesCS 101-EChapter 4Aaron Bloomfield2AnnouncementsHWs are being renumberedJ1, J2, etc., for Java programming assignmentsC1, C2, etc., for CodeLab assignmentsHW1 = J1, HW2 = C1, HW3 = C2, etc.HWs J2 and J3 assigned this Wednesday (6 Oct)J2 due next Thursday (14 Oct)J3 due following Thursday (21 Oct)HW J4 will be assigned 18 Oct, and due 29 OctSome CodeLab HWs in there as wellSecond midterm on 27 OctNo labs this SundayCan go to another lab with permissionLab quiz grades will be entered by the end of this week3PreparationScene so far has been background material and experienceComputing systems and problem solvingVariablesTypesInput and outputExpressions AssignmentsObjectsStandard classes and methodsNow: Experience what Java is really aboutDesign and implement objects representing information and physical world objects4Object-oriented programmingBasisCreate and manipulate objects with attributes and behaviors that the programmer can specifyMechanismClassesBenefitsAn information type is design and implemented onceReused as neededNo need reanalysis and re-justification of the representation5First class – ColoredRectangle PurposeRepresent a colored rectangle in a windowIntroduce the basics of object design and implementation6BackgroundJFramePrincipal Java class for representing a titled, bordered graphical window.Standard classPart of the swing libraryimport javax.swing.* ;7Some Java Swing components8ExampleConsiderJFrame w1 = new JFrame("Bigger");JFrame w2 = new JFrame("Smaller");w1.setSize(200, 125);w2.setSize(150, 100);w1.setVisible(true);w2.setVisible(true);ConsiderJFrame w1 = new JFrame("Bigger");JFrame w2 = new JFrame("Smaller");w1.setSize(200, 125);w2.setSize(150, 100);w1.setVisible(true);w2.setVisible(true);ConsiderJFrame w1 = new JFrame("Bigger");JFrame w2 = new JFrame("Smaller");w1.setSize(200, 125);w2.setSize(150, 100);w1.setVisible(true);w2.setVisible(true);200 pixels 150 pixels125pixels100pixels// Purpose: Displays two different windows.import javax.swing.*;public class TwoWindows {// main(): application entry pointpublic static void main (String[] args) {JFrame w1 = new JFrame("Bigger");JFrame w2 = new JFrame("Smaller");w1.setSize(200, 125);w2.setSize(150, 100);w1.setVisible(true);w2.setVisible(true);}}10Class ColoredRectangle – initial versionPurposeSupport the display of square window containing a blue filled-in rectangleWindow has side length of 200 pixelsRectangle is 40 pixels wide and 20 pixels highUpper left hand corner of rectangle is at (80, 90)Limitations are temporaryRemember BMI.java preceded BMICalculator.javaLots of concepts to introduce11ColoredRectangle in actionConsiderColoredRectangle r1 = new ColoredRectangle();ColoredRectangle r2 = new ColoredRectangle();System.out.println("Enter when ready");Scanner stdin = new Scanner (System.in);stdin.nextLine();r1.paint(); // draw the window associated with r1r2.paint(); // draw the window associated with r2ConsiderColoredRectangle r1 = new ColoredRectangle();ColoredRectangle r2 = new ColoredRectangle();System.out.println("Enter when ready");Scanner stdin = new Scanner (System.in);stdin.nextLine();r1.paint(); // draw the window associated with r1r2.paint(); // draw the window associated with r2ConsiderColoredRectangle r1 = new ColoredRectangle();ColoredRectangle r2 = new ColoredRectangle();System.out.println("Enter when ready");Scanner stdin = new Scanner (System.in);stdin.nextLine();r1.paint(); // draw the window associated with r1r2.paint(); // draw the window associated with r2ConsiderColoredRectangle r1 = new ColoredRectangle();ColoredRectangle r2 = new ColoredRectangle();System.out.println("Enter when ready");Scanner stdin = new Scanner (System.in);stdin.nextLine();r1.paint(); // draw the window associated with r1r2.paint(); // draw the window associated with r2r1.paint()The messages instruct the objects to display themselvesr2.paint()ColoredRectangle object referenced by r1 is being sent a messageColoredRectangle object referenced by r2 is being sent a message// Purpose: Create two windows containing colored rectangles.import java.util.*;public class BoxFun {//main(): application entry pointpublic static void main (String[] args) {ColoredRectangle r1 = new ColoredRectangle();ColoredRectangle r2 = new ColoredRectangle();System.out.println("Enter when ready");Scanner stdin = new Scanner (System.in);stdin.nextLine();r1.paint(); // draw the window associated with r1r2.paint(); // draw the window associated with r2}}13ColoredRectangle.java outlineimport javax.swing.*;import java.awt.*;public class ColoredRectangle {// instance variables for holding object attributesprivate int width; private int height; private int x;private int y;private JFrame window;private Color color;// ColoredRectangle(): default constructorpublic ColoredRectangle() { // ...}// paint(): display the rectangle in its windowpublic void paint() { // ...}}14Instance variables and attributesData fieldJava term for an object attributeInstance variableSymbolic name for a data fieldUsually has private accessAssists in information hiding by encapsulating the object’s attributesDefault initializationNumeric instance variables initialized to 0Logical instance variables initialized to falseObject instance variables initialized to nullpublic class ColoredRectangle {// instance variables for holding object attributesprivate int width; private int x;private int height;
View Full Document