© 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.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Introduction To Scientific Programming Chapter 7 – Inheritance or (More on Classes)
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Constructors. Defining Constructors  A constructor is a special kind of method that is designed to perform initializations, such as giving values to.
CSE 11 HW 2, 3 and 4 Posted in public and on web Deadlines HW 2 turnin: Wed Jan 22 interview: Saturday January 25.class files in public directory for HW.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Vectors l Linked Data Structures Dynamic Data Structures.
ArrayList, Multidimensional Arrays
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
© 2013 Ken Howard, Southern Methodist University Object-Oriented Programming: Inheritance CSE 1341.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Chapter 11. Multidimensional Arrays and Vectors Multidimensional Arrays Vectors Computer Programming with JAVA.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
UML (U NIFIED M ODELING LANGUAGE ) Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Unit 2 Review (Part 1) Arrays, Inheritance, and the Ethical Use of Computers.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
COMP Inheritance Basics Yi Hong June 09, 2015.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Multidimensional Arrays l Vectors Multidimensional Arrays.
Written by: Dr. JJ Shepherd
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Vector program patterns Vectors contain many elements, so loops are common. Counted processing // Print the first 3 elements. for (int i = 0; i < 3; i++)
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CSC 113 Tutorial QUIZ I.
Inheritance Basics Programming with Inheritance
Inheritance I Class Reuse with Inheritance
Extending Classes.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Inheritance, Polymorphism, and Interfaces. Oh My
Operator overloading Dr. Bhargavi Goswami
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Jeff West - Quiz Section 9
Midterm Exam Information
Programming with inheritance Based on slides by Alyssa Harding
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Presentation transcript:

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

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

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.

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 ) by instructor at least 48 hours before the midterm

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

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.

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).

Using Vectors Need import java.util.*;

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

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

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,

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.

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.

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.

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)

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.

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

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

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

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.

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

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);} }

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

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

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)); }

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.