Review For Midterm 3: CS 101 Spring 2007
Review For Midterm 3: CS 101 Spring 2007
CS 101
Spring 2007
Topic Coverage
Loops
Chapter
Methods
Chapter
Classes
Chapter
Chapter
Arrays
Chapter
4
5
6, Designing and creating classes
9, Static fields and methods, wrapper classes
8
Classes
Circle class
Radius
PI = 3.141592653589793234
(x,y) location
We are only going to play with the first two (radius and
PI) in this example
Circle
- radius = 0.0
- PI = 3.14159
-
Example: System.in
Example: System.out.println
(c.radius);
Example: c.PI = 4;
Circle
- radius = 0.0
- PI = 3.14159
-
+
main
(String[]
args)
(String[]
args)
Create circles
A few others
10
c1
Circle
- radius = 0.0
- PI = 3.14159
-
+ Circle()
+ Circle (double r)
+
The constructor is always the first method called when creating (or
constructing) an object
11
c1
Circle
- radius = 0.0
- PI = 3.14159
-
+ Circle()
+ Circle (double r)
+
The constructor is always the first method called when creating (or
constructing) an object
12
Constructors
Note that there are two methods with the same name!
13
- radius = 1.0
- PI = 3.1415926536
-
+ Circle()
+ Circle (double r)
+
Shorthand representation
c
Circle
- radius = 1.0
- PI = 3.14159
14
main
(String[]
args)
15
c1
Circle
- radius = 1.0
- PI = 3.14159
c2
Circle
- radius = 1.0
- PI = 3.14159
c3
Circle
- radius = 1.0
- PI = 3.14159
c4
Circle
- radius = 1.0
- PI = 3.14159
16
(String[]
args)
17
c1
c2
Circle
- radius = 1.0
- PI = 3.14159
Circle
- radius = 1.0
- PI = 3.14159
c1000000
Circle
- radius = 1.0
- PI = 3.14159
Circle
- radius = 1.0
c2
c3
Circle
- radius = 1.0
PI
Circle
- radius = 1.0
3.14159265
36
c1000000
c4
Circle
- radius = 1.0
19
It prints 4.3
20
Circle.PI
21
22
System.in
System.out
Math.PI
Integer.MAX_VALUE
23
Such as getPi()
It is declared as static:
static double getPi () {
return PI;
}
Thus, they are often called static methods
Because Java knows that class methods dont care about
any specific object, it only allows them to access static
variables (aka class variables)
Consider Math.sin()
Class (i.e. static) methods can ONLY access class (i.e. static)
variables
26
Loops
Chapter 4
27
Chapter 4: Iteration
while loop syntax
While statements:
while ( expression ) action
Action is executed repeatedly while expression is
true
Once expression is false, program execution
moves on to next statement
Action can be a single statement or a block
If expression is initially false, action is never
executed
28
int valuesProcessed = 0;
double valueSum = 0;
// set up the input
Reading in values
if (valuesProcessed > 0) {
double average = valueSum / valuesProcessed;
System.out.println("Average: " + average);
} else {
System.out.println("No list to average");
}
29
30
Chapter 4: Iteration
for loop syntax
For statements:
for ( forinit; forexpression; forupdate ) action
forinit is executed once only (before the loop
starts the first time)
Action is executed repeatedly while
forexpression is true
After action is executed at the end of each loop,
forupdate is executed
Once forexpression is false, program execution
moves on to next statement
Action can be a single statement or a block
If expression is initially false, action is never
31
executed
Execution Trace
for ( int i = 0; i < 3; ++i ) {
0
3
2
1
}
System.out.println("all done");
i is 0
i is 1
i is 2
all done
32
Translates to:
int count;
count = 0;
while (count < 10) {
System.out.println (count);
count++;
}
33
34
Chapter 4: Iteration
Common pitfalls
35
break
Immediately stops the execution of the current
loop
continue
Immediately starts execution of the next loop
The for update is executed, then the condition is
tested
36
Chapter 4: Iteration
File access
37
Chapter 4: Iteration
Scanner methods
nextInt()
nextDouble()
38
Classes
Chapter 6
39
}
public void bar() {e is visible in the method after it is declared
}
int f;
}
40
41
42
The values passed into the method call are called actual
parameters
foo (7);
// 7 is the actual parameter
The parameter names within the method are called formal
parameters
44
Java will figure out which one you mean to call by which
methods parameter list best matches the actual
parameters you supply
46
48
Arrays
Chapter 8
50
An array is an object
Thus, it is actually a reference to a series of
values somewhere in memory
The individual parts of an array are called elements
Elements can be a primitive type or an object
All elements in the array must have the same type
An array is an object, with fields and methods
The length is a field in the array object
clone() is a method in the array object
51
Array declaration
int[] array;
Array initialization
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Note that the int here could have been String, etc.
Consider
int[] a;
int[] b = null;
int[] c = new int[5];
int[] d = { 1, 2, 3, 4, 5 };
a = c;
d = c;
a
null
53
54
55
57
Segment
int c[][] = {{1, 2}, {3, 4}, {5, 6}, {7, 8, 9}};
Produces
c[0]
c[1]
c[2]
c[3]
c[2][0] c[2][1]
c[0][0] c[0][1]
ragged array
c[1][0] c[1][1]
58
or
0
59
Collection interface
60