CS 125 Exam Review Winter 2008 Some Exam

  • Slides: 33
Download presentation
CS 125 Exam Review Winter 2008

CS 125 Exam Review Winter 2008

Some Exam Info • Tuesday (22 nd) at 4: 00 -6: 30 pm in

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

Old Topics • Primitive variables • Declare • Initialize • Operations • Mod, Division

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

Overloading Two methods in the same class with… • • • Exact same method

Overloading Two methods in the same class with… • • • Exact same method name Different parameters (by number or type) Ignore return type • Examples: index. Of, put. Peg • Midterm: Why the overloaded methods didn’t work?

Class Diagrams public class Cat { //instance variables private String name; //constructor public Cat(String

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() …

Information Hiding (1/2) • Designing methods so the user doesn’t need to understand how

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

Information Hiding (2/2) • Visibility modifiers - public or private – Public: accessed from

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?

Encapsulation • Type of information hiding • Separating interface & implementation • Interface –

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?

How Objects are stored • Recall reference variables (objects) reference Holds the memory location

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

Comparing Objects Blob b 1 = new Blob(2); Blob b 2 = new Blob(2);

Comparing Objects Blob b 1 = new Blob(2); Blob b 2 = new Blob(2); Blob b 3 = b 1; Blob b 1 = new Blob(2); Blob b 2 = new Blob(2); Blob b 3 = b 1 b 3. triple. Value() b 1==b 2 ? b 1. equals(b 2) ? b 1==b 3 ? b 1. equals(b 2) ? b 1. equals(b 3) ? b 1==b 3 ?

Passing Parameters to methods Type of Parameter What is passed? Change to original? “Passed-byvalue”

Passing Parameters to methods Type of Parameter What is passed? Change to original? “Passed-byvalue” “Passed-byreference” Primitive Object Copy of primitive Reference to object ___

Example 1 Blah my. Object = new Blah(); int n 1=3, n 2=5; my.

Example 1 Blah my. Object = new Blah(); int n 1=3, n 2=5; my. Object. make. Equal(n 1, n 2); System. out. println(n 1==n 2); //somewhere else public void make. Equal(int n 1, int n 2) { } n 2=n 1; Output: _____ Output?

Example 2 Square s 1 = new Square(5); Square s 2 = new Square(3);

Example 2 Square s 1 = new Square(5); Square s 2 = new Square(3); s 1. make. Equal(s 2); System. out. println(s 1. equals(s 2)); Output: _____

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

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

Static Methods • Usually, we call methods on objects – my. Board. put. Peg(…),

Static Methods • Usually, we call methods on objects – my. Board. put. Peg(…), 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)

Static Methods • Static methods can only call ______ methods and variables • Interpreting

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

Constants • Key word: final • Arbitrary numbers should never appear in code (except

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

Declaring Arrays (1/7) type[] array. Name = new type[size] 1. char[] letters = new

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

Array of Objects (2/7) Person[] class = new Person[5]; Here, references are made for

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

Initializing Arrays (3/7) • While primitive variables are usually assigned a default value (0,

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]; …

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

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

Using Arrays (5/7) Student[] class = new Student[#]; • Find length: class. length •

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

Using Arrays (6/7) • You should easily know how to: – Search – Iterate

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

Array Questions (7/7) 1. Find lowest/highest value in array. 2. Shift all values of

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

2 -D Arrays Type[][] my. Array = new Type[3][5]; my. Array[0]. length my. Array.

2 -D Arrays Type[][] my. Array = new Type[3][5]; my. Array[0]. length my. Array. length

2 -D Arrays • 2 D arrays are 1 D arrays where each array

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

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

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

Inheritance Person super class (more general) Student has ‘everything’ Person has and (maybe) more.

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

… Say Coffee extends Drink: Drink d = new Coffee(); ok? Coffee c =

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

Overriding Methods • A subclass overrides a method in its super class. – Use

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?

Accessing Super Class • Keyword: super • Use super(params) – To access parent’s constructor

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

Method Resolution Object Somewhere in Hot. Drink class: • this. method. Name() Drink Hot.

Method Resolution Object Somewhere in Hot. Drink class: • this. method. Name() Drink Hot. Drink Coffee – Looks at itself first – Hot. Drink ->… • super. method. Name() – Starts looking in parent class – Drink -> Object

Examples Person p = new Student(“Josh”, 19); p. set. Age(20); - Will compile if

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