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 28 Exam: Wed 3-6pm, Center 119 –Individual and Group –Survey Linked from Moodle under sample exam (up by Friday 5pm) Study CLICKER QUESTIONS (and quizzes, labs, PSAs, practice writing code) –Review Session: Monday 3-4pm Filming Today –If you or someone in your group doesn’t want to be “a face in the crowd”, move into taped off area –If you answer question in class – we won’t be using your voice SO DON’T BE SHY!!!!

3 By the end of today’s class you should be able to… LG56: Create arrays of objects of any length or type and be able to use good software design for using arrays as fields LG57: Apply your Picture and Sound knowledge of arrays to general usage of arrays LG58: Evaluate expressions reflecting the fact that array variables in Java refer to a location in memory where that data is stored. LG59: Describe necessary code structures required to perform specific tasks on arrays.

4 Our NEW and IMPROVED Species class example public class Species { ///////// fields //////////// private String name; private int[] population; private String[] location; private double growthRate; /////// constructors /////////// /////// methods //////////////// }

5 public Species(String newName, int[] newPop, String[] location, double newGR) { name = newName; growthRate = newGR; if (newPop.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[newPop.length]; location = new String[location.length]; for (int i=0; i < location.length; i++) { this.location[i] = location[i]; this.population[i] = newPop[i]; } Review: What is this code doing? A)Making sure newPop and location are legal B)Making sure newPop and location are of the same length C)Setting population and location to null D)Error, return without variable

6 Designing a class securely… Security of instance variables fields –Data values in a specific field may need to be checked for limited legal value ranges Examples? –Data values BETWEEN instance variables might be limited/constrained Example (date) –Arrays may need to be checked for length / correlation with each other

7 A possible setter method public boolean changePopulation(int pop, String loc) { if (pop < 0) return false; > } location[0] = pop;location[location.length] = pop; for (int i=0; i<loc.length; i++) { if (location[i].equals(loc)) population[i] = pop; } for (int i=0; i<pop.length; i++) { if (population[i] == pop) location[i] = pop; }

8 Arrays in Java Not just for Pixels anymore…

9 How many of the following statements are true? A particular “instance” of an array can “hold” objects of different types: An array of Species objects of length 10 MUST have 10 references to Species objects stored in it An array of Species objects of length 10 has SOME value (possibly null) in each of its cells An array instantiated with 10 items can be changed to hold 11 items if needed

10 Which of the following evaluates to true about the code snippet below? int[] foo = {33,22,11}; int[] bar = {33,22,11}; foo == bar; foo.equals(bar); bar.equals(foo); foo != bar; It depends on how memory is allocated…

11 Question (that we don’t have to answer) : How many people in the class scored above the average grade for the class? –Hint it’s not always N/2

12 How many loops would be needed to count the number of array elements above the average of the values in the array? (assuming you don’t already know the average) A.1 loop B.2 loops, one after the other C.2 nested loops D.3 loops, one after the other


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

Similar presentations


Ads by Google