Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 1 CS100A, Fall 1998 After discussing the notion of an algorithm and (computer) program briefly, the first lecture.

Similar presentations


Presentation on theme: "CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 1 CS100A, Fall 1998 After discussing the notion of an algorithm and (computer) program briefly, the first lecture."— Presentation transcript:

1 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 1 CS100A, Fall 1998 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.

2 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 2 CS100A, Fall 1998 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 & Cardie

3 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 3 Main Concepts today: Algorithm, program. Method and method call. Graphics. Execution of a program. Executing using the debugger Reading: Holmes 2, 12-19, 25, 28-30 Handout for assignment 0, Sect. 2-4 First assignment due: Tuesday, 8 Sept. at beginning of lecture!!!

4 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 4 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.

5 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 5 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.

6 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 6 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 function

7 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 7 // 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)).

8 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 8 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).

9 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 9 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.

10 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 10 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”.

11 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 11 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. 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.

12 CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 12 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.


Download ppt "CS100A, Fall 1998. Lec. 1, Tue. 1 Sept. 1 CS100A, Fall 1998 After discussing the notion of an algorithm and (computer) program briefly, the first lecture."

Similar presentations


Ads by Google