Transcript (ppt)

CS100A, Fall 1997
Lecture 14, Tuesday, 21 October
Java Topics
Concepts:
• for loops
• Increment/decrement operators (++, --)
• Definite vs. indefinite iteration
• Inheritance revisited: super and this
• Static methods and variables revisited
Reading in Lewis/Loftus (review)
5.2 (181-183), 5.4 (196-199), 8 (285-329)
CS100A, Fall 1997, Lecture 14
1
for Loops
• The following schema occurs frequently.
k = lowValue;
while (k != highValue) {
// process kth step
…
k = k + 1;
}
• Java provides a for statement for use when
the loop initialization, test, and increment
are closely related.
for (k = lowValue; k != highValue; k = k+1)
// process kth step
CS100A, Fall 1997, Lecture 14
2
for Statement
• Template
for (initialization; condition; step)
statement;
• This is equivalent to
initialization;
while ( condition ) {
statement;
step;
}
• The step should increment or decrement a
variable as needed.
CS100A, Fall 1997, Lecture 14
3
for Statement
• Example:
// inv: a[0..i-1] = 0
for (i = 0; i < n; i = i + 1)
a[i] = 0;
is equivalent to
i = 0;
// inv: a[0..i-1] = 0
while (i < n) {
a[i] = 0;
i = i + 1;
}
CS100A, Fall 1997, Lecture 14
4
Increment/Decrement Operators (++, --)
• We often increase or decrease the value of a
variable by 1. The statements
variable++;
variable--;
respectively increase or decrease variable
by 1.
• Use: For clarity, use these operators only as
stand-alone statements or in for statements.
nItems--;
// inv: items[0..k-1] have been printed
for (k = 0; k != nItems; k++)
System.out.println(items[k]);
• Never use the value of a ++ or -- operation
Most confusing.
i = j++;
// FORBIDDEN!
CS100A, Fall 1997, Lecture 14
5
Definite vs. Indefinite Iteration
• Definite iteration: The number of iterations
to be performed is known before execution
of the loop begins.
• Indefinite iteration: The number of
iterations is not known in advance.
• Use while for indefinite iteration.
• for may be used for definite iteration.
while is also appropriate, particularly if it
makes it easier to derive a good loop
invariant.
• As a matter of style, never use for for
indefinite iteration.
CS100A, Fall 1997, Lecture 14
6
Definite vs. Indefinite Iteration — Decide
• Set the elements a[0..n-1] to -1.
• Search for the number 17 in a[0..12].
• Find the largest number in a[0..12].
• Read 12 numbers from the input.
• Read input until a -1 is encountered.
• Read input until 12 numbers are read or a -1
is encountered.
• Create a new array b that contains an exact
copy of array a.
CS100A, Fall 1997, Lecture 14
7
Inheritance Revisited
• In the last few lectures we examined three
related classes.
// Unordered set of integers
class List {nItems, items, List( ),
List(n), makeEmpty( ), add(n),
delete(n), isMember(n), size( ),
toString( ), find(n) }
// Sorted set of integers
class SortedList extends List
{ SortedList( ), SortedList(n), add(n),
delete(n), find(n), toString( ) }
// Dynamically expanding sorted
// set of integers
class ExpandableSortedList extends
SortedList {ExpandableSortedList( ),
ExpandableSortedList(n),
add(n), toString( ) }
CS100A, Fall 1997, Lecture 14
8
• Objects of these classes are related. Every
SortedList is also a List. Every
ExpandableSortedList is also a SortedList,
and is also a List.
• A variable or parameter that can refer to a
List can refer to any object that is a List or
is an instance of an extended List class.
Similarly for SortedList and its extensions.
• However, a variable of type SortedList can
only refer to objects of class SortedList or
classes extended from it. A SortedList is
(also) a List, but a List is not a SortedList.
List
SortedList
ExpandableSortedList
CS100A, Fall 1997, Lecture 14
9
• Examples
List p1, p2, p3;
SortedList s1, s2;
ExpandableSortedList e1, e2;
p1 = new List( );
// ok
p2 = new SortedList( ); // ok;
s1 = new SortedList( ); // ok
p3 = new ExpandableSortedList();//ok
e1 = new ExpandableSortedList();//ok
s2 = e1;
// ok
e2 = p1;
// illegal
p3 = p1;
// ok
p2 = e1;
// ok
CS100A, Fall 1997, Lecture 14
10
• An object of an extended class contains all
of the fields and methods of all of the
classes from which it is derived. Methods
or fields that are redefined in the extended
class mask corresponding items in the base
class, but they are still present.
SortedList s = new SortedList( );
s nItems, Items
List( ), List(n), find(n),
makeEmpty( ), size( ),
List part
add(n), delete(n),
isMember(n), toString( )
SortedList
part
SortedList( ), SortedList(n),
find(n), add(n), delete(n),
isMember(n), toString( )
Methods in italics are hidden or
overridden and can’t be referenced directly.
CS100A, Fall 1997, Lecture 14
11
this and super
• The identifier this is available in any nonstatic method and refers to the object whose
method is being executed.
• The identifier super is available in any
instance method of a derived class. It refers
to the current object, just like this, but
viewed as an instance of the parent class.
This allows access to overridden methods in
the extended class.
CS100A, Fall 1997, Lecture 14
12
• Example: Suppose we add a method meth
and instance variable n to class SortedList.
The method also has a parameter named n.
class SortedList extends List {
int n;
…
public void meth ( int n ) {
int i, j, k;
i = find(17);
// SortedList.find
j = this.find(17); // same
k = super.find(42) // List.find
n = n;
// copy parameter n
// to itself.
this.n = n; // store value of
// parameter n in class
// instance variable n
}
CS100A, Fall 1997, Lecture 14
13
Example (cont)
SortedList s = new SortedList( );
s.meth(100);
s
List part
SortedList
part
nItems, Items
List( ), List(n), find(n),
makeEmpty( ), size( ),
add(n), delete(n),
isMember(n), toString(
)
SortedList( ), SortedList(n),
find(n), add(n), delete(n),
isMember(n), toString( )
Methods in italics are hidden or overridden
in SortedList and can’t be referenced directly.
CS100A, Fall 1997, Lecture 14
14
super and Constructors
• super has one additional use. It may appear
at the beginning of a constructor for the
extended class to execute the constructor for
the parent class. The constructor executed
is the one whose parameter list matches the
argument list in the constructor call.
• Example:
class SortedList extends List
{
…
public SortedList(int n) {
super(n);
// execute List(n)
…
}
…
}
CS100A, Fall 1997, Lecture 14
15
static Revisited
• Every object contains a copy of all the
ordinary variables and methods declared in
a class. In methods, this and super are
implicitly declared and initialized to refer to
the object whose method is being executed.
• If a method or variable declaration includes
the qualifier static, then only one instance
of that method or variable exists and it is
associated with the class itself, not with any
objects of that class.
• Implication: this and super are not
available in static methods. There is no
object to refer to.
• Implication: A static method or variable is
referred to from outside the class by using
the class name: ClassName.memberName.
Inside the class it may be referred to
directly using memberName.
CS100A, Fall 1997, Lecture 14
16
Example: Class Math
• The Java library contains the usual
collection of basic math routines to compute
things like sines, square roots, etc.
• These items are not naturally associated
with “objects”. Instead, they exist as static
members of class Math.
class Math {
static double PI = 3.1415926535;
static double E = 2.7182818284;
// yield square root of x
public static double sqrt(double x)
{…}
// yield cosine of x
public static double sin(double x)
{…}
…
}
CS100A, Fall 1997, Lecture 14
17
Example (cont.)
class Application {
public static void main
(String [ ] args) {
double r = 288.0;
double a = Math.PI * r * r;
double x;
x = Math.sqrt(r/2);
}
}
CS100A, Fall 1997, Lecture 14
18