Announcements P 4 due tomorrow P 5 handed

  • Slides: 21
Download presentation
Announcements • P 4 due tomorrow • P 5 handed out today • References?

Announcements • P 4 due tomorrow • P 5 handed out today • References? Leave me a picture and a short note CS 100 Lecture 18 1

Today’s Topics • Pascal’s Triangle (fun with 2 D arrays) • Applets • Parameter

Today’s Topics • Pascal’s Triangle (fun with 2 D arrays) • Applets • Parameter passing -- Examples galore! CS 100 Lecture 18 2

Pascal’s Triangle 1 1 1 Note: Elements of an array can be 1 2

Pascal’s Triangle 1 1 1 Note: Elements of an array can be 1 2 1 arrays of different lengths! 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 CS 100 Lecture 18 3

Compute Pascal’s Triangle // Yield Pascal's triangle with size rows public static int[][] calculate.

Compute Pascal’s Triangle // Yield Pascal's triangle with size rows public static int[][] calculate. Pascal(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; CS 100 Lecture 18 } 4

Java and World Wide Web HTML is the language in which files (or pages)

Java and World Wide Web HTML 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> CS 100 Lecture 18 5

HTML is a markup language • <command> begins a command, </command> ends it. •

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 CS 100 Lecture 18 6

Applets in html Command applet: <applet archive=Applet. Classes. jar code=“class. Name. class” width=200 height=400

Applets in html Command applet: <applet archive=Applet. Classes. jar code=“class. Name. 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 Applet. Classes. jar. The class to begin execution is in file class. Name. 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” CS 100 Lecture 18 7

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

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. CS 100 Lecture 18 8

Example: import java. awt. *; import java. applet. Applet; public class Trivial. Applet extends

Example: import java. awt. *; import java. applet. Applet; public class Trivial. Applet extends Applet { public void init() { repaint(); } public void paint( Graphics g ) { g. draw. String( "Hello World!", 10 ); g. draw. String("Parking", 50); g. draw. Oval(45, 24, 43); g. draw. Line(82, 30, 51, 61); }} CS 100 Lecture 18 9

Html for this applet: <title>Trivial. Applet</title> <hr> <applet archive="Applet. Classes. jar" code="Trivial. Applet. class"

Html for this applet: <title>Trivial. Applet</title> <hr> <applet archive="Applet. Classes. jar" code="Trivial. Applet. class" width=200 height=200> </applet> <hr> <a href="Trivial. Applet. java">The source. </a> CS 100 Lecture 18 10

CS 100 Lecture 18 11

CS 100 Lecture 18 11

Graphics • Applets are most useful if you employ graphics • Check out the

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. ) when you’re writing applets - this was just a trivial example. CS 100 Lecture 18 12

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

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 CS 100 Lecture 18 MAKE THE SPACE ASSIGN (COPY!) ARGUMENTS TO PARAMETERS RUN THE METHOD DELETE FRAME RETURN VALUE 13

Parameter-Passing Redux public class Trivial. Application { public static void main(String args[]) { int

Parameter-Passing Redux public class Trivial. Application { public static void main(String args[]) { int x = 1; int y = 2; C c = new C(); c. my. Method(x, y); System. out. println("x = " + x + " y = " + y); } public class C { public void my. Method(int a, int b) { a = 3; b = 4; } Lecture 18 } CS 100 } 14

Names and boxes public class Trivial. Application { public static void main(String args[]) {

Names and boxes public class Trivial. Application { public static void main(String args[]) { int x = 1; int y = 2; C c = new C(); c. my. Method(x, y); System. out. println("x = " + x + " y = " + y); } public class C { public void my. Method(int x, int y) { x = 3; CS 100 y = 4; } Lecture 18 } } 15

Names and boxes, 2 public class Trivial. Application { public static void main(String args[])

Names and boxes, 2 public class Trivial. Application { public static void main(String args[]) { int x = 1; int y = 2; C c = new C(); c. my. Method(x, y); System. out. println("x = " + x + " y = " + y); } public class C { public void my. Method(int y, int x) { x = 3; y = 4; } Lecture 18 } CS 100 } 16

Complications public class Trivial. Application { public static void main(String args[]) { int x

Complications public class Trivial. Application { public static void main(String args[]) { int x = 1; int y = 2; C c = new C(x, y); c. my. Method(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 my. Method(int a, int b) { a = 3; b = 4; } } CS 100 Lecture 18 }} 17

Complications, 2 public class Trivial. Application { public static void main(String args[]) { int

Complications, 2 public class Trivial. Application { public static void main(String args[]) { int x = 1; int y = 2; C c = new C(x, y); c. my. Method(); 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 my. Method() { x = 3; y = 4; } CS 100 Lecture 18 } }} 18

Complications, 3 public class Trivial. Application { public static void main(String args[]) { int

Complications, 3 public class Trivial. Application { public static void main(String args[]) { int x = 1; int y = 2; C c = new C(); c. my. Method(x, x); System. out. println("x = " + x + " y = " + y); } public class C { public void my. Method(int y, int x) { x = 3; y = 4; } Lecture 18 } CS 100 } 19

. . . public class Trivial. Application { public static void main(String args[]) {

. . . public class Trivial. Application { public static void main(String args[]) { int x = 1; int y = 2; C c = new C(x, y); c. my. Method(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 my. Method(C d) { d. x = 3; d. y = 4; } CS 100 Lecture 18 } }} 20

public class Trivial. Application { public static void main(String args[]) { int x =

public class Trivial. Application { public static void main(String args[]) { int x = 1; int y = 2; C c = new C(x, y); c. my. Method(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 my. Method(C d) { x = 3; y = 4; } } CS 100 Lecture 18 }} 21