CS100A, Fall 1997

advertisement
CS100A, Fall 1997
After discussing the notion of an algorithm and (computer) program briefly,
the first lecture will introduce method calls, since method calls are mandatory
when doing any I/O or graphics and since they are one of the most frequently
used statements.
First, we will discuss the heading of a method --the precise comment that
specifies what the method does, prefix modifiers, name, and parameters.
Throughout, the need for a precise specification will be emphasized, for
without a precise specification, one can’t easily tell what the method does.
Then, we will discuss method calls (invocations), using a substitution
semantics: the method call does what the specification of the method says, but
with parameters replaced by arguments of the call.
We will discuss the fact that THEY (the students) could execute something
like drawOval(5,5,4,3), if given the right color crayon and the graph paper on
which to draw it. Something like drawOval(5,5,4,3) is simply shorthand for the
comment of the method, with the parameters replaced by the arguments.
We will illustrate this on the computer, using a small program that draws
circles/squares and titles for them in a window. We will also step through
execution of the program using the debugger, so they get the idea of sequential
execution.
A nice feature of using the debugger for this is that we can show what
happens when the Drawing window is first covered by another window and
then uncovered by moving the other window away: the Drawing window is
now all messed up --but the system calls method paint in order to redraw that
window! Thus, the students get a bit of a feel for what is going on behind the
scenes when they move windows around.
Necessarily, we will also mention integer arithmetic, but weleave a full
explanation of integer expressions to the first section.
We will explain carefully that many parts of a Java program will not be
understandable at this point --prefix modifiers, classes, the “g.” on
“g.drawOval(…)”, and so forth. These will all become clearer as the course
progresses.
CS100A, Fall 1997.
Lecture 1
1
CS100A, Fall 1997
• Goal: learn to write intelligible and
correct computer programs —
effectively and efficiently.
• Main Language: Java (object
oriented, cleaner and easier to use
than C++).
• Software: Metrowerks CodeWarrior,
on the Macintosh or the PC.
• Instructors: Gries & Perkins
CS100A, Fall 1997.
Lecture 1
2
Main Concepts today:
•
•
•
•
•
Algorithm, program.
Method and method call.
Graphics.
Exucution of a program.
Executing using the debugger
Reading:
• Lewis/Loftus 34-40, 89-92
• Handout for assignment 0, Sect. 2-4
First assignment due: Tuesday, 9
Sept. at beginning of lecture!!!
CS100A, Fall 1997.
Lecture 1
3
Algorithm: A set of instructions
for performing some task. An
algorithm is:
•
•
•
•
precise
unambiguous
effective (always works)
abstract
Examples:
• A recipe for cooking soup.
• “Here’s a list of integers; tell me
whether each one is even or odd.”
• Play a game of tic-tac-toe in a way
that you never lose.
CS100A, Fall 1997.
Lecture 1
4
• Program: An algorithm that is written
in some language: English, Sanskrit,
C++, Java, FORTRAN, Pascal.
• Computer program: a program in a
“computer language”, e.g. Java.
Abstraction
Representation
Algorithm
program
Number
numeral
Object with
property
“threeness”
3, three,
|||, III
CS100A, Fall 1997.
Lecture 1
5
An algorithm is usually given as a
sequence of instructions to execute
(in the order given).
In a programming language like Java,
each of these instructions is called a
statement.
One of the most frequent kinds of
statement used in Java is the call of a
method call, or method invocation.
To understand a method call, we need to
know a bit about what a method is.
Terms sometimes used interchangeably:
method
procedure
CS100A, Fall 1997.
Lecture 1
function
6
// Draw a line from point (x1,y1) to
// point (x2,y2)
public void drawLine
(int x1, int y1, int x2, int y2)
Heading of a method:
• Comment: explains precisely what
the method is to do. Written as a
command to do it.
• Some prefix modifiers (public and
void) --explained later
• The name of the method: drawLine.
• The parameters: x1, y1, x2, y2 (along
with their types (int)).
CS100A, Fall 1997.
Lecture 1
7
HEADING:
// Draw a line from point (x1,y1) to
// point (x2,y2)
public void drawLine
(int x1, int y1, int x2, int y2)
CALL:
drawLine(3, 5, 1, 1);
COMMAND TO BE EXECUTED:
Draw a line from point (3, 5) to
point (1, 1).
CS100A, Fall 1997.
Lecture 1
8
HEADING:
// Print the largest of x, y, and z
public void printMax
(int x, int y, int z)
CALL:
printMax(20, 5, 20-5*4);
COMMAND TO BE EXECUTED:
Print the largest of 20, 5, and 20-5*4.
Note: the method name, printMax, has
no meaning for Java. We could as
well have used the name aaa, or w.
CS100A, Fall 1997.
Lecture 1
9
HEADING:
// In the output window, skip a line and
// print “Hello”.
public void printHello( )
CALL:
printHello( );
COMMAND TO BE EXECUTED:
In the output window, skip a line and
print “Hello”.
CS100A, Fall 1997.
Lecture 1
10
Important points:
1. A method can have zero parameters.
2. Syntax of a method call:
• a name:
•
(
•
list of zero or more arguments
(names), separated by “,”
• )
• ;
3. Lewis/Loftus use formal parameter
for parameter and actual parameter
for argument.
CS100A, Fall 1997.
Lecture 1
11
3. To find out what a method call does,
make a copy of the specification of the
method (the comment in the heading)
and replace all occurrences of the
parameters by the arguments of the call.
2. For this process to be effective, the
specification (the comment) must be
• precise
• understandable
• correct (it must say exactly what the
•
method actually does
Rest of lecture: Demonstrate calls with a
small Java program, using both run and
debug.
CS100A, Fall 1997.
Lecture 1
12
Download