20
Creative Commons Attribution Non-Commercial Share Alike License http://creativecommons.org/license s/by-nc-sa/3.0/ Original Developer: Beth Simon, 2009 [email protected]

Creative Commons Attribution Non-Commercial Share Alike License

  • Upload
    moana

  • View
    49

  • Download
    0

Embed Size (px)

DESCRIPTION

Creative Commons Attribution Non-Commercial Share Alike License. http://creativecommons.org/licenses/by-nc-sa/3.0/ Original Developer: Beth Simon, 2009 [email protected]. CSE8A Lecture 25. Read next class: Re-read Chapter 11 No class, No lab Wed - PowerPoint PPT Presentation

Citation preview

Page 1: Creative Commons Attribution Non-Commercial Share Alike License

Creative Commons AttributionNon-Commercial Share Alike

License• http://creativecommons.org/licenses/by-n

c-sa/3.0/• Original Developer: Beth Simon, 2009

[email protected]

Page 2: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 3: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 4: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 5: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 6: Creative Commons Attribution Non-Commercial Share Alike License

Class, Field, Method?1. automobile2. numberDoors3. isExpired4. door5. bodyColor6. getEfficiency7. color8. miles/gallon9. licensePlate10. setPlate11.getRating

Page 7: Creative Commons Attribution Non-Commercial Share Alike License

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)

Page 8: Creative Commons Attribution Non-Commercial Share Alike License

How many errors are there in this code (and what are they)

A. 2B. 3C. 4D. 5E. >=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; }

}

Page 9: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 10: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 11: Creative Commons Attribution Non-Commercial Share Alike License

“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

Page 12: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 13: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 14: Creative Commons Attribution Non-Commercial Share Alike License

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

Page 15: Creative Commons Attribution Non-Commercial Share Alike License

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;

}

Page 16: Creative Commons Attribution Non-Commercial Share Alike License

Our Species class examplepublic class Species{

///////// fields //////////// private String name; private int[] population; private double growthRate;

/////// constructors ///////////

/////// methods ////////////////}

Page 17: Creative Commons Attribution Non-Commercial Share Alike License

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?

Page 18: Creative Commons Attribution Non-Commercial Share Alike License

Overloading: Which are legal overloads?

A. 1B. 2C. 3D. 1 and 3E. 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 dint e)

public void setPopultion(int[] a)

1) Solo: (45 sec)2) Discuss: (2 min)3) Group: (20 sec)

Page 19: Creative Commons Attribution Non-Commercial Share Alike License

Terminology Check1. Declaration2. Instantiation3. Initialization

double [] foo;

for (int i = 0; i < foo.length; i++){ foo[i] = -11.5; }

foo = new double[5];

Page 20: Creative Commons Attribution Non-Commercial Share Alike License

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