apcs java ab 2004 review of cs1 and cs2 review for ap test #1 sources: 2003 workshop notes from...

30
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

Upload: naomi-hicks

Post on 13-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

APCS Java AB 2004

Review of CS1 and CS2Review for AP test #1Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go with Computing Concepts With Java

Essentials, Fran Trees and Cay Horstmann

Page 2: 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

Formal Parameters

Specified in the definition of a methodRepresent information passed to the

method when it is calledCan be used to determine the behavior

of the method (like a blue print)Can be used in calculations carried out

by the method

Page 3: 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

Actual Parameters

Also called argumentsThe values given as parameters when a

method is calledMay be literals (number, string literal)May be a variable in the current contextMay be an expression that matches the type

specified in the declarationExpression may be a newly created object

using new (…)

Page 4: 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

Implicit parameter to a class

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

Page 5: 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

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

Page 6: 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

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.

Page 7: 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

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.

Page 8: 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

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.

Page 9: 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

Static Methods

Do not operate on objectsHave the keyword static before the

method signatureAre called using the class name rather

than an object nameAn example of a class with static

methods is the Math class, java.util.Math

Page 10: 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

Class Variables

Declared with the word staticInitialized once when the first object of

that class type is instantiated.Example: given in class

Page 11: 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

Boolean Expressions

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

Page 12: 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

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.

Page 13: 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

Immutable classes

A class is immutable if it has no modifiers.

String is an example of an immutable class.

Page 14: 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

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

Page 15: 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

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.

Page 16: 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

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.

Page 17: 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

An example of compareTo for a Coin class

Coin.java

Page 18: 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

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 intExample 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.java

Page 19: 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

Polymorphism

Also called dynamic bindingSince 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.

Page 20: 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

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.

Page 21: 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

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.

Page 22: 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

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

Page 23: 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

Is-A vs. Has-A

Consider the Athlete and SkiJumper example.

A subclass is-a superclassA skijumper is-a athleteHas-A implies a uses relationship (as in

coupling). The SkiJumper has-a name (String).

Page 24: 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

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;

Page 25: 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

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.

Page 26: 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

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;

Page 27: 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

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.

Page 28: 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

Recursion

Usually do not have a loopAn if-else control structure handles the

control flow and identifies the casesBe able to make a table or branching

tree of recursive calls.Be able to write a recursive method

based on a recursive definition.

Page 29: 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

Linear Search

Recognize the linear search algorithmIdentify best and worst situations for

linear search.

Page 30: 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

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.