java lecture 3 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Java Lecture 3 PowerPoint Presentation
Download Presentation
Java Lecture 3

Loading in 2 Seconds...

play fullscreen
1 / 62
adia

Java Lecture 3 - PowerPoint PPT Presentation

91 Views
Download Presentation
Java Lecture 3
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Java Lecture 3 CS 1311 Structure 13X11

  2. Types of Java Programs • Applications • Stand alone • Requires JVM (java.exe, etc.) on your machine • Applets • JVM built in to web browser runs program • Can be downloaded from network or files can be on your machine • Susceptible to browser problems! • Servlets • Like an applet but code runs on server when a particular web page is browsed

  3. Heap (Dynamic Area) (Store stuff here) Stack (Static Area) (Store stuff here) Class Pool (What you wrote) A View of Memory

  4. Recall class CokeMachine { int numCans = 3; void vend() { if(numCans > 0) { System.out.println("Have a Coke!"); numCans = numCans - 1; } else { System.out.println("Sorry!"); } } void load(int n) { numCans = numCans + 1; System.out.println("Ready to vend!"); } public static void main(String args[]) { CokeMachine cc; cc = new CokeMachine(); cc.vend(); cc.vend(); cc.vend(); cc.vend(); cc.load(); cc.vend(); } }

  5. External CokeMachine cc; cc = new CokeMachine(); cc.vend(); cc.load(5); class CokeMachine { int numCans = 3; void vend() { if(numCans > 0) { System.out.println ("Have a Coke!"); numCans = numCans - 1; } else { System.out.println ("Sorry!"); } } void load(int n) { numCans = numCans + 1; System.out.println ("Ready to vend!"); } } Two Views

  6. Could write... class Driver { public static void main(String args[]) { CokeMachine cc; cc = new CokeMachine(); cc.vend(); cc.vend(); cc.vend(); cc.vend(); cc.load(5); } // main } // Simulation javac *.java java Driver

  7. What happened? java Driver Heap Stack Driver main Class Pool

  8. What happened? Java searches for (and finds) static main method Heap Stack Driver main Class Pool

  9. What happened? Java searches for (and finds) static main method Heap Stack main Driver main Class Pool

  10. What happened? CokeMachine cc; Heap Stack main CokeMachine numCans vend() load() Driver main Class Pool

  11. What happened? CokeMachine cc; Heap Stack main cc (CokeMachine) CokeMachine numCans vend() load() Driver main Class Pool

  12. What happened? cc = new CokeMachine(); Obj: CokeMachine numCans vend() load() Heap 3 Stack main cc (CokeMachine) CokeMachine numCans vend() load() Driver main Class Pool

  13. What happened? cc.vend(); Obj: CokeMachine numCans vend() load() Heap 2 Stack main cc (CokeMachine) CokeMachine numCans vend() load() Driver main Class Pool Have a Coke!

  14. What happened? cc.vend(); Obj: CokeMachine numCans vend() load() Heap 1 Stack main cc (CokeMachine) CokeMachine numCans vend() load() Driver main Class Pool Have a Coke! Have a Coke!

  15. What happened? cc.vend(); Obj: CokeMachine numCans vend() load() Heap 0 Stack main cc (CokeMachine) CokeMachine numCans vend() load() Driver main Class Pool Have a Coke! Have a Coke! Have a Coke!

  16. What happened? cc.vend(); Obj: CokeMachine numCans vend() load() Heap 0 Stack main cc (CokeMachine) CokeMachine numCans vend() load() Driver main Class Pool Have a Coke! Have a Coke! Sorry!

  17. As a convenience... • We don't really need the driver class • We can put a static main in the CokeMachine • But the operation is essentially the same! • In fact, you should always put a static main in every class you write • Use it to test the class for correct operation • Will Java get confused?

  18. class A main() class B main() class C main() No Way, Jose! class D main() class E main() class F main() Why?

  19. class A main() class B main() class C main() Because! class D main() class E main() class F main() javac *.java java F

  20. Questions?

  21. Fields • Sometimes called attributes • In our CokeMachine: numCans • Can be • Primitives • References • Access cc.numCans = 42; // Direct access cc.vend(); // Via modifier • Is direct access a "GoodThing"® or a "BadThing"®

  22. Controlling Access • Access to fields (and methods) can be controlled public -- Anyone can access private -- Access is only from within class Recall public static void main(String args[])

  23. Example class CokeMachine { int numCans = 3; double cash = 0.0; static final double PRICE = 0.65; void vend() { if(numCans > 0) { numCans--; cash += PRICE; System.out.println("Have a coke!"); } else { System.out.println("Sorry, no Cokes"); } }

  24. Where's my money?!?!?! Let's Test It! class Tester { public static void main(String args[]) { CokeMachine cm; // Make reference cm = new CokeMachine(); // Make a machine cm.numCans--; // Steal a Coke System.out.println("Cash = " + cm.cash); } }

  25. Let's add some protection class CokeMachine { private int numCans = 3; private double cash = 0.0; private static final double PRICE = 0.65; public void vend() { if(numCans > 0) { numCans--; cash += PRICE; System.out.println("Have a coke!"); } else { System.out.println("Sorry, no Cokes"); } }

  26. class CokeMachine { private int numCans = 3; private double cash = 0.0; private static final double PRICE = 0.65; public void vend() { if(numCans > 0) { numCans--; cash += PRICE; System.out.println("Have a coke!"); } else { System.out.println("Sorry, no Cokes"); } } } class Tester { public static void main(String a[]) { CokeMachine cm = new CokeMachine(); cm.numCans--; } } class CokeMachine { private int numCans = 3; private double cash = 0.0; private static final double PRICE = 0.65; public void vend() { if(numCans > 0) { numCans--; cash += PRICE; System.out.println("Have a coke!"); } else { System.out.println("Sorry, no Cokes"); } } public static void main(String a[]) { CokeMachine cm = new CokeMachine(); cm.numCans--; } } Warning about Testing

  27. Demo

  28. Details • Methods • Module defined in a class which effectively becomes a message that an object of that class can understand cc.vend(); • Send a "vend()" message to the object referenced by cc • Almost all code executed in Java will be executed in methods

  29. Anatomy of a method • <access> <static> <return type> <name>(<parameter list>) { <method body> } • Examples: public static void main(String args[]) { System.out.println("Hello World!"); } void vend(){ /* code goes here */ } private int addEmUp(int a, int b, int c) { return a + b + c; }

  30. Parts is parts • <access> public private • There are others we won't discuss • <static> static nothing which implies dynamic or instance

  31. Parts is parts • <return type> • Methods may optionally return a value • May be any valid type • Class name • Primitive void indicates no return value • If you promise to return something Java will expect you to keep your promise • If you promise to not return anything Java will break your kneecaps with a Louisville Slugger®if you don't keep your promise

  32. A Promise is a Promise public int max(int a, int b) { if(a >= b) { return a; } if(a < b) { return b; } }

  33. A Promise is a Promise public int max(int a, int b) { if(a >= b) { return a; } if(b < a) { return b; } } Classname.java:<lineno>: Return required at end of int max(int, int). public int max(int a, int b)

  34. Method Name... • <name> • Method name follows normal identifier rules • Style note: Start method names with a lower case letter but capitalize multiword names: enqueue vend vendACoke load loadCokes

  35. Parameter List • <parameter list> • Declare each parameter individually • i.e. int a, float f, CokeMachine cm • Java only supports pass by value (in) parameters. public void swap(int a, int b) { /* This method is worthless! */ int t = a; a = b; b = t; }

  36. Parameter List public static void testMachine(int n, CokeMachine cm) { for(int i=0; i < n; i++) { cm.vend(); } n = 0; // Just to be mean cm = null; // and nasty! } • What is the effect on the outside world? • Could this be considered (just maybe) a side effect?

  37. i: cm: 3 Pass by Value class Demo { /* Code for testMachine here*/ public static void Main(String args[]) { int i = 3; CokeMachine cm; cm = new CokeMachine(); testMachine(i, cm); } } Object: CokeMachine numCans = 3 main:

  38. i: cm: 3 Pass by Value class Demo { /* Code for testMachine here*/ public static void Main(String args[]) { int i = 3; CokeMachine cm; cm = new CokeMachine(); testMachine(i, cm); } } n: cm: testMachine: Object: CokeMachine numCans = 3 3 main:

  39. i: cm: 3 Pass by Value public static void testMachine (int n, CokeMachine cm) { for(int i=0; i < n; i++) { cm.vend(); } n = 0; // Just to be mean cm = null; // and nasty! } class Demo { /* Code for testMachine here*/ public static void Main(String args[]) { int i = 3; CokeMachine cm; cm = new CokeMachine(); exerciseMachine(i, cm); } } n: cm: testMachine: Object: CokeMachine numCans = 3 3 main:

  40. i: cm: 3 Pass by Value public static void testMachine (int n, CokeMachine cm) { for(int i=0; i < n; i++) { cm.vend(); } n = 0; // Just to be mean cm = null; // and nasty! } class Demo { /* Code for testMachine here*/ public static void Main(String args[]) { int i = 3; CokeMachine cm; cm = new CokeMachine(); exerciseMachine(i, cm); } } n: cm: testMachine: Object: CokeMachine numCans = 0 3 main:

  41. i: cm: 3 Pass by Value public static void testMachine (int n, CokeMachine cm) { for(int i=0; i < n; i++) { cm.vend(); } n = 0; // Just to be mean cm = null; // and nasty! } class Demo { /* Code for testMachine here*/ public static void Main(String args[]) { int i = 3; CokeMachine cm; cm = new CokeMachine(); exerciseMachine(i, cm); } } n: cm: testMachine: Object: CokeMachine numCans = 0 0 main:

  42. i: cm: 3 Pass by Value public static void testMachine (int n, CokeMachine cm) { for(int i=0; i < n; i++) { cm.vend(); } n = 0; // Just to be mean cm = null; // and nasty! } class Demo { /* Code for testMachine here*/ public static void Main(String args[]) { int i = 3; CokeMachine cm; cm = new CokeMachine(); exerciseMachine(i, cm); } } n: cm: testMachine: Object: CokeMachine numCans = 0 0 main:

  43. i: cm: 3 Pass by Value class Demo { /* Code for testMachine here*/ public static void Main(String args[]) { int i = 3; CokeMachine cm; cm = new CokeMachine(); exerciseMachine(i, cm); } } Object: CokeMachine numCans = 0 main:

  44. Questions?

  45. Sidebar on Methods • Factorial (Yes, again) class FactDemo { public static void main(String args[]) { int n = 10; int i; int answer = 1; for(i = 1; i <= n; i++) { answer *= i; } // for System.out.println(n + "! = " + answer); } // main } // factDemo

  46. Sidebar on Methods • Make it a method public static void fact(int n) { int i; int answer = 1; for(i = 1; i <= n; i++) { answer *= i; } // for System.out.println(n + "! = " + answer); } // fact

  47. Sidebar on Methods • Make it smaller and more efficient? public static void fact(int n) { int i, ans; for(i = 1, ans = 1; i <= n; ans *= i++); System.out.println(n + "! = " + ans); } // fact

  48. Sidebar on Methods • Make it more readable? public static void fact(int n) { int i; int ans = 1; for(i = 1; i <= n; i++) ans *= i; System.out.println(n + "! = " + ans); } // fact

  49. Sidebar on Methods • Make it more useful? public static int fact(int n) { int i; int ans = 1; for(i = 1; i <= n; i++) ans *= i; return ans; } // fact

  50. Sidebar on Methods • Make it more useful? class FactDemo { public static int fact(int n) { int i; int ans = 1; for(i = 1; i <= n; i++) ans *= i; return ans; } // fact public static void main(String args[]) { System.out.println("10! = " + fact(10)); } }