Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS125 Exam Review Winter 2008. Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice.

Similar presentations


Presentation on theme: "CS125 Exam Review Winter 2008. Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice."— Presentation transcript:

1 CS125 Exam Review Winter 2008

2 Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice questions & old exams –Reference sheet Bring WATCARD, pen, pencil, eraser only

3 Old Topics Primitive variables Declare Initialize Operations Mod, Division String methods Board class Coordinate class Scanner next(), nextLine() Boolean statements AND,OR,NOT If statements Loops for while Tracing

4 Overloading Two methods in the same class with… Exact same method name Different parameters (by number or type) Ignore return type Examples: indexOf, putPeg Midterm: Why the overloaded methods didn’t work?

5 Class Diagrams public class Cat { //instance variables private String name; //constructor public Cat(String n) {…} //methods public void meow() {…} } Cat - String name … + Cat(String n) - void meow() …

6 Information Hiding (1/2) Designing methods so the user doesn’t need to understand how they work –Examples, putPeg, removePeg (we use them, but don’t know how they work) Why do this? _______, _______, _______ Pre- and post- conditions

7 Information Hiding (2/2) Visibility modifiers - public or private –Public: accessed from anywhere –Private: accessed within its own class Make sure you understand this key idea :) Applies to methods or variables ________ variables are always private So how do we use them?

8 Encapsulation Type of information hiding Separating interface & implementation Interface –What the user accesses…method signatures, pre- and post- conditions Implementation –What programmer sees Where do private methods fall?

9 How Objects are stored Recall reference variables (objects) reference object Holds the memory location of where to find the object Actual object (instance variables, etc.)

10 Comparing Objects Blob b1 = new Blob(2); Blob b2 = new Blob(2); Blob b3 = b1; b1==b2 ? b1.equals(b2) ? b1==b3 ? Blob b1 = new Blob(2); Blob b2 = new Blob(2); Blob b3 = b1 b3.tripleValue() b1.equals(b2) ? b1.equals(b3) ? b1==b3 ?

11 Passing Parameters to methods “Passed-by- value” “Passed-by- reference” Type of Parameter PrimitiveObject What is passed? Copy of primitive Reference to object Change to original? ___

12 Example 1 Output? Blah myObject = new Blah(); int n1=3, n2=5; myObject.makeEqual(n1,n2); System.out.println(n1==n2); //somewhere else public void makeEqual(int n1,int n2) { n2=n1; } Output: _________

13 Example 2 Square s1 = new Square(5); Square s2 = new Square(3); s1.makeEqual(s2); System.out.println(s1.equals(s2)); Output: _________

14 Top-Down Design Step-wise refinement Helper methods Stubs

15 Static Methods Usually, we call methods on objects –myBoard.putPeg(…), rect.area() Sometimes it doesn’t make sense to call a method on an object, so we call it directly from a class –Math.abs(-7)

16 Static Methods Static methods can only call ______ methods and variables Interpreting reference sheet: Math class: + static double abs (double a), returns double Store absolute value of -5.4 in double num: __________________________

17 Constants Key word: final Arbitrary numbers should never appear in code (except 0 or 1) Usually declared/initialized: public static final int PI = 3.14 (As a side note, why public? why static?)

18 Declaring Arrays (1/7) type[] arrayName = new type[size] 1.char[] letters = new char[5]; 2.String[] names; names = new String[30/2*3]; 3. int a=10; MyObject[] mult = new int[a]

19 Array of Objects (2/7) Person[] class = new Person[5]; Here, references are made for 5 Person objects… but they are all null. (NullPointerException, anyone?) class

20 Initializing Arrays (3/7) While primitive variables are usually assigned a default value (0,false,etc.) it is good practice to initialize them. -Or you may want different initial values Initialize alpha array to the alphabet (I.e. alpha[0]=‘a’, alpha[1]=‘b’, etc.) char[] alpha = new char[26]; …

21 Initialize Array of Objects (4/7) for (int i=0; i<class.length; i++) { class[i] = new Person(); } class Person

22 Using Arrays (5/7) Student[] class = new Student[#]; Find length: class.length Index range _______ to _______ To access individual element –class[index] –Treat ‘class[2]’ like any other single Student object

23 Using Arrays (6/7) You should easily know how to: –Search –Iterate (go through) –Manipulate values –Pass an array as a method parameter –Return an array from a method

24 Array Questions (7/7) 1.Find lowest/highest value in array. 2.Shift all values of an array up (and last value is now first) (Think about how you would do these…)

25 2-D Arrays Type[][] myArray = new Type[3][5]; myArray.length myArray[0].length

26 2-D Arrays 2D arrays are 1D arrays where each array is a 1D array

27 2-D Arrays Same rules as 1-D array Examples…

28 Inheritance Person Student Person Student Student extends Person, Student is a Person Student inherits everything public the Person has. super class (more general) Student has ‘everything’ Person has and (maybe) more.

29 … Say Coffee extends Drink: Drink d = new Coffee(); ok? Coffee c = new Drink(); ok? Wanting any drink and getting coffee is good; wanting coffee and getting a drink may not be.

30 Overriding Methods A subclass overrides a method in its super class. –Use the exact same method signature –Used to make the method more tailored to the class –Example, Lab 12 Vehicle Not the same as overloading. What’s the difference?

31 Accessing Super Class Keyword: super Use super(params) –To access parent’s constructor –Must be first line in child’s constructor Use super.someMethod(params) –To access parent’s someMethod method

32 Method Resolution Somewhere in HotDrink class: this.methodName() –Looks at itself first –HotDrink -> Drink ->… super.methodName() –Starts looking in parent class –Drink -> Object Object Drink HotDrink Coffee

33 Examples Person p = new Student(“Josh”,19); p.setAge(20); -Will compile if _______ has setAge method -Will look in the _______ class for setAge


Download ppt "CS125 Exam Review Winter 2008. Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice."

Similar presentations


Ads by Google