APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.

Slides:



Advertisements
Similar presentations
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
Chapter 10 Classes Continued
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
The AP Java Subset Topics. A Topics Primitive Types int double boolean.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go with Computing Concepts With Java Essentials, Fran Trees and Cay Horstmann

Formal Parameters Specified in the definition of a method Represent information passed to the method when it is called Can be used to determine the behavior of the method (like a blue print) Can be used in calculations carried out by the method

Actual Parameters Also called arguments The values given as parameters when a method is called May be literals (number, string literal) May be a variable in the current context May be an expression that matches the type specified in the declaration Expression may be a newly created object using new (…)

Implicit parameter to a class The word this is the name for the implicit parameter in a class. Eg. this.instancefield or this.method()

Parameter Passing Remember that primitive variables contain their value. Remember that object variables are references. All method parameters are passed by value. Primitive method parameters can NOT cause changes outside the method. Object variable parameters can only be changed via modifiers (mutators).

Scope Variables declared within a method are only visible within that method Formal parameters are only visible within that method For loop counters are only available within the for loop. Instance variables are visible within a class. Also called instance fields.

Characteristics of an object Or what to put in the private section of the class. Instance variables specify the attributes of an object. State of an object is the set of attributes that determine its current configuration, that may change. Modifier methods change state – affect the future behavior of the class.

Methods The signature includes the method name and formal parameter list. Methods may be overloaded meaning they have the same name with different formal parameters. Methods that one class inherits from another may be overridden meaning the signatures are the same and the child class method supercedes the parent.

Static Methods Do not operate on objects Have the keyword static before the method signature Are called using the class name rather than an object name An example of a class with static methods is the Math class, java.util.Math

Class Variables Declared with the word static Initialized once when the first object of that class type is instantiated. Example: given in class

Boolean Expressions Short circuit evaluation of && Compare Strings using equals! Compare primitives with ==

Class Design: Cohesion and Coupling A class should represent one concept. Cohesion refers to how well all the class responsibilities relate to the concept the class represents. High cohesion makes a class reusable. Coupling refers to how much a class depends on other classes (collaborators). In OOD the goal is to have high cohesion and low coupling.

Immutable classes A class is immutable if it has no modifiers. String is an example of an immutable class.

Interfaces Establish a type, just as a class does. Specifies methods, no implementation No data fields (instance or class variables) Can have constants (public static final) No constructors Can not be instantiated A class must implement an interface and all methods in the interface. Classes may implement multiple interfaces. Methods are public by default

Interface example java.lang.Comparable Single method of Comparable interface int compareTo(Object other) < 0 if this less than other > 0 if this greater than other = 0 if this equals other The String class implements Comparable in order to allow lexicographical ordering of Strings. It might make sense for our Student class to implement Comparable.

What’s this Object class? All Java classes inherit from class Object. You are responsible for knowing when to overload some Object methods String toString () Boolean equals (Object other) ToString is used by System.out.print to determine what you are shown when you display an object. You can always write your own equals in order to compare your own classes in the manner you wish.

An example of compareTo for a Coin class Coin.java

More on casts A simple example is putting a double into an int. double x = 5.2; int y = (int)x; // casts x to an int Example of casting the Object type to the class type in the compareTo example. You must cast to convert an Interface type to a class type. CastExample.javaCastExample.java

Polymorphism Also called dynamic binding Since an interface name can refer to any class that realizes that interface each of which have their own version of the interface methods, the JVM figures out at run time what the object type is and runs the appropriate method.

Inheritance with extends Subclasses inherit instance fields and constants from their parent. Inherited fields that are private are NOT accessible to the subclass. A subclass does NOT have to override all methods in the parent class. Subclasses should NOT ever override instance fields. Subclasses may add additional methods and instance fields.

Constructors in a subclass A call to the super class constructor must be the first line in the constructor. After saying super (…), any additional instance fields and state variables should be initialized.

Converting An object of a superclass can be assigned to an object of its subclass with proper casting. An object of a subclass can be directly assigned to an object of its superclass. Whether the superclass method or subclass method is run is determined at runtime by the type of the object NOT the object reference. Ex. Binding.javaBinding.java

Is-A vs. Has-A Consider the Athlete and SkiJumper example. A subclass is-a superclass A skijumper is-a athlete Has-A implies a uses relationship (as in coupling). The SkiJumper has-a name (String).

1D Arrays An array is a fixed length sequence of values. May be primitives or objects. Must be sized first and can not be resized later. Initialized item by item using for-loop Initialized and sized using initializer list int [] array2 = {3, 5, 9, 10}; Accessed from 0 to length – 1 with [] Length is a public field, note that no parenthesis are used. int length = array2.length;

1D Arrays The array variable is a reference. Assigning two arrays to each other does not make a copy of the array. If an array is of objects, then each element of the array is a reference to an object of that type. Assigning two elements to each other does not make a copy of the elements.

2D arrays A row,column table constructed by making an array of arrays. int [] [] twoD = new int [3][5]; Like 1D arrays must be sized, then each element created. Can contain objects or primitives. Can be initialized with an initializer list Int [][] array3 = { {2,3,4}, {4,5,6}}; Accessed using [][]: array3[0][1] accesses element with value 3. Typically a nested for loop is used to process all elements. Getting number of rows: int nRows = array3.length; Getting number of columns: int nCols = array3[0].length;

Recursion Must be one or more base cases General case must head the solution towards the base cases. Infinite recursion occurs if the base case is never reached. Methods often call a recursive helper method. Classes may be recursive. Eg. Broccoli and NestedSquares.

Recursion Usually do not have a loop An if-else control structure handles the control flow and identifies the cases Be able to make a table or branching tree of recursive calls. Be able to write a recursive method based on a recursive definition.

Linear Search Recognize the linear search algorithm Identify best and worst situations for linear search.

Other array manipulations Use a state variable to maintain the current location in a partially filled array. Ex. The Song class in Simon from last year. Add an element to an array. Remove an element from an array.