200 likes | 359 Views
Creative Commons Attribution Non-Commercial Share Alike License. http://creativecommons.org/licenses/by-nc-sa/3.0/ Original Developer: Beth Simon, 2009 bsimon@cs.ucsd.edu. CSE8A Lecture 25. Read next class: Re-read Chapter 11 No class, No lab Wed
E N D
Creative Commons AttributionNon-Commercial Share Alike License • http://creativecommons.org/licenses/by-nc-sa/3.0/ • Original Developer: Beth Simon, 2009bsimon@cs.ucsd.edu
CSE8A Lecture 25 • Read next class: Re-read Chapter 11 • No class, No lab Wed • PSA8 due Tues 11:59 (individual – Sound collage) • Week 10 • PSA9 due WED 11:59pm (pair – Make a class for Participant in medical study) • Lecture all week, Exam Wed of finals week 3-6pm • Yes, there’s a quiz Friday Week 10 • Exam: Wed 3-6pm • Individual and Group
By the end of today’s class you should be able to… • LG50: Describe how a Java class is made up of instance variables or fields, constructors, and methods and brainstorm a given class design. • LG51: Identify common errors made by novices in defining their own classes. • LG53: Identify common structure of “getter” and “setter” methods. • LG54: Be able to draw the memory model of an object (at a more detailed level than before) – based on what happens in a constructor. • LG55: Identify legal and illegal instances of method overloading, so you can know what “variations” on methods you can write. • LG56: Create arrays of objects of any length or type and be able to use good software design for using arrays as fields
Chapter 11: Creating Classes • Classes (and class definitions) are the primary programming support for object-oriented programming languages. • Classes are comprised of • Data (we need to know about object) • Fields or instance variables • Constructors (to create new objects) • Methods (actions we need to be able to perform on objects) • Constructors are “special” methods
Let’s look around in Picture.java • Wait, there’s nothing – because we didn’t want to scare you in Week 2 • So we hid things away in SimplePicture.java • Let’s look there. • And in Pixel.java
Class, Field, Method? • automobile • numberDoors • isExpired • door • bodyColor • getEfficiency • color • miles/gallon • licensePlate • setPlate • getRating
A Class example for “class” • Class: Species • Fields/Instance Variables: • name • population • growthRate • Methods: • 1: Constructors (2 versions) • 2: Getters (get values in instance variables) • 3: Setters/Mutators (change values in instance variables)
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; growthRate = 33.3; } }
Which of following would you select for a “getter” method header for Species class? public void getName(); public void getPopulation(); public void getGrowthRate(); public String getName(); public intgetPopulation(); public double getGrowthRate(); public void getName(String newName); public void getPopulation(intnewPop); public void getGrowthRate(double newGrowthRate); private void getName(); private void getPopulation(); private void getGrowthRate();
Which of following would you select for a “setter” method header for Species class? public void setName(); public void setPopulation(); public void setGrowthRate(); public String setName(); public intsetPopulation(); public double setGrowthRate(); public void setName(String newName); public void setPopulation(intnewPop); public void setGrowthRate(double newGrowthRate);
“Best Practice” Getters/Setters • A reason we make all out fields/instance variables private is we want to protect our code from malicious or “silly” users • Including other “users” on our development team • Classes provide encapsulation and data protection • Put safeguards on how the data can be changed • What is legal and illegal
Which is the BEST Setter Method forpopulation instance variable? public void setPopulation(intnewPop) { population = newPop; } public void setPopulation(intnewPop) { if (newPop >= 0) population = newPop; } public booleansetPopulation(intnewPop) { if (newPop >= 0) { population = newPop; return false; } return true; }
Better technique: Setters/Modifiers • The best return type for many setters/modifiers is… • void:a setter’s job is to change an instance variable value • void: a setter’s job is to change an instance variable value as long as it is legal (if it’s not it will print out an error message) • The type of the value that is being changed: a setter should return the value that was changed • boolean: a setter should return true if the setting was successful and false if not
A MODIFIED Class example for “class” • Class: Species • Fields/Instance Variables: • name • Population on 5 continents • growthRate public class Species { private String name; private double[] population; private double growthRate; public Species() { String name = “No Name Yet”; double[] population = {0,0,0,0,0}; growthRate = 33.3; } }
Constructors: Under the hood • Constructors “automatically execute” some “hidden” code for you. public Species() { String name = “No Name Yet”; double[] population = {0,0,0,0,0}; growthRate = 33.3; }
Our Species class example public class Species { ///////// fields //////////// private String name; private int[] population; private double growthRate; /////// constructors /////////// /////// methods //////////////// }
Draw what happens here… (hint pg 357) public Species(String newName, int[] newPop, double newGR) { name = newName; for (inti=0; i< this.population.length;i++) population[i] = newPop[i]; growthRate = newGR; } DO YOU UNDERSTAND? There’s a missing line that causes an error – what is it?
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( int a, int b, int c, int d int e) public void setPopultion(int[] a)
Terminology Check • Declaration • Instantiation • Initialization foo = new double[5]; for (inti = 0; i < foo.length; i++) { foo[i] = -11.5; } double [] foo;
Keeping our data secure (pg 356 is insecure) 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; } public Species(String newName, int[] newPop, double newGR) { name = newName; //BAD DESIGN… INSECURE population = newPop; growthRate = newGR; }