1、Java Lecture 3,CS 1311Structure,13X11,Types of Java Programs,ApplicationsStand aloneRequires JVM (java.exe, etc.) on your machineAppletsJVM built in to web browser runs programCan be downloaded from network or files can be on your machineSusceptible to browser problems!ServletsLike an applet but cod
2、e runs on server when a particular web page is browsed,A View of Memory,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
3、static void main(String args) CokeMachine cc;cc = new CokeMachine();cc.vend();cc.vend();cc.vend();cc.vend();cc.load();cc.vend();,Recall,Two Views,ExternalCokeMachine cc;cc = new CokeMachine();cc.vend();cc.load(5);,class CokeMachine int numCans = 3; void vend() if(numCans 0) System.out.println (Have
4、a Coke!); numCans = numCans - 1; else System.out.println (Sorry!); void load(int n) numCans = numCans + 1; System.out.println (Ready to vend!); ,javac *.javajava Driver,Could write.,class Driver public static void main(String args) CokeMachine cc;cc = new CokeMachine();cc.vend();cc.vend();cc.vend();
5、cc.vend();cc.load(5); / main / Simulation,What happened?,java Driver,Drivermain,ClassPool,Stack,Heap,What happened?,Java searches for(and finds) staticmain method,ClassPool,Stack,Heap,Drivermain,What happened?,Java searches for(and finds) staticmain method,ClassPool,Stack,Heap,Drivermain,main,What h
6、appened?,CokeMachine cc;,ClassPool,Stack,Heap,Drivermain,main,CokeMachinenumCans vend() load(),What happened?,CokeMachine cc;,ClassPool,Stack,Heap,Drivermain,main cc (CokeMachine),CokeMachinenumCans vend() load(),What happened?,cc = new CokeMachine();,ClassPool,Stack,Heap,Drivermain,main cc (CokeMac
7、hine),CokeMachinenumCans vend() load(),Obj: CokeMachinenumCans vend() load(),3,What happened?,cc.vend();,ClassPool,Stack,Heap,Drivermain,main cc (CokeMachine),CokeMachinenumCans vend() load(),Obj: CokeMachinenumCans vend() load(),2,Have a Coke!,What happened?,cc.vend();,ClassPool,Stack,Heap,Driverma
8、in,main cc (CokeMachine),CokeMachinenumCans vend() load(),Obj: CokeMachinenumCans vend() load(),1,Have a Coke!Have a Coke!,What happened?,cc.vend();,ClassPool,Stack,Heap,Drivermain,main cc (CokeMachine),CokeMachinenumCans vend() load(),Obj: CokeMachinenumCans vend() load(),0,Have a Coke!Have a Coke!
9、Have a Coke!,What happened?,cc.vend();,ClassPool,Stack,Heap,Drivermain,main cc (CokeMachine),CokeMachinenumCans vend() load(),Obj: CokeMachinenumCans vend() load(),0,Have a Coke!Have a Coke!Sorry!,As a convenience.,We dont really need the driver classWe can put a static main in the CokeMachineBut th
10、e operation is essentially the same!In fact, you should always put a static main in every class you writeUse it to test the class for correct operationWill Java get confused?,No Way, Jose!,class Dmain(),class Emain(),class Fmain(),Why?,Because!,class Dmain(),class Emain(),class Fmain(),javac *.javaj
11、ava F,Questions?,Fields,Sometimes called attributesIn our CokeMachine: numCansCan bePrimitivesReferencesAccesscc.numCans = 42; / Direct access cc.vend(); / Via modifierIs direct access a GoodThing or a BadThing,Controlling Access,Access to fields (and methods) can be controlledpublic - Anyone can ac
12、cessprivate - Access is only from within classRecallpublic static void main(String args),Example,class CokeMachineint 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 Co
13、kes);,Lets Test It!,class Testerpublic static void main(String args)CokeMachine cm;/ Make referencecm = new CokeMachine(); / Make a machinecm.numCans-;/ Steal a CokeSystem.out.println(Cash = + cm.cash);,Wheres my money?!?!?!,Lets add some protection,class CokeMachineprivate int numCans = 3;private d
14、ouble 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);,Warning about Testing,class CokeMachineprivate int numCans = 3;private double cash = 0.0;private static final
15、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 CokeMachineprivate int numCans = 3;private doubl
16、e 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-;,Demo,Details,MethodsModule define
17、d in a class which effectively becomes a message that an object of that class can understandcc.vend();Send a vend() message to the object referenced by ccAlmost all code executed in Java will be executed in methods,Anatomy of a method, () Examples:public static void main(String args)System.out.print
18、ln(Hello World!);void vend() /* code goes here */ private int addEmUp(int a, int b, int c) return a + b + c;,Parts is parts,publicprivateThere are others we wont discussstaticnothing which implies dynamic or instance,Parts is parts,Methods may optionally return a valueMay be any valid typeClass name
19、Primitivevoid indicates no return valueIf you promise to return something Java will expect you to keep your promiseIf you promise to not return anything Java will break your kneecaps with a Louisville Slugger if you dont keep your promise,A Promise is a Promise,public int max(int a, int b)if(a = b)r
20、eturn a;if(a = b)return a;if(b a)return b;,Classname.java: Return required at end of int max(int, int). public int max(int a, int b),Method Name.,Method name follows normal identifier rulesStyle note: Start method names with a lower case letter but capitalize multiword names:enqueuevendvendACokeload
21、loadCokes,Parameter List,Declare each parameter individuallyi.e. int a, float f, CokeMachine cmJava 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;,Parameter List,public static void testMachine(int n, CokeMachine cm)for
22、(int i=0; i n; i+)cm.vend();n = 0; / Just to be meancm = null; / and nasty!,What is the effect on the outside world?Could this be considered (just maybe) a side effect?,Pass by Value,class Demo /* Code for testMachine here*/public static void Main(String args) int i = 3;CokeMachine cm;cm = new CokeM
23、achine();testMachine(i, cm);,Object: CokeMachinenumCans = 3,main:,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: CokeMachinenumCans = 3,main:,testMachine:,n: cm:,3,Pass by Value,c
24、lass Demo /* Code for testMachine here*/public static void Main(String args) int i = 3;CokeMachine cm;cm = new CokeMachine();exerciseMachine(i, cm);,Object: CokeMachinenumCans = 3,main:,testMachine:,n: cm:,3,public static void testMachine (int n, CokeMachine cm) for(int i=0; i n; i+) cm.vend();n = 0
25、; / Just to be meancm = null; / and nasty!,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: CokeMachinenumCans = 0,main:,testMachine:,n: cm:,3,public static void testMachine (in
26、t n, CokeMachine cm) for(int i=0; i n; i+) cm.vend();n = 0; / Just to be meancm = null; / and nasty!,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: CokeMachinenumCans = 0,main
27、:,testMachine:,n: cm:,0,public static void testMachine (int n, CokeMachine cm) for(int i=0; i n; i+) cm.vend();n = 0; / Just to be meancm = null; / and nasty!,Pass by Value,class Demo /* Code for testMachine here*/public static void Main(String args) int i = 3;CokeMachine cm;cm = new CokeMachine();e
28、xerciseMachine(i, cm);,Object: CokeMachinenumCans = 0,main:,testMachine:,n: cm:,0,public static void testMachine (int n, CokeMachine cm) for(int i=0; i n; i+) cm.vend();n = 0; / Just to be meancm = null; / and nasty!,Pass by Value,class Demo /* Code for testMachine here*/public static void Main(Stri
29、ng args) int i = 3;CokeMachine cm;cm = new CokeMachine();exerciseMachine(i, cm);,Object: CokeMachinenumCans = 0,main:,Questions?,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; / forSyste
30、m.out.println(n + ! = + answer); / main / factDemo,Sidebar on Methods,Make it a methodpublic static void fact(int n) int i;int answer = 1; for(i = 1; i = n; i+) answer *= i; / forSystem.out.println(n + ! = + answer); / fact,Sidebar on Methods,Make it smaller and more efficient?public static void fac
31、t(int n) int i, ans;for(i = 1, ans = 1; i = n; ans *= i+);System.out.println(n + ! = + ans); / fact,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,Sidebar on Methods,Make it more useful
32、?public static int fact(int n) int i;int ans = 1;for(i = 1; i = n; i+)ans *= i;return ans; / fact,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; / factpublic static void main(String args) System.out.prin
33、tln(10! = + fact(10);,Sidebar on Methods,Do it recursively!class FactDemo public static int fact(int n) if(n = 0)return 1;elsereturn n * fact(n - 1); / factpublic static void main(String args) System.out.println(10! = + fact(10);,Sidebar on Methods,Do it recursively!class FactDemo public static int
34、fact(int n) if(n = 0)return 1;elsereturn n * fact(n - 1); / factpublic static void main(String args) for(int i = 1; i 18; i+)System.out.println(i + ! = + fact(i);,ShakyResults,1! = 12! = 23! = 64! = 245! = 1206! = 7207! = 50408! = 403209! = 36288010! = 362880011! = 3991680012! = 47900160013! = 19320
35、5350414! = 127894528015! = 200431001616! = 200418918417! = -288522240,Primitive Data Type Ranges,Type,Size,Min,Default,boolean,false,Max,1,false*,true*,char,u0000 (null),16,byte,(byte) 0,8,-128,127,short,(short) 0,16,-32,768,32,767,int,0,32,-2,147,483,648,2,147,483,647,long,0L,64,-9,223,372,036,854,
36、775,808,9,223,372,036,854,775,807,float,0.0F,32,Approx 3.4E+38 with 7 significant digits,double,0.0D,64,Approx 1.7E+308 with 15 significant digits,void,* Not truly min and max.,Notice!,Notice how Java gave you a helpful warning message to help keep you spacecraft from plunging into the Sun NOT.,Side
37、bar on Methods,Fix the problem (forever?)!class FactDemo public static long fact(long n) if(n = 0)return 1;elsereturn n * fact(n - 1); / factpublic static void main(String args) for(int i = 1; i 18; i+)System.out.println(i + ! = + fact(i);,Pop Quiz,class Widgetint a, b;public int sum() return a + b;
38、public static void main(String a) a = 5;b = 10;System.out.println(sum();,Okay?,Pop Quiz,class Widgetint a, b;public int sum() return a + b;public static void main(String a) Widget w;w = new Widget();w.a = 5;w.b = 10;System.out.println(w.sum();,Questions?,Final Thought,Just what is a class?,class,declarations,initializationmodules*,methods,Questions?,