Computer Science
15-111 (Sections A & B), Spring 2007
Class Notes: 14-Feb-2007
Logistics
i.
Include your partner’s name in your
file header comment!
ii.
Do not include my header comment
(just yours!)
iii.
Reminder: style counts!
i.
Do not include my code at bottom of
file
ii.
Otherwise, it should be the same
code you submitted online
iii.
This will be used to grade style
Topic Outline:
1. Quick review:
a. Creating a new class
b. Instance variables (aka “member” variables)
i.
Accessors
and mutators
1. Only for other classes
2. Your code can access instance
variables directly!
ii.
Non-visibility
of private variables from other classes
iii.
Local
variables masking instance variables
iv.
Qualified
instance variable references
c. Static (class) variables
i.
Accessors,
mutators, visibility, local variable masking
ii.
Qualified
static variable references
iii.
Static
variables referenced via “this.” ß Bad
Style!
d. How to choose: static or instance or local variable?
e. Constructors
i.
The
default constructor
ii.
Adding
a constructor
iii.
Multiple
constructors
f.
Initialization
sequence
i.
First,
initialize static variables (once)
ii.
Second,
construct the entire superclass instance
iii.
Third,
initialize instance variables
iv.
Fourth,
call the constructor
g. Calling a Class method
h. Calling an Instance method
i.
There
is ALWAYS an object (aka “instance”)!
ii.
From
the same class: “this” is implicit
iii.
From
another class
2.
public
static void main(String[] args) { }
Understand this
signature! You are responsible for each
keyword(public,
static, void) and the
parameters
(String[] args).
class Foo {
public static void main(String[] args)
{
System.out.printf("%d args:
",args.length);
for (String arg : args)
System.out.printf("%s ",arg);
System.out.println();
}
// try: java
Foo -mode text foo.txt
// prints: 3 args: -mode text foo.txt
}
3. Passing Objects: References
import
java.util.*;
class Foo {
public static void badClear(ArrayList<String> list) {
list = new
ArrayList<String>();
}
public static void goodClear(ArrayList<String> list) {
list.clear();
}
public static void main(String[] args)
{
ArrayList<String> list = new
ArrayList<String>();
list.add("Go");
list.add("Penguins");
System.out.println(list.toString()); // prints [Go, Penguins]
badClear(list);
System.out.println(list.toString()); // prints [Go,
Penguins]
goodClear(list);
System.out.println(list.toString()); // prints []
}
}
4. Passing null References:
import
java.util.*;
class Foo {
public static void main(String[] args)
{
String[] strings = new String[5];
System.out.println(strings[0]); // prints null
System.out.println(strings[0].length());
// nullPointerException
}
}
5. Passing Self-References: “this”
class ArrayOfTen {
Object[] values = new Object[10];
public boolean contains(Object
object) {
for (int i=0; i<10; i++)
if (object == values[i])
return true;
return false;
}
public boolean containsSelf() {
return contains(this); // ß
HERE IT IS!!!
}
public static void main(String[]
args) {
ArrayOfTen foo = new
ArrayOfTen();
System.out.println(foo.containsSelf());
// <- false
foo.values[3] = foo;
System.out.println(foo.containsSelf());
// <- true
}
}
6. Nested (aka Inner) Classes
MyList.java
from the book
7. Anonymous Inner Classes
a. Example: event handlers in BasicGUI.java
b. Also:
class Foo {
public Foo(int value) { this.value =
value; }
public String toString() { return
"" + this.value; }
public static void main(String[] args)
{
Foo i1 = new Foo(1);
Foo i2 = new Foo(2) {
public String toString() {
// The next line fails! value is private!
// return ("My value is
" + this.value);
return ("My value:
" + this.getValue());
}
};
System.out.println(i1); // prints: 1
System.out.println(i2); //
prints: My value: 2
}
public int getValue() { return
this.value; }
private int value;
}
8. Final Constants
class Foo {
public static final double PI_SQUARED
= 9.8696044;
public static void main(String[] args)
{
System.out.println(Math.PI);
Math.PI = 2; // won't compile (PI is final)
System.out.println(Foo.PI_SQUARED);
Foo.PI_SQUARED = 2; // won't compile
}
}
9. Final Classes
class MyInteger
extends Integer { }; //
ß Will not work, Integer is final!
class MyMath
extends Math { }; // ß
Will not work, Math is final!
10. Private Constructors
Math myMath =
new Math(); // ß Will not work, Math() is private!
11. Arrays of Objects and Initialization
in the Constructor (useful for Tetris!)
class Tetris {
private int[][] board = new
int[rows][cols];
public Tetris() {
for (int row=0; row<rows; row++)
for (int col=0; col<cols; col++)
board[row][col] = emptyColor;
}
...
}
12. Object-Oriented Design, or O-O-D (read
Section 6.18 carefully!)
a. Type-extensibility: The ability to add user-defined types to the
language so that they are as easy to use as native types.
b. Abstract Data Type (ADT) – the
public behavior of a type.
c. Implementation – the actual code,
including the private methods and data.
i.
The
same ADT can have multiple implementations!
d. Encapsulation: ability to hide internal detail while
providing a public interface to a user-defined type.
e. UML = Universal Modeling Language
(describes O-O models)