1 / 19

CSE 252 Principles of Programming Languages LAB SECTION

CSE 252 Principles of Programming Languages LAB SECTION. WEEK 2 Introduction to Java II. WEEK 2. Reference Type Arrays ArrayList Methods Pass by Value ( Primitives and References ). Reference Type Arrays.

acacia
Télécharger la présentation

CSE 252 Principles of Programming Languages LAB SECTION

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 252 Principles of ProgrammingLanguagesLAB SECTION WEEK 2 Introductionto Java II

  2. WEEK 2 • ReferenceTypeArrays • ArrayList • Methods • PassbyValue (PrimitivesandReferences)

  3. ReferenceTypeArrays Referencetypearraysarereferencetypes of collection of referencetypes. String[] sArray = newString[4]; (1)(2) sArray[0]=newString(“Kemal”);//aliastosArray[0]=“Kemal”; //sArray[1],sArray[2],sArray[3]  null

  4. sArray[0]=newString(“Kemal”);

  5. ReferenceTypeArrays String s=“CSE252”; String[] sArray=newString[4]; sArray[0]=“Kemal”;(1) sArray[3]=s;(2) ??

  6. String[] sArray = newString[4]; (1)(2)

  7. Arrays(General) Array is a kind of class in Java API. Themostusefulvariable: length: the size of an array.

  8. Methods Functions in java. • InstanceMethods: An instance method is a method which is associated with one object and uses the variables of that object. int i=scannerObject.nextInt(); // nextInt is instancemethod. • StaticMethods: Themethodwhich is associatedwith a class, not an object. System.out.println(“StaticMethod”);

  9. Methods Themethodforreturning a variableandpassingparametersaresame at a instancemethodand a classmethod. Herewewilllook at staticmethodsbecausemainmethod can onlycallstaticmethods. Thecallingprinciple of a method at java is pass-by-value.

  10. Example(1) publicclassFunctionExample { publicstaticvoidmain(String[] args){ int a=max(4,15); System.out.println(a); } staticintmax(int a,int b){ return a>b?a:b; } }

  11. Example(2) publicclassFunctionExample { publicstaticvoidmain(String[] args){ write(“CSE 252”); write(generateNum()); intnum=generateNum(); System.out.println(num); } staticvoidwrite(String s){ System.out.println(s); } staticintgenerateNum(){ return 5; } }

  12. Example(3): publicclassFunctionExample { publicstaticvoidmain(String[] args){ String s=“Kemal”; write(s); } staticvoidwrite(String s){ System.out.println(s); } staticintgenerateNum(){ return 5; } }

  13. Pass-by-value (Inprimitivetypes) Example (4): publicclassFunctionExample { publicstaticvoidmain(String[] args){ int a=90; int b=80; swap(a,b); // not swapped !!! System.out.println(a + “ “ + b); } staticvoid swap(int a,int b){ inttmp=a; a=b; b=tmp; } }

  14. Pass-by-value (ReferenceTypes) publicclassFunctionExample { publicstaticvoidmain(String[] args){ int[] intArray=createArray(); System.out.println(intArray.length); // hereintArray.length is 5 System.out.println(intArray[0]); // hereintArray[0] is 9 } staticint[] createArray(){ int[] arrayInMethod =newint[5]; arrayInMethod[0]=9; returnarrayInMethod; } }

  15. Exceptions Exceptions -> runtimeerrors. • Exceptionscannot be catched at compilation time • It can occurwhenthe program is running. • Normal flow of instructions can be distruptedwith an exception. • When a program violates the semantic constraints of the Java programming language, the Java virtual machine signals this error to the program as an exception.  An example of such a violation is an attempt to index outside the bounds of an array.

  16. Exceptions Example: int a=scannerObj.nextInt(); thismethodwillwaitforuserentrancefromkeyboardand it alsoacceptusertoenter a numberliteral (3,-2,4,5009,-80,90.3). Whenuserenters an nonnumberliterallike 3AA4,Kemal,CSE252,443AA, a violationforsemanticsoccursand an exceptionwill be thrown.

  17. HandlingExceptions Exceptionsmust be handled. If an occurredexception is not handledbythe program, theexecution of program will be terminatedwith an exceptionmessage. Thisstatement can causefatalsituationswhenexecuting I/O systemsorlow-levelsystems. Exceptionswill be handledwithtrycatchstatements.

  18. HandlingExceptions Example: int i; try{ i=scannerObject.nextInt(); }catch(Exception e){ System.out.println(“Youmustenter a number”); return; } System.out.println(i);

More Related