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 questions & old exams– Reference sheet
• Bring WATCARD, pen, pencil, eraser only
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
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?
Class Diagrams
public class Cat{
//instance variablesprivate String name;//constructorpublic Cat(String n){…}//methodspublic 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 they work– Examples, putPeg, removePeg (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 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
– 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 object
Holds the memorylocation of whereto find the object
Actual object (instance variables, etc.)
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 = b1b3.tripleValue()
b1.equals(b2) ?b1.equals(b3) ?b1==b3 ?
Passing Parameters to methods
“Passed-by-value”
“Passed-by-reference”
Type of Parameter Primitive Object
What is passed? Copy of primitive
Reference to object
Change to original? ___ ___
Example 1
Output?
Blah myObject = new Blah();int n1=3, n2=5;myObject.makeEqual(n1,n2);System.out.println(n1==n2);
//somewhere elsepublic void makeEqual(int n1,int n2){
n2=n1;}
Output: _________
Example 2
Square s1 = new Square(5);Square s2 = new Square(3);s1.makeEqual(s2);System.out.println(s1.equals(s2));
Output: _________
Top-Down Design
• Step-wise refinement
• Helper methods
• Stubs
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)
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 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[] 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]
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
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] = new Person(); }
class
Person Person Person Person Person
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 (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 an array up (and last value is now first)
(Think about how you would do these…)
2-D Arrays
Type[][] myArray = new Type[3][5];
myArray.length
myArray[0].length
2-D Arrays
• 2D arrays are 1D arrays where each array is a 1D array
2-D Arrays
• Same rules as 1-D array
• Examples…
Inheritance
Person
StudentPerson
Student
Student extends Person, Student is a PersonStudent inherits everything public the Person has.
super class(more general)
Student has ‘everything’Person has and (maybe) more.
…
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 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– Must be first line in child’s constructor
• Use super.someMethod(params)– To access parent’s someMethod method
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
Examples
Person p = new Student(“Josh”,19);
p.setAge(20);
- Will compile if _______ has setAge method- Will look in the _______ class for setAge
Top Related