CSE 8A Lecture 19

Preview:

DESCRIPTION

CSE 8A Lecture 19. Reading for next class: Review! Today’s goals: More practice with designing classes Tracing code and creating memory models Optional (individual) PSA 10 Optional: Do it for personal satisfaction! CAPES: In lab this week Why we need your feedback. Exam 4. - PowerPoint PPT Presentation

Citation preview

CSE 8A Lecture 19• Reading for next class: Review!

• Today’s goals:– More practice with designing classes– Tracing code and creating memory models

• Optional (individual) PSA 10– Optional: Do it for personal satisfaction!

• CAPES: In lab this week– Why we need your feedback

Exam 4• Out of 11 points

– Issue with points on Q2: I announced it was out of 6 points, the other instructors announced 7

– We solved this issue by making it out of 7, but the rubric gives everyone 1 free point (i.e., if you wrote anything at all, even if it was completely wrong, you got a point)

• Stats:– Mean: 9.3/11 (~85%); Median: 10/11– 131 perfect scores (11 out of 11)– Nice job!

• If you scored below 55% you are in danger of failing the course. Come talk to me.

Problem 1

// In the Sound classpublic void mystery() { SoundSample[] original = this.getSamples(); for (int index=1; index<original.length; index++) { original[index].setValue(original[index-1].getValue()+ original[index].getValue()); }}

10 40 0 30 20 10 50 0 60original

Problem 2

500 500 500 500 500 500 500 500 500 500

500 500 500 -7500 7500 -7500 7500 -7500 500 500

public void bleep(int start, int end){ for ( int i = start; i <= end; i += 2 ) { this.setSampleValueAt(i, -7500); this.setSampleValueAt(i+1, 7500); } if ( (end – start + 1) % 2 == 1 ) { this.setSampleValueAt(i, -7500) }}

SoundSample array before:

SoundSample array after call to mySound.bleep(3, 7):

Does this work?A. Yes, alwaysB. Yes, but only

sometimesC. No, never

Problem 2

500 500 500 500 500 500 500 500 500 500

500 500 500 -7500 7500 -7500 7500 -7500 500 500

public void bleep(int start, int end){ for ( int i = start; i <= end; i++ ) { if ( i % 2 == 0 ): this.setSampleValueAt( -7500 ); else: this.setSampleValueAt( 7500 ); } }

SoundSample array before:

SoundSample array after call to mySound.bleep(3, 7):

Does this work?A. Yes, alwaysB. Yes, but only

sometimesC. No, never

Problem 2

500 500 500 500 500 500 500 500 500 500

500 500 500 -7500 7500 -7500 7500 -7500 500 500

public void bleep(int start, int end){ for ( int i = start; i <= end; i += 2 ) { this.setSampleValueAt( -7500 ); } for ( int i = start+1; i <= end; i += 2 ) { this.setSampleValueAt( 7500 ); } }

SoundSample array before:

SoundSample array after call to mySound.bleep(3, 7):

Does this work?A. Yes, alwaysB. Yes, but only

sometimesC. No, never

public class Point{ private int x; private int y;

public Point(int x_in, int y_in) { this.x = x_in; this.y = y_in; }

public int getX() { return this.x; }

public void setX( int x_in ) { this.x = x_in; }

public static void main( String[] args ) { Point r = new Point(12, 52); Point q = r; Point r = new Point(3, r.getX()); q.setX( q.getX() + r.getX() ); }

What are the values of r and q at the end of this code (DRAW THE MEMORY MODEL!!)

What are the values of r, and q when this code completes? r qA. (12, 52) (12, 52)B. (3, 12) (12, 52) C. (15, 52) (15, 52) D. (3, 12) (15, 52) E. None of these

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

Private instance variables in Speciespublic 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 ////////////////}

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

Which of following would you select for “getter” method signatures for Species class?

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

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 String getName();private int[] getPopulation();private double getGrowthRate();

Which of following would you select for “setter” method declarations for Species class?

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

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 boolean setPopulation(int[] newPop);public void setGrowthRate(double newGrowthRate);

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

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

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 northAmerica, int southAmerica, int europe, int asia, int africa, int australia, int antarctica)public void setPopulation(int[] a)

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

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

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

Draw a memory model for this code: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;}

The Species class, reviewpublic class Species{

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

/////// constructors /////////// public Species(String name, int[] pop, double gr){ this.name = name; population = new int[pop.length]; for (int i=0; i< this.population.length;i++) population[i] = pop[i]; growthRate = gr;}

/////// methods ////////////////

The Species class, continued

/////// methods ////////////////

public void setPopulation(int pop, int index) {population[index] = pop;

}

public int getPopulation(int index) {return population[index];

}

}

A redesign of the Species class• This idea that the population array is just 7 entries, one per

location is a bit “obscure”.– What are the names of the locations? Which entry is for North America?

Which for Europe?

• Another, better approach: “parallel arrays”

• Declare and create two arrays of the same length– One for location names: String[] location;– One for population numbers: int[] population;– And write code so that for every index I, population[I] is the population

in the location with name location[I]

Write a constructor for the new Species class

public Species(String name, int[] pop, String[] location, double gr)

{ this.name = name; population = new int[pop.length]; for (int i=0; i< population.length;i++) population[i] = pop[i];

<<INSERT CODE HERE>> growthRate = gr;}

location = location;

this.location = location;

location = new String[location.length];for (int i=0; i < location.length; i++) location[i] = location[i];

location = new String[location.length];for (int i=0; i < location.length; i++) this.location[i] = location[i];

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

Write a constructor for the new Species class

Still has some “bad software design issues…”

public Species(String name, int[] pop, String[] location, double gr)

{ this.name = name; population = new int[pop.length]; for (int i=0; i< population.length;i++) population[i] = pop[i];

this.location = new String[location.length]; for (int i=0; i < location.length; i++) this.location[i] = location[i];

growthRate = gr;}

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

public Species(String name, int[] pop, String[] location, double gr)

{ name = newName; growthRate = gr; if (pop.length != location.length) { System.out.println(“Error constructing Species. “+ “Population array and location array must be same length.”); population = null; location = null; return; } population = new int[pop.length]; this.location = new String[location.length]; for (int i=0; i < location.length; i++) { this.location[i] = location[i]; this.population[i] = pop[i]; } }

What is this code doing?1) Solo: (30 sec)2) Discuss: (2 min)3) Group: (20 sec)

A) Making sure pop is an array of ints, location is anarray of Strings

B) Making sure pop and location are of the same length

C) Setting population and location to null

D) This will not compile(return without value)

A possible setter methodpublic boolean setPopulation(int pop, String loc){ if (pop < 0) return false; for (int i=0; i<loc.length; i++) { if (location[i].equals(loc)) { population[i] = pop; return true; } } return false;}

TODO

• Start studying for the final!