Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Similar presentations


Presentation on theme: "© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any."— Presentation transcript:

1 © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any other purpose without the written permission of Walter Savitch. wsavitch@ucsd.edu

2 CSE 11 HW 3 turnin deadline: Tues Jan 28 HW 4 turnin deadline: Thurs Jan 30 Do not need to turnin the HW if you take an interview before the turnin deadline

3 Midterm Warning In lecture Tuesday February 4. Quiz grades are not so great. Pick up graded quizzes from TA. Do Self-Test Exercises. Sample midterm on web page and in public. Can bring notes on one index card.

4 You Must Have an Approved Picture ID at the Midterm or You Get Zero Only the following will do A UCSD Picture ID A Current Picture Drivers License A Passport Other ID approve (in person not via email) by instructor at least 48 hours before the midterm

5 Vectors Similar to arrays, but Can change size automatically Have lots of nice built-in methods Do not have the a[i] notation

6 Using Vectors Vector v2 = new Vector(30); Vector v = new Vector( ); v.addElement("Zero"); v.addElement("One"); v.addElement("Two"); Must add elements in order, position 0, then 1, then 2, etc. Elements of a vector must each be an object, i.e., an object of a class or an array object.

7 Using Vectors Elements of a vector must each be an object, i.e., an object of a class or an array object. v.addElement(“Hello");//OK v.addElement(42);//Illegal Can mix different types of objects in one vector (but seldom do that).

8 Using Vectors Need import java.util.*;

9 Vectors are Like Arrays a is an array; v is a vector: a[3] = “Here”; v.setElementAt("Here", 3); String temp = a[3]; String temp = (String)v.elementAt(index);

10 Details v.setElementAt("Here", 3); Must already have some element at positions 0, 1, 2, and 3. String temp = (String)v.elementAt(index);

11 Details String temp = (String)v.elementAt(index); v.elementAt(index) knows it is an object, but does not know what class it belongs to. Need a type cast,

12 The size Method for (index = 0; index < v.size( ); index++) System.out.println(v.elementAt(index)); v.size( ) is the number of elements currently in v.

13 size versus capacity v.size() is the number of elements currently in v. v.capacity() is the number of elements for which v currently has memory allocated, but if v needs more memory it will be allocated automatically.

14 Built in Searching int i = v. indexOf(“Hello”); System.out.println(v.elementAt(i)); If “Hello” is in the vector v, the output will be Hello You still need to do binary search for HW 4, not just use this.

15 Making a copy of a Vector v1 and v2 vectors v1 = v2; //Just makes v1 and v2 two names for the same vector. v1 = (Vector)v2.clone(); //makes v1 a separate copy of v2. Note type cast (Vector)

16 Memory Management with Vectors Automatically increase capacity of vector when needed. No automatic shrinking of capacity. But, you can do some manual memory management with vectors. This will not effect what your programs do, but can effect the efficiency of your programs.

17 Memory Management Initial capacity can be argument to constructor: Vector v = new Vector(100); v.ensureCapacity(150); v.trimToSize( )

18 The Type Object Every object of every class is of type Object. The base type of a vector is Object. So, v.elementAt(index) returns a value of type Object. So, need type cast String temp = (String)v.elementAt(index);

19 The Type Object v.clone() returns a value of type Object, so you need a type cast: Vector v2 = (Vector) v.clone(): More about the type Object when we cover inheritance Chapter 7

20 Inheritance Make new class (derived class) from an old class (base class). Derived class automatically has (inherits) all the methods and instance variables of the base class. Derived class normally also adds instance variables and/or methods.

21 A (Base) Class public class Person { private String name; public Person( ) { name = "No name yet.";} public Person(String initialName) { name = initialName; }

22 public void setName(String newName) { name = newName;} public String getName( ) { return name;} public void writeOutput( ) { System.out.println("Name: " + name);} public boolean sameName(Person otherPerson) { return (this.name.equalsIgnoreCase(otherPerson.name);} }

23 A Derived Class public class Student extends Person { private int studentNumber; public Student( ) { super( ); //Invokes base class constructor //to initialize namme instance variable studentNumber = 0 } public Student(String initialName, int initialStudentNumber) { super(initialName); studentNumber = initialStudentNumber; }

24 A Derived Class Continued public void reset(String newName, int newStudentNumber) { setName(newName); studentNumber = newStudentNumber; } public int getStudentNumber( ) { return studentNumber; } public void setStudentNumber(int newStudentNumber) { studentNumber = newStudentNumber; }

25 A Derived Class Continued public void writeOutput( ) { System.out.println("Name: " + getName( ) ); System.out.println("Student Number: " + studentNumber); } public boolean equals(Student otherStudent) { return (this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber)); }

26 Private instance variables in the base class Are inherited by the derived class. Cannot be accessed by name in the definition of the derived class. Must be handled using public accessor and mutator methods inherited from the base class.


Download ppt "© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any."

Similar presentations


Ads by Google