cs125 exam review
DESCRIPTION
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. Primitive variables Declare Initialize Operations - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/1.jpg)
CS125 Exam Review
Winter 2008
![Page 2: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/2.jpg)
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
![Page 3: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/3.jpg)
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
![Page 4: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/4.jpg)
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?
![Page 5: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/5.jpg)
Class Diagrams
public class Cat{
//instance variablesprivate String name;//constructorpublic Cat(String n){…}//methodspublic void meow(){…}
}
Cat- String name…
+ Cat(String n)- void meow()…
![Page 6: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/6.jpg)
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
![Page 7: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/7.jpg)
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?
![Page 8: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/8.jpg)
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?
![Page 9: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/9.jpg)
How Objects are stored
• Recall reference variables (objects)
reference object
Holds the memorylocation of whereto find the object
Actual object (instance variables, etc.)
![Page 10: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/10.jpg)
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 ?
![Page 11: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/11.jpg)
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? ___ ___
![Page 12: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/12.jpg)
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: _________
![Page 13: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/13.jpg)
Example 2
Square s1 = new Square(5);Square s2 = new Square(3);s1.makeEqual(s2);System.out.println(s1.equals(s2));
Output: _________
![Page 14: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/14.jpg)
Top-Down Design
• Step-wise refinement
• Helper methods
• Stubs
![Page 15: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/15.jpg)
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)
![Page 16: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/16.jpg)
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:
__________________________
![Page 17: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/17.jpg)
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?)
![Page 18: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/18.jpg)
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]
![Page 19: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/19.jpg)
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
![Page 20: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/20.jpg)
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];…
![Page 21: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/21.jpg)
Initialize Array of Objects (4/7)
for (int i=0; i<class.length; i++)
{ class[i] = new Person(); }
class
Person Person Person Person Person
![Page 22: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/22.jpg)
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
![Page 23: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/23.jpg)
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
![Page 24: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/24.jpg)
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…)
![Page 25: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/25.jpg)
2-D Arrays
Type[][] myArray = new Type[3][5];
myArray.length
myArray[0].length
![Page 26: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/26.jpg)
2-D Arrays
• 2D arrays are 1D arrays where each array is a 1D array
![Page 27: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/27.jpg)
2-D Arrays
• Same rules as 1-D array
• Examples…
![Page 28: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/28.jpg)
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.
![Page 29: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/29.jpg)
…
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.
![Page 30: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/30.jpg)
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?
![Page 31: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/31.jpg)
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
![Page 32: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/32.jpg)
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
![Page 33: CS125 Exam Review](https://reader035.vdocument.in/reader035/viewer/2022062803/56814865550346895db57330/html5/thumbnails/33.jpg)
Examples
Person p = new Student(“Josh”,19);
p.setAge(20);
- Will compile if _______ has setAge method- Will look in the _______ class for setAge