1 / 18

CSE 8A Lecture 18

CSE 8A Lecture 18. Reading for next class: 11.6-11.8 Today’s goals: More practice with designing classes Tracing code and creating memory models PSA 9 (classes) due next Monday (12/3) Individual (no partner) PSA8 Interview due tonight

jasia
Download Presentation

CSE 8A Lecture 18

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. CSE 8A Lecture 18 • Reading for next class: 11.6-11.8 • Today’s goals: • More practice with designing classes • Tracing code and creating memory models • PSA 9 (classes) due next Monday (12/3) • Individual (no partner) • PSA8 Interview due tonight • WIC event: Yahoo! Demos. Immediately after class in the Qualcomm conference room in Jacobs Hall.

  2. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) A Point class The Point of Java: objects and classes public class Point { private int x; private int y; public Point(intx_in, inty_in) { this.x = x_in; this.y = y_in; } public static void main( String[] args ) { Point r = new Point(10, 20); Point p = new Point(3, r.x); Point q = p; } Draw a picture of the memory model at the end of main (vote on next slide)

  3. x 10 x 10 r r y 20 y 20 A. C. x 3 x 3 p p y 10 y q x 3 q y 10 x 10 r D. None of these E. I don’t know y 20 B. x 3 p y 10 q

  4. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) A Point class The Point of Java: objects and classes public class Point { private int x; private int y; public Point(intx_in, inty_in) { this.x = x_in; this.y = y_in; } public static void main( String[] args ) { Point r = new Point(10, 20); Point p = new Point(3, r.x); Point q = p; r.x = 1; q.y = r.x + p.x; } What are the values of r, p, and q when this code completes? r p q A. (1, 20) (3, 1) (3, 4) B. (1, 20) (1, 1) (3, 2) C. (10, 20) (3, 13) (3, 13) D. (1, 20) (1, 13) (1, 13) E. None of these

  5. Class, Field, or Method? • A class is a type of thing • A field (instance variable) is a property, that might possibly have different values at different times • A method is an action that can be performed • So usually class and field names are nouns, method names are verbs…

  6. Another example of a class • Class: Species • Instance Variables: • name, a String • population on 7 continents, an array of 7 ints • growthRate, a double • Constructor: one that takes no arguments, and: • initializes name to “No Name Yet” • initializes population to all 0 • Initializes growthRate to 33.3

  7. Solo: (60 sec) • Discuss: (2 min) • Group: (20 sec) How many errors are there in this code (and what are they) • 2 • 3 • 4 • 5 • >=6 public class Species private String name; { public static void main(String[] args) { double[] population; double growthRate; } public Species() { String name = "No Name Yet"; double[] population = {0,0,0,0,0,0,0}; growthRate = 33.3; } }

  8. How many errors are there in this code (and what are they) public class Species { private String name; { public static void main(String[] args) { privateint[] population; private double growthRate; } public Species() { String name = "No Name Yet"; int[] population = {0,0,0,0,0,0,0}; growthRate = 33.3; } } All instance variables and methods have to go inside the class { } All instance variables have to go outside of method definitions Instance variables should be private Refer to instance variables in a constructor; do not redeclare them

  9. Visibility of Instance Variables • Class design rule of thumb: make all instance variables private • “private” means: visible only inside this class • So a private instance variable or instance method cannot be seen from outside the class • Making an instance variable private prevents incorrectly setting its value by malicious or careless users of the class

  10. Private instance variables in Species public class Species { ///////// fields //////////// private String name; private int[] population; private double growthRate; /////// constructors /////////// public Species() { String name = "No Name Yet"; population = {0,0,0,0,0,0,0}; growthRate = 33.3; } /////// methods //////////////// }

  11. Getter and Setter methods • Q: Instance variables correspond to properties of an object… if they are private and hidden inside, how can they interact with other objects? • A: Define public instance methods which give controlled, safe access to the private instance variables • If the method can change an instance variable, it is a “mutator” or “modifier” or “setter” method • If it only returns the value of an instance variable, it is an “accessor” or “getter” method

  12. Solo: (45 sec) • Discuss: (2 min) • Group: (20 sec) Which of following would you select for “getter” method signatures for Species class? public void getName(); public void getPopulation(); public void getGrowthRate(); public String getName(); public int[] getPopulation(); public double getGrowthRate(); public void getName(String newName); public void getPopulation(intnewPop); public void getGrowthRate(double newGrowthRate); private String getName(); private int[] getPopulation(); private double getGrowthRate();

  13. Solo: (45 sec) • Discuss: (2 min) • Group: (20 sec) Which of following would you select for “setter” method declarations for Species class? public void setName(); public void setPopulation(); public void setGrowthRate(); public String setName(); public int[] setPopulation(); public double setGrowthRate(); public void setName(String newName); public void setPopulation(int[] newPop); public void setGrowthRate(double newGrowthRate); public void setName(String newName); public booleansetPopulation(int[] newPop); public void setGrowthRate(double newGrowthRate);

  14. Solo: (30 sec) • Discuss: (2 min) • Group: (20 sec) Return type for Setters • A getter method should have a non-void return type • A setter can be designed in several ways: • void: just change the values of the instance variable(s), don’t return anything • boolean: return true if the setting was successful and false if not (for example if setting would be ‘illegal’) • The type of the value that is being changed: return the previous value

  15. Solo: (45 sec) • Discuss: (2 min) • Group: (20 sec) Overloading: Which are legal overloads? • 1 • 2 • 3 • 1 and 3 • 1 and 2 public Species() public Species(String newName); public booleansetGrowthRate(double gr) public void setGrowthRate(double gr) public void setPopulation( intnorthAmerica, intsouthAmerica, inteurope, intasia, intafrica, intaustralia, intantarctica) public void setPopulation(int[] a)

  16. Solo: (45 sec) • Discuss: (2 min) • Group: (20 sec) Terminology Check • Declaration • Instantiation • Initialization foo = new double[5]; for (inti = 0; i < foo.length; i++) { foo[i] = -11.5; } double [] foo;

  17. Draw a memory model for this code: public Species(String newName, int[] newPop, double newGR) { name = newName; population = new int[newPop.length]; for (inti=0; i< this.population.length;i++) population[i] = newPop[i]; growthRate = newGR; }

  18. TODO • Reading for next class: 11.6-11.8 • Interview for PSA8 (if you haven’t done it already) • Finish PSA9 • Do this one individually (no team programming)

More Related