NonCommercial Share Alike License httpcreativecommonsorglicensesbyncsa30 Original Developer Beth Simon 2009 bsimoncsucsdedu CSE8A Lecture 25 Read next class Reread Chapter 11 ID: 584134
Download Presentation The PPT/PDF document "Creative Commons Attribution" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Creative Commons Attribution
Non-Commercial Share Alike License
http://creativecommons.org/licenses/by-nc-sa/3.0/
Original Developer: Beth Simon, 2009bsimon@cs.ucsd.eduSlide2
CSE8A Lecture 25
Read next class: Re-read Chapter 11
No class, No lab WedPSA8 due Tues 11:59 (individual – Sound collage)Week 10PSA9 due WED 11:59pm (pair – Make a class for Participant in medical study)Lecture all week, Exam Wed of finals week 3-6pmYes, there’s a quiz Friday Week 10Exam: Wed 3-6pmIndividual and GroupSlide3
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 fieldsSlide4
Chapter 11: Creating Classes
Classes (and class definitions) are the primary programming support for
object-oriented programming languages.Classes are comprised ofData (we need to know about object)Fields or instance variablesConstructors (to create new objects)Methods (actions we need to be able to perform on objects)Constructors are “special” methodsSlide5
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.javaLet’s look there.And in Pixel.javaSlide6
Class, Field, Method?
automobile
numberDoorsisExpireddoorbodyColorgetEfficiencycolormiles/gallonlicensePlate setPlategetRatingSlide7
A Class example for “class”
Class: Species
Fields/Instance Variables:namepopulationgrowthRateMethods:1: Constructors (2 versions)2: Getters (get values in instance variables)3: Setters/Mutators (change values in instance variables)Slide8
How many errors are there in this
code (and what are they)
2345>=6public 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; }}Slide9
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
();Slide10
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);Slide11
“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 teamClasses provide encapsulation and data protectionPut safeguards on how the data can be changedWhat is legal and illegalSlide12
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;
}Slide13
Better technique:
Setters/Modifiers
The best return type for many setters/modifiers is…void: a setter’s job is to change an instance variable valuevoid: 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 changedboolean: a setter should return true if the setting was successful and false if notSlide14
A MODIFIED Class example for “class”
Class: Species
Fields/Instance Variables:namePopulation on 5 continentsgrowthRatepublic 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; }}Slide15
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;}Slide16
Our Species class example
public class Species
{ ///////// fields //////////// private String name; private int[] population; private double growthRate; /////// constructors /////////// /////// methods ////////////////}Slide17
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?Slide18
Overloading:
Which are legal overloads?
1231 and 31 and 2public 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)
Solo:
(45 sec)
Discuss:
(2 min
)
Group:
(20
sec)Slide19
Terminology Check
Declaration
InstantiationInitializationdouble [] foo;for (int i = 0; i < foo.length;
i++){ foo[i] = -11.5; }foo = new double[5];Slide20
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
;
}