240 likes | 416 Views
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.
E N D
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 class public void mystery() { SoundSample[] original = this.getSamples(); for (int index=1; index<original.length; index++) { original[index].setValue(original[index-1].getValue()+ original[index].getValue()); } } original
Problem 2 • SoundSample array before: • SoundSample array after call to mySound.bleep(3, 7): public void bleep(int start, int end){ for ( inti = start; i <= end; i += 2 ) { this.setSampleValueAt(i, -7500); this.setSampleValueAt(i+1, 7500); } if ( (end – start + 1) % 2 == 1 ) { this.setSampleValueAt(i, -7500) } } Does this work? Yes, always Yes, but only sometimes No, never
Problem 2 • SoundSample array before: • SoundSample array after call to mySound.bleep(3, 7): public void bleep(int start, int end){ for ( inti = start; i <= end; i++ ) { if ( i % 2 == 0 ): this.setSampleValueAt( -7500 ); else: this.setSampleValueAt( 7500 ); } } Does this work? Yes, always Yes, but onlysometimes No, never
Problem 2 • SoundSample array before: • SoundSample array after call to mySound.bleep(3, 7): public void bleep(int start, int end){ for ( inti = start; i <= end; i += 2 ) { this.setSampleValueAt( -7500 ); } • for ( inti = start+1; i <= end; i += 2 ) • { • this.setSampleValueAt( 7500 ); • } } Does this work? Yes, always Yes, but onlysometimes No, never
public class Point { private int x; private int y; public Point(intx_in, inty_in) { this.x = x_in; this.y = y_in; } public intgetX() { return this.x; } public void setX( intx_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 when this code completes? rq A. (12, 52) (12, 52) B. (3, 12) (12, 52) C. (15, 52) (15, 52) D. (3, 12) (15, 52) E. None of these What are the values of r and q at the end of this code (DRAW THE MEMORY MODEL!!)
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 Species public 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
Solo: (45 sec) • Discuss: (2 min) • Group: (20 sec) Which of following would you select for “getter” method signatures 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(intnewPop); public void getGrowthRate(double newGrowthRate); private String getName(); private int[] getPopulation(); private double getGrowthRate();
Solo: (45 sec) • Discuss: (2 min) • Group: (20 sec) Which of following would you select for “setter” method declarations 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); public void setName(String newName); public booleansetPopulation(int[] newPop); public void setGrowthRate(double newGrowthRate);
Solo: (30 sec) • Discuss: (2 min) • Group: (20 sec) 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
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( intnorthAmerica, intsouthAmerica, inteurope, intasia, intafrica, intaustralia, intantarctica) public void setPopulation(int[] a)
Solo: (45 sec) • Discuss: (2 min) • Group: (20 sec) Terminology Check • Declaration • Instantiation • Initialization foo = new double[5]; for (inti = 0; i < foo.length; i++) { foo[i] = -11.5; } double [] foo;
Draw a memory model for this code: 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; }
TheSpecies class, review public 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 (inti=0; i< this.population.length;i++) population[i] = pop[i]; growthRate = gr; } /////// methods ////////////////
TheSpecies class, continued /////// methods //////////////// public void setPopulation(int pop, int index) { population[index] = pop; } public intgetPopulation(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] Be sure to call the String[] location Draw them – samples.
Solo: (30 sec) • Discuss: (2 min) • Group: (20 sec) 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 (inti=0; i< population.length;i++) population[i] = pop[i]; <<INSERT CODE HERE>> growthRate = gr; } location = new String[location.length]; for (inti=0; i < location.length; i++) location[i] = location[i]; location = location; this.location = location; location = new String[location.length]; for (inti=0; i < location.length; i++) this.location[i] = location[i];
Solo: (30 sec) • Discuss: (2 min) • Group: (20 sec) 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 (inti=0; i< population.length;i++) population[i] = pop[i]; this.location = new String[location.length]; for (inti=0; i < location.length; i++) this.location[i] = location[i]; growthRate = gr; } Still has some “bad software design issues…”
Solo: (30 sec) • Discuss: (2 min) • Group: (20 sec) What is this code doing? 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 (inti=0; i < location.length; i++) { this.location[i] = location[i]; this.population[i] = pop[i]; } } Making sure pop is an array of ints, location is anarray of Strings Making sure pop and location are of the same length Setting population and location to null This will not compile(return without value)
A possible setter method public booleansetPopulation(intpop, String loc) { if (pop < 0) return false; for (inti=0; i<loc.length; i++) { if (location[i].equals(loc)) { population[i] = pop; return true; } } return false; }
TODO • Start studying for the final!