Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS100Lecture 181 Announcements P4 due tomorrow P5 handed out today References? Leave me a picture and a short note."— Presentation transcript:

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

2 CS100Lecture 182 Today’s Topics Pascal’s Triangle (fun with 2D arrays) Applets Parameter passing -- Examples galore!

3 CS100Lecture 183 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 be arrays of different lengths!

4 CS100Lecture 184 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; }

5 CS100Lecture 185 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: This goes at the top of the page This is a new paragraph. this is in italics. This is in bold but this isn’t. A new paragraph in a larger font

6 CS100Lecture 186 HTML is a markup language begins a command, ends it. There are commands for paragraphs, new line, font change, font-style change, lists, tables, and many more. Cornell’s page produces a link on the page: Cornell’s page

7 CS100Lecture 187 Applets in html Command applet: <applet archive=AppletClasses.jar code=“className.class” width=200 height=400 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”

8 CS100Lecture 188 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.

9 CS100Lecture 189 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); }}

10 CS100Lecture 1810 Html for this applet: TrivialApplet The source.

11 CS100Lecture 1811

12 CS100Lecture 1812 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.

13 CS100Lecture 1813 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 TO PARAMETERS RUN THE METHOD DELETE FRAME RETURN VALUE

14 CS100Lecture 1814 Parameter-Passing Redux public 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;} }

15 CS100Lecture 1815 Names and boxes public 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;} }

16 CS100Lecture 1816 Names and boxes, 2 public 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;} }

17 CS100Lecture 1817 Complications 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.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;} }

18 CS100Lecture 1818 Complications, 2 public 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;} }

19 CS100Lecture 1819 Complications, 3 public 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;} }

20 CS100Lecture 1820... 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;} }

21 CS100Lecture 1821 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;} }


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

Similar presentations


Ads by Google