Cse 252 principles of programming languages lab section
Download
1 / 19

CSE 252 Principles of Programming Languages LAB SECTION - PowerPoint PPT Presentation


  • 65 Views
  • Uploaded on
  • Presentation posted in: General

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha

Download Presentation

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.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


Cse 252 principles of programming languages lab section

CSE 252 Principles of ProgrammingLanguagesLAB SECTION

WEEK 2

Introductionto Java II


Week 2

WEEK 2

  • ReferenceTypeArrays

  • ArrayList

  • Methods

  • PassbyValue (PrimitivesandReferences)


Reference type arrays

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


Cse 252 principles of programming languages lab section

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


Reference type arrays1

ReferenceTypeArrays

String s=“CSE252”;

String[] sArray=newString[4];

sArray[0]=“Kemal”;(1)

sArray[3]=s;(2)

??


Cse 252 principles of programming languages lab section

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


Arrays general

Arrays(General)

Array is a kind of class in Java API.

Themostusefulvariable: length: the size of an array.


Methods

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”);


Methods1

Methods

Themethodforreturning a variableandpassingparametersaresame at a instancemethodand a classmethod.

Herewewilllook at staticmethodsbecausemainmethod can onlycallstaticmethods.

Thecallingprinciple of a method at java is pass-by-value.


Cse 252 principles of programming languages lab section

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;

}

}


Cse 252 principles of programming languages lab section

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;

}

}


Cse 252 principles of programming languages lab section

Example(3):

publicclassFunctionExample {

publicstaticvoidmain(String[] args){

String s=“Kemal”;

write(s);

}

staticvoidwrite(String s){

System.out.println(s);

}

staticintgenerateNum(){

return 5;

}

}


Pass by value in primitive types

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;

}

}


Pass by value reference types

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;

}

}


Exceptions

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.


Exceptions1

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.


Handling exceptions

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.


Handling exceptions1

HandlingExceptions

Example:

int i;

try{

i=scannerObject.nextInt();

}catch(Exception e){

System.out.println(“Youmustenter a number”);

return;

}

System.out.println(i);


ad
  • Login