Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/http://creativecommons.org/licenses/by-nc-

Similar presentations


Presentation on theme: "Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/http://creativecommons.org/licenses/by-nc-"— Presentation transcript:

1 Creative Commons Attribution Non-Commercial Share Alike License http://creativecommons.org/licenses/by-nc- sa/3.0/http://creativecommons.org/licenses/by-nc- sa/3.0/ Original Developer: Beth Simon, 2009 bsimon@cs.ucsd.edu

2 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

3 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

4 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

5 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

6 Class, Field, Method? 1.automobile 2.numberDoors 3.isExpired 4.door 5.bodyColor 6.getEfficiency 7.color 8.miles/gallon 9.licensePlate 10. setPlate 11.getRating

7 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)

8 How many errors are there in this code (and what are they) A.2 B.3 C.4 D.5 E.>=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; }

9 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 int getPopulation(); public double getGrowthRate(); public void getName(String newName); public void getPopulation(int newPop); public void getGrowthRate(double newGrowthRate); private void getName(); private void getPopulation(); private void getGrowthRate();

10 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 int setPopulation(); public double setGrowthRate(); public void setName(String newName); public void setPopulation(int newPop); public void setGrowthRate(double newGrowthRate);

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

12 Which is the BEST Setter Method for population instance variable? public void setPopulation(int newPop) { population = newPop; } public void setPopulation(int newPop) { if (newPop >= 0) population = newPop; } public boolean setPopulation(int newPop) { if (newPop >= 0) { population = newPop; return false; } return true; }

13 Better technique: Setters/Modifiers The best return type for many setters/modifiers is… A.void: a setter’s job is to change an instance variable value B.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) C.The type of the value that is being changed: a setter should return the value that was changed D.boolean: a setter should return true if the setting was successful and false if not

14 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; }

15 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; }

16 Our Species class example public class Species { ///////// fields //////////// private String name; private int[] population; private double growthRate; /////// constructors /////////// /////// methods //////////////// }

17 Draw what happens here… (hint pg 357) public Species(String newName, int[] newPop, double newGR) { name = newName; for (int i=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?

18 Overloading: Which are legal overloads? A.1 B.2 C.3 D.1 and 3 E.1 and 2 public Species() public Species(String newName); public boolean setGrowthRate(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) 1)Solo: (45 sec) 2)Discuss: (2 min) 3)Group: (20 sec)

19 Terminology Check 1.Declaration 2.Instantiation 3.Initialization double [] foo; for (int i = 0; i < foo.length; i++) { foo[i] = -11.5; } foo = new double[5];

20 Keeping our data secure (pg 356 is insecure) public Species(String newName, int[] newPop, double newGR) { name = newName; population = new int[newPop.length]; for (int i=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; }


Download ppt "Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/http://creativecommons.org/licenses/by-nc-"

Similar presentations


Ads by Google