21
CS100 Lecture 18 1 Announcements • P4 due tomorrow • P5 handed out today • References? Leave me a picture and a short note

CS100Lecture 181 Announcements P4 due tomorrow P5 handed out today References? Leave me a picture and a short note

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

CS100 Lecture 18 1

Announcements

• P4 due tomorrow

• P5 handed out today

• References? Leave me a picture and a short note

CS100 Lecture 18 2

Today’s Topics

• Pascal’s Triangle (fun with 2D arrays)

• Applets

• Parameter passing -- Examples galore!

CS100 Lecture 18 3

Pascal’s Triangle 1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

int [] [] p;

p = new int[7][]; // p.length is 7

p[0] = new int[1]; // p[0].length is 1

p[1] = new int[2]; // p[1].length is 2

p[2] = new int[3]; // p]2].length is 3

Note: Elements of an array can bearrays of different lengths!

CS100 Lecture 18 4

Compute Pascal’s Triangle// Yield Pascal's triangle with size rows

public static int[][] calculatePascal(int size) {

int[][]p= new int[size][]; //the triangle

for (int r= 0; r != size; r= r+1) {

// Allocate row i of triangle --its r+1 values

p[r]= new int[r + 1];

// Calculate row r of Pascal's triangle

p[r][0]= 1;

for (int c= 1; c < r; c= c+1)

p[r][c]= p[r-1][c-1] + p[r-1][c];

p[r][r]= 1;

}

return p;

}

CS100 Lecture 18 5

Java and World Wide WebHTML is the language in which files (or pages) are written

for the world wide web. Internet Explorer and Netscape, the two most used browsers, read html files and display them on the screen. Example html file:

<html><head><title> This goes at the top of the page</title></head><body><P>This is a new paragraph. <em>this is in italics.</em>

<b>This is in bold</b> but this isn’t.</P><P><font size=+1>A new paragraph in a larger

font</font></P></body> </html>

CS100 Lecture 18 6

HTML is a markup language

• <command> begins a command, </command> ends it.• There are commands for paragraphs, new line, font change,

font-style change, lists, tables, and many more.• <A href=“http://www.cs.cornell.edu”>Cornell’s page</A>

produces a link on the page: Cornell’s page

CS100 Lecture 18 7

Applets in htmlCommand applet:

<applet archive=AppletClasses.jar

code=“className.class”

width=200 height=400

</applet>

Execute a java program. The window for it is 200 x 400.

All of the classes in it are in file AppletClasses.jar. The class to begin execution is in file className.class.

Many examples of applets. To see one, bring up page www.cs.cornell.edu, click on CSFacts, and click to get to “Faculty over the years”

CS100 Lecture 18 8

Applets and OO• Applets are a good advertisement for object oriented

programming• Your classes inherit from Applet• Applet contains all the details of how an applet works and

interacts with the system . . . • . . . So you can just worry about what you want your applet to do

• Note: no main method, already inside a running program when applet is invoked.

CS100 Lecture 18 9

Example: import java.awt.*;

import java.applet.Applet;

public class TrivialApplet extends Applet

{ public void init() {

repaint(); }

public void paint( Graphics g ) {

g.drawString( "Hello World!", 10, 10 );

g.drawString("Parking", 50, 50);

g.drawOval(45, 24, 43, 43);

g.drawLine(82,30,51, 61);

}}

CS100 Lecture 18 10

Html for this applet:

<title>TrivialApplet</title>

<hr>

<applet archive="AppletClasses.jar" code="TrivialApplet.class" width=200 height=200>

</applet>

<hr>

<a href="TrivialApplet.java">The source.</a>

CS100 Lecture 18 11

CS100 Lecture 18 12

Graphics• Applets are most useful if you employ graphics

• Check out the package java.awt in the Java API -- lots of graphics classes there.

• Remember -- you can use all the programming techniques you’ve learned so far (loops, ifs, method calls, etc. etc.) when you’re writing applets - this was just a trivial example.

CS100 Lecture 18 13

Method Invocation• 0. Allocate a new set of locations to contain

the parameters and local variables of the method being called, in what we call a frame.

• 1. Assign the arguments of the call to the parameters of the method.

• 2. Execute the procedure body.

• 3. Delete the frame produced in step 0; if the method is a function, return the value of the function to the place of call.

• Terminology: parameter: formal parameter argument: actual parameter

MAKE THE SPACE

ASSIGN (COPY!)ARGUMENTS

TOPARAMETERS

RUN THEMETHOD

DELETE FRAMERETURN VALUE

CS100 Lecture 18 14

Parameter-Passing Reduxpublic class TrivialApplication {

public static void main(String args[]) {

int x = 1;

int y = 2;

C c = new C();

c.myMethod(x, y);

System.out.println("x = " + x + " y = " + y); }

}

public class C {

public void myMethod(int a, int b) {

a = 3;

b = 4;}

}

CS100 Lecture 18 15

Names and boxespublic class TrivialApplication {

public static void main(String args[]) {

int x = 1;

int y = 2;

C c = new C();

c.myMethod(x, y);

System.out.println("x = " + x + " y = " + y); }

}

public class C {

public void myMethod(int x, int y) {

x = 3;

y = 4;}

}

CS100 Lecture 18 16

Names and boxes, 2public class TrivialApplication {

public static void main(String args[]) {

int x = 1;

int y = 2;

C c = new C();

c.myMethod(x, y);

System.out.println("x = " + x + " y = " + y); }

}

public class C {

public void myMethod(int y, int x) {

x = 3;

y = 4;}

}

CS100 Lecture 18 17

Complicationspublic class TrivialApplication {

public static void main(String args[]) {

int x = 1;

int y = 2;

C c = new C(x, y);

c.myMethod(c.x, c.y);

System.out.println("x = " + x + " y = " + y); }}

public class C {

int x; int y;

public C(int xx, int yy) {x = xx; y = yy;}

public void myMethod(int a, int b) {

a = 3; b = 4;}

}

CS100 Lecture 18 18

Complications, 2public class TrivialApplication {

public static void main(String args[]) {

int x = 1;

int y = 2;

C c = new C(x, y);

c.myMethod();

x = c.x; y = c.y;

System.out.println("x = " + x + " y = " + y); }}

public class C {

int x; int y;

public C(int xx, int yy) {x = xx; y = yy;}

public void myMethod() {

x = 3; y = 4;}

}

CS100 Lecture 18 19

Complications, 3public class TrivialApplication {

public static void main(String args[]) {

int x = 1;

int y = 2;

C c = new C();

c.myMethod(x, x);

System.out.println("x = " + x + " y = " + y); }

}

public class C {

public void myMethod(int y, int x) {

x = 3;

y = 4;}

}

CS100 Lecture 18 20

. . .public class TrivialApplication {

public static void main(String args[]) {

int x = 1;

int y = 2;

C c = new C(x, y);

c.myMethod(c);

x = c.x; y = c.y;

System.out.println("x = " + x + " y = " + y); }}

public class C {

int x; int y;

public C(int xx, int yy) {x = xx; y = yy;}

public void myMethod(C d) {

d.x = 3; d.y = 4;}

}

CS100 Lecture 18 21

public class TrivialApplication {

public static void main(String args[]) {

int x = 1;

int y = 2;

C c = new C(x, y);

c.myMethod(c);

x = c.x; y = c.y;

System.out.println("x = " + x + " y = " + y); }}

public class C {

int x; int y;

public C(int xx, int yy) {x = xx; y = yy;}

public void myMethod(C d) {

x = 3; y = 4;}

}