classes n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Classes PowerPoint Presentation
Download Presentation
Classes

Loading in 2 Seconds...

play fullscreen
1 / 130
Download Presentation

Classes - PowerPoint PPT Presentation

rolandc
0 Views
Download Presentation

Classes

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Classes Chapter 4 Spring 2005 CS 101 Aaron Bloomfield

  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 • Now: Experience what Java is really about • Design and implement objects representing information and physical world objects

  3. 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 • Reused as needed • No need reanalysis and re-justification of the representation

  4. First class – ColoredRectangle • Purpose • Represent a colored rectangle in a window • Introduce the basics of object design and implementation

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

  6. Some Java Swing components

  7. 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); • 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); • 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);

  8. // Purpose: Displays two different windows. import javax.swing.*; public class TwoWindows { // main(): application entry point public 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); } }

  9. An optical illusion

  10. Another optical illusion

  11. 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

  12. ColoredRectangle in action • Consider 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 r1 r2.paint(); // draw the window associated with r2 • Consider 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 r1 r2.paint(); // draw the window associated with r2 • Consider 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 r1 r2.paint(); // draw the window associated with r2 • Consider 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 r1 r2.paint(); // draw the window associated with r2

  13. // Purpose: Create two windows containing colored rectangles. import java.util.*; public class BoxFun { //main(): application entry point public 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 r1 r2.paint(); // draw the window associated with r2 } }

  14. 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() { // ... } }

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

  16. 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); } }

  17. public class ColoredRectangle { // instance variables to describe object attributes ... // ColoredRectangle(): default constructor public ColoredRectangle() { ... The name of a constructor always matches the name of its class } ... A constructor does not list its return type. A constructor always returns a reference to a new object of its class } ColoredRectangle default constructor

  18. Quick survey • I understand the purpose of a constructor • Very well • With some review, I’ll be good • Not really • Not at all

  19. 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); } }

  20. 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

  21. ColorRectangle - width = 40 - height = 20 - x = 80 The value of a - y = 90 ColoredRectangle - window = variable is a String - color = reference to a - text = "Box Fun" ColoredRectangle void + paint() : - ... object int + length() : + ... Color JFrame - color = - width = 200 - ... - height = 200 - title = + brighter() : Color - ... + ... boolean void + setVisible( status) : + ... r ColoredRectangle r = new ColoredRectangle();

  22. Quick survey • I understood the memory diagram from the previous slide • Very well • With some review, I’ll be good • Not really • Not at all

  23. Computer bugs

  24. Another possible Constructor public class ColoredRectangle { // instance variables for holding object attributes private int width = 40; private int x = 80; private int height = 80; private int y = 90; private JFrame window; private Color color = Color.BLUE; // ColoredRectangle(): default constructor public ColoredRectangle() { window = new JFrame("Box Fun"); window.setSize(200, 200); window.setVisible(true); }

  25. 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); } }

  26. 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

  27. Java coordinate system

  28. 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

  29. Instance variable window references the JFrame attribute of the object that caused the invocation. The values of these instance variables are also from the ColoredRectangle object Method invocation public class ColoredRectangle { // instance variables to describe object attributes ... // paint(): display the rectangle in its window public void paint() { true ); window.setVisible( Graphics g = window.getGraphics(); g.setColor(color); g.fillRect(x, y, width, height); } ... } Typo in book: p. 149 claims paint() is static; it is not

  30. Quick survey • I understood the ColoredRectangle class • Very well • With some review, I’ll be good • Not really • Not at all

  31. Improving ColoredRectangle • Analysis • A ColoredRectangle object should • Be able to have any color • Be positionable anywhere within its window • Have no restrictions on its width and height • Accessible attributes • Updateable attributes

  32. Improving ColoredRectangle • Additional constructions and behaviors • Specific construction • Construct a rectangle representation using supplied values for its attributes • Accessors • Supply the values of the attributes • Individual methods for providing the width, height, x-coordinate position, y-coordinate position, color, or window of the associated rectangle • Mutators • Manage requests for changing attributes • Ensure objects always have sensible values • Individual methods for setting the width, height, x-coordinate position, y-coordinate position, color, or window of the associated rectangle to a given value

  33. Initial value of the formal parameter Object to be manipulated comes from the actual parameter is the one referenced by s public void setWidth ( int w ) { ... Changes to the formal parameter } do not affect the actual parameter A mutator method • Definition // setWidth(): width mutator public void setWidth(int w) { width = w; } • Usage ColoredRectangle s = new ColoredRectangle(); s.setWidth(80);

  34. The invocation sends a message to the ColoredRectangle referenced by s to modify its width attribute. public class ColoredRectangle { ... / / setWidth(): width mutator For this invocation of method public void int setWidth ( w) { setWidth(), w is initialized to 80 width = w; } Method setWidth() sets the instance variable width of its ColoredRectangle. ... } Method setWidth() is completed. Control is transferred back Mutator setWidth() evaluation new ColoredRectangle(); ColoredRectangle s = s .setWidth(80);

  35. Today’s demotivators

  36. Java parameter passing • The value is copied to the method • Any changes to the parameter are forgotten when the method returns

  37. 5 5 7 y x y Java parameter passing • Consider the following code: static void foobar (int y) { y = 7; } public static void main (String[] args) { int x = 5; foobar (x); System.out.println(x); } • What gets printed? formal parameter actual parameter

  38. y x “5" “7" Java parameter passing • Consider the following code: static void foobar (String y) { y = “7”; } public static void main (String[] args) { String x = “5”; foobar (x); System.out.println(x); } • What gets printed? formal parameter actual parameter

  39. y x width = 10 width = 0 Java parameter passing • Consider the following code: static void foobar (ColoredRectangle y) { y.setWidth (10); } public static void main (String[] args) { ColoredRectangle x = new ColoredRectangle(); foobar (x); System.out.println(x.getWidth()); } • What gets printed? formal parameter actual parameter

  40. y x width = 10 width = 0 width = 0 Java parameter passing • Consider the following code: static void foobar (ColoredRectangle y) { y = new ColoredRectangle(); y.setWidth (10); } public static void main (String[] args) { ColoredRectangle x = new ColoredRectangle(); foobar (x); System.out.println(y.getWidth()); } • What gets printed? formal parameter actual parameter

  41. Java parameter passing • The value of the actual parameter gets copied to the formal parameter • This is called pass-by-value • C/C++ is also pass-by-value • Other languages have other parameter passing types • Any changes to the formal parameter are forgotten when the method returns • However, if the parameter is a reference to an object, that object can be modified • Similar to how the object a final reference points to can be modified

  42. Quick survey • I felt I understand Java parameter passing • Very well • With some review, I’ll be good • Not really • Not at all

  43. Damage Control

  44. The main() method • Consider a class with many methods: public class WhereToStart { public static void foo (int x) { // ... } public static void bar () { // ... } public static void main (String[] args) { // ... } } • Where does Java start executing the program? • Always at the beginning of the main() method!

  45. Variable scoping • A variable is visible within the block it is declared in • Called the “scope” of the variable public class Scoping { int z public static void foo (int x) { // ... } public static void bar () { // ... } public static void main (String[] args) { int y; // ... } } This instance variable is visible anywhere in the Scoping class This parameter is visible only in the foo() method This local variable is visible until the end of the main() method

  46. More on classes vs. objects

  47. A new example: creating a Car class • What properties does a car have in the real world? • Color • Position (x,y) • Fuel in tank • We will implement these properties in our Car class public class Car { private Color color; private int xpos; private int ypos; private int fuel; //... }

  48. Car - color - xpos - fuel - ypos + … Car’s instance variables public class Car { private Color color; private int xpos; private int ypos; private int fuel; //... }

  49. Car - color = null - xpos = 0 - fuel = 0 - ypos = 0 + … Instance variables and attributes • Default initialization • If the variable is within a method, Java does NOT initialize it • If the variable is within a class, Java initializes it as follows: • Numeric instance variables initialized to 0 • Logical instance variables initialized to false • Object instance variables initialized to null

  50. Car behaviors or methods • What can a car do? And what can you do to a car? • Move it • Change it’s x and y positions • Change it’s color • Fill it up with fuel • For our computer simulation, what else do we want the Car class to do? • Create a new Car • Plot itself on the screen • Each of these behaviors will be written as a method