1 / 64

Classes

Classes. Preparation. Scene so far has been background material and experience Computing systems and problem solving Variables Types Input and output Expressions Assignments Objects Standard classes and methods. Ready. Experience what Java is really about

goldy
Download Presentation

Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes

  2. Preparation • Scene so far has been background material and experience • Computing systems and problem solving • Variables • Types • Input and output • Expressions • Assignments • Objects • Standard classes and methods

  3. Ready • Experience what Java is really about • Design and implement objects representing information and physical world objects

  4. Object-oriented programming • Basis • Create and manipulate objects with attributes and behaviors that the programmer can specify • Mechanism • Classes • Benefits • An information type is design and implemented once and reused as needed • No need reanalysis and re-justification of the representation

  5. Design an air conditioner representation • Context • Behaviors • Attributes

  6. Design an air conditioner representation • Context • Repair person • Sales person • Consumer • Behaviors • Attributes

  7. Design an air conditioner representation • Context – Consumer • Behaviors • Attributes

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

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

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

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

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

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

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

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

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

  17. Design an air conditioner representation // Represent an air conditioner – from consumer view point public class AirConditioner { // instance variables // constructors // methods } • Source AirConditioner.java

  18. Static variables and constants // shared resource for all AirConditioner 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; • Every object in the class has access to the same static variables and constants • A change to a static variable is visible to all of the objects in the class • Examples StaticDemo.java and DemoStatic.java

  19. Instance variables // individual object attributes int powerSetting; int fanSetting; int temperatureSetting; • Instance variables are always initialized as soon the object comes into existence • If no value is specified • 0 used for numeric variables • false used for logical variables • null used for object variables • Examples InitializeDemo.java

  20. Constructors // AirConditioner(): default constructor public AirConditioner() { this.powerSetting = AirConditioner.OFF; this.fanSetting = AirConditioner.LOW; this.temperatureSetting = AirConditioner.DEFAULT_TEMP; } // AirConditioner(): specific constructor public AirConditioner(int myPower, int myFan, int myTemp) { this.powerSetting = myPower; this.fanSetting = myFan; this.temperatureSetting = myTemp; } • Example AirConditionerConstruction.java

  21. Simple mutators // turnOn(): set the power setting to on public void turnOn() { this.powerSetting = AirConditioner.ON; } // turnOff(): set the power setting to off public void turnOff() { this.powerSetting = AirConditioner.OFF; } • Example TurnDemo.java

  22. Simple accessors // getPowerStatus(): report the power setting public int getPowerStatus() { return this.powerSetting; } // getFanStatus(): report the fan setting public int getFanStatus() { return this.fanSetting; } // getTemperatureStatus(): report the temperature setting public int getTemperatureStatus () { return this.temperatureSetting; } • Example AirConditionerAccessors.java

  23. Parametric mutators // setPower(): set the power setting as indicated public void setPower(int desiredSetting) { this.powerSetting = desiredSetting; } // setFan(): set the fan setting as indicated public void setFan(int desiredSetting) { this.fanSetting = desiredSetting; } // setTemperature(): set the temperature setting as indicated public void setTemperature(int desiredSetting) { this.temperatureSetting = desiredSetting; } • Example AirConditionerSetMutation.java

  24. Facilitator toString() // toString(): produce a String representation of the object public String toString() { String result = "[ power: " + this.powerSetting + ", fan: " + this.fanSetting + ", temperature: " + this.temperatureSetting + " ] "; return result; }

  25. Sneak peek facilitator toString() public String toString() { String result = "[ power: " ; if ( this.powerSetting == AirConditioner.OFF ) { result = result + "OFF"; } else { result = result + "ON " ; } result = result + ", fan: "; if ( this.fanSetting == AirConditioner.LOW ) { result = result + "LOW "; } else { result = result + "HIGH"; } result = result + ", temperature: " + this.temperatureSetting + " ]"; return result; }

  26. ColoredRectangle • Purpose • Represent a colored rectangle in a window • Introduce the basics of object design and implementation

  27. Background • JFrame • Principal Java class for representing a titled, bordered graphical window. • Standard class • Part of the swing library import javax.swing.* ;

  28. Example • Consider JFrame w1 = new JFrame("Bigger"); JFrame w2 = new JFrame("Smaller"); w1.setSize(200, 125); w2.setSize(150, 100); w1.setVisible(true); w2.setVisible(true);

  29. Example • Consider JFrame w1 = new JFrame("Bigger"); JFrame w2 = new JFrame("Smaller"); w1.setSize(200, 125); w2.setSize(150, 100); w1.setVisible(true); w2.setVisible(true);

  30. Example • Consider JFrame w1 = new JFrame("Bigger"); JFrame w2 = new JFrame("Smaller"); w1.setSize(200, 125); w2.setSize(150, 100); w1.setVisible(true); w2.setVisible(true);

  31. Example • Consider JFrame w1 = new JFrame("Bigger"); JFrame w2 = new JFrame("Smaller"); w1.setSize(200, 125); w2.setSize(150, 100); w1.setVisible(true); w2.setVisible(true);

  32. Class ColoredRectangle – initial version • Purpose • Support the display of square window containing a blue filled-in rectangle • Window has side length of 200 pixels • Rectangle is 40 pixels wide and 20 pixels high • Upper left hand corner of rectangle is at (80, 90) • Limitations are temporary • Remember BMI.java preceded BMICalculator.java • Lots of concepts to introduce

  33. ColoredRectangle in action • Consider ColoredRectangle r1 = new ColoredRectangle(); ColoredRectangle r2 = new ColoredRectangle(); System.out.println("Enter when ready"); System.in.read(); r1.paint(); // draw the window associated with r1 r2.paint(); // draw the window associated with r2

  34. ColoredRectangle in action • Consider ColoredRectangle r1 = new ColoredRectangle(); ColoredRectangle r2 = new ColoredRectangle(); System.out.println("Enter when ready"); System.in.read(); r1.paint(); // draw the window associated with r1 r2.paint(); // draw the window associated with r2

  35. ColoredRectangle in action • Consider ColoredRectangle r1 = new ColoredRectangle(); ColoredRectangle r2 = new ColoredRectangle(); System.out.println("Enter when ready"); System.in.read(); r1.paint(); // draw the window associated with r1 r2.paint(); // draw the window associated with r2

  36. ColoredRectangle in action • Consider ColoredRectangle r1 = new ColoredRectangle(); ColoredRectangle r2 = new ColoredRectangle(); System.out.println("Enter when ready"); System.in.read(); r1.paint(); // draw the window associated with r1 r2.paint(); // draw the window associated with r2

  37. ColoredRectangle.java outline import javax.swing.*; import java.awt.*; public class ColoredRectangle { // instance variables for holding object attributes private int width; private int height; private int x; private int y; private JFrame window; private Color color; // ColoredRectangle(): default constructor public ColoredRectangle() { // ... } // paint(): display the rectangle in its window public void paint() { // ... } }

  38. Instance variables and attributes • Data field • Java term for an object attribute • Instance variable • Symbolic name for a data field • Usually has private access • Assists in information hiding by encapsulating the object’s attributes • Default initialization • Numeric instance variables initialized to 0 • Logical instance variables initialized to false • Object instance variables initialized to null

  39. public class ColoredRectangle { // instance variables for holding object attributes private int width; private int x; private int height; private int y; private JFrame window; private Color color; // ColoredRectangle(): default constructor public ColoredRectangle() { window = new JFrame("Box Fun"); window.setSize(200, 200); width = 40; x = 80; height = 20; y = 90; color = Color.BLUE; window.setVisible(true); } // paint(): display the rectangle in its window public void paint() { Graphics g = window.getGraphics(); g.setColor(color); g.fillRect(x, y, width, height); } }

  40. public class ColoredRectangle { // instance variables for holding object attributes private int width; private int x; private int height; private int y; private JFrame window; private Color color; // ColoredRectangle(): default constructor public ColoredRectangle() { window = new JFrame("Box Fun"); window.setSize(200, 200); width = 40; x = 80; height = 20; y = 90; color = Color.BLUE; window.setVisible(true); } // paint(): display the rectangle in its window public void paint() { Graphics g = window.getGraphics(); g.setColor(color); g.fillRect(x, y, width, height); } }

  41. ColoredRectangle default constructor

  42. public class ColoredRectangle { // instance variables for holding object attributes private int width; private int x; private int height; private int y; private JFrame window; private Color color; // ColoredRectangle(): default constructor public ColoredRectangle() { window = new JFrame("Box Fun"); window.setSize(200, 200); width = 40; x = 80; height = 20; y = 90; color = Color.BLUE; window.setVisible(true); } // paint(): display the rectangle in its window public void paint() { Graphics g = window.getGraphics(); g.setColor(color); g.fillRect(x, y, width, height); } }

  43. Color constants • Color.BLACK • Color.BLUE • Color.CYAN • Color.DARK_GRAY • Color.GRAY • Color.GREEN • Color.LIGHT_GRAY • Color.MAGENTA • Color.ORANGE • Color.PINK • Color.RED • Color.WHITE • Color.YELLOW

  44. public class ColoredRectangle { // instance variables for holding object attributes private int width; private int x; private int height; private int y; private JFrame window; private Color color; // ColoredRectangle(): default constructor public ColoredRectangle() { window = new JFrame("Box Fun"); window.setSize(200, 200); width = 40; x = 80; height = 20; y = 90; color = Color.BLUE; window.setVisible(true); } // paint(): display the rectangle in its window public void paint() { Graphics g = window.getGraphics(); g.setColor(color); g.fillRect(x, y, width, height); } }

  45. Graphical context • Graphics • Defined in java.awt.Graphics • Represents the information for a rendering request • Color • Component • Font • … • Provides methods • Text drawing • Line drawing • Shape drawing • Rectangles • Ovals • Polygons

  46. Java coordinate system

  47. public class ColoredRectangle { // instance variables for holding object attributes private int width; private int x; private int height; private int y; private JFrame window; private Color color; // ColoredRectangle(): default constructor public ColoredRectangle() { window = new JFrame("Box Fun"); window.setSize(200, 200); width = 40; x = 80; height = 20; y = 90; color = Color.BLUE; window.setVisible(true); } // paint(): display the rectangle in its window public void paint() { Graphics g = window.getGraphics(); g.setColor(color); g.fillRect(x, y, width, height); } }

  48. Method invocation • Consider r1.paint(); // display window associated with r1 r2.paint(); // display window associated with r2 • Observe • When an instance method is being executed, the attributes of the object associated with the invocation are accessed and manipulated • Important that you understand what object is being manipulated

More Related