300 likes | 568 Views
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
E N D
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.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.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.