Agenda Review User input Scanner Strong type checking Other flow-control structures switch break &...

25
Advanced Programming in Java

Transcript of Agenda Review User input Scanner Strong type checking Other flow-control structures switch break &...

Page 1: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

Advanced Programming in Java

Page 2: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

2

AgendaReviewUser input

ScannerStrong type checkingOther flow-control structures

switchbreak & continue

StringsArrays

Page 3: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

3

ReviewVariables

Primitive data typesOperatorsMethods

Parameter passingCall by value

ConditionsIf, else, else if

Loopswhiledo-whilefor

Page 4: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

4

User InputPrint on console

System.out.printlnHow to read from console?ScannerExample:

Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();double d = scanner.nextDouble();

Page 5: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

5

Example

Scanner scanner = new Scanner(System.in);int a = scanner.nextInt();int b = scanner.nextInt();long pow = power(a,b);System.out.println(pow);

Page 6: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

6

Type CheckingJava has a strong type-checking mechanismSome assignment is not permitted

int intVal = 2;long longVal =12;

intVal = longVal;Syntax ErrorlongVal = intVal;OKintVal = (int)longVal; OK (Type Casting)

Page 7: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

7

Direct Type ConversionThe arrows are

transitiveAll other conversions

need an explicit castboolean is not

convertiblechar is a special type

byte

char

short

int

long

float

double

boolean

Page 8: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

8

Type Conversion Grid

Page 9: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

9

Type ConversionN : the conversion cannot be performedY : the conversion is performed automatically

and implicitly by JavaC : the conversion is a narrowing conversion

and requires an explicit castY* : the conversion is an automatic widening

conversion, but that some of the least significant digits of the value may be lost by the conversion

Page 10: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

10

Exampleint i = 123456789; //a big integerfloat f = i; //f stores and approximation of iSystem.out.println(f);//output : 1.23456792E8i = (int) f;System.out.println(i); //output : 123456792

floating-point types are approximations of numbers

They cannot always hold as many significant digits as the integer types

Page 11: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

11

Switch statementAn alternative to if-elseBetter structureBefore Java 1.7

can use byte, short, char, and int (note: not long) primitive data types or their corresponding wrapper types

 Starting with J2SE 5.0, it is possible to use enum types

With Java 1.7Strings are also allowed

Page 12: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

12

switch exampleswitch (i) {case 1:

System.out.println("1");break;

case 2:System.out.println("2");break;

default:System.out.println("default");

}

Page 13: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

Scanner scanner = new Scanner(System.in);boolean again = true;while(again){

System.out.println("1: Play");System.out.println("2: Setting:");System.out.println("3: Exit");System.out.print("Enter Your Choice:");int i = scanner.nextInt();switch (i) {case 1:

play();break;

case 2:setting();break;

case 3:again = false;break;

default:System.out.println("Enter a valid number");

}}

Page 14: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

14

BreakBreaks the execution of a loop

while(true){int nextInt = scanner.nextInt();if(nextInt==0)

break;...

}

Page 15: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

15

ContinueStops the execution of the body of the loop

and continues from the beginning of the loop

for(int i=0;i<10;i++){if(i==4)continue;System.out.println(i);

}

Difference between continue in for and while

Page 16: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

16

Nested LoopsScanner scanner = new Scanner (System.in);int nextInt;do{

nextInt = scanner.nextInt();for(int i=0;i<nextInt;i++){

System.out.println(i);}

}while(nextInt>0);

How to break or continue from outer loop?

Page 17: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

17

Label outer: for (int i = 0; i < 10; i++)inner: for (int j = 0; j < 10; j++) {

if (j == 2)break outer;

else {System.out.println(i);System.out.println(j);continue inner;

}}

Page 18: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

18

Tip of the Day: Indentationint nextInt;do{nextInt = scanner.nextInt();for(int i=0;i<nextInt;i++){System.out.println(i);}}while(nextInt>0);

Page 19: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

19

Tip of the Day: Indentationint nextInt;do{

nextInt = scanner.nextInt();for(int i=0;i<nextInt;i++){

System.out.println(i);}

}while(nextInt>0);

Page 20: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

20

CommentsComments are ignored by compilerUsually is used for documentation and

description of the codeOn line comment

//nextInt = scanner.nextInt();Some line comment

/*nextInt = scanner.nextInt();for(int i=0;i<nextInt;i++){

System.out.println(i);}*/

Page 21: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

21

StringA sequence of charactersCharacter:

char ch = ‘a’;char ch = ‘1’;char ch = ‘#’;

Strings:String st = “Ali”;String st = “123”;String st = “1”;String st = “”;

String is not a primitive type

Page 22: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

22

StringString in C and C++

char* and char[]\0 at the end of String

Some functionsstrlen, strcpy, …

String in java is a classString in java is not equal to char[]Constant strings

“salam!”“Hellow World!”

Page 23: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

23

ExampleScanner scanner = new Scanner(System.in);String input;input = scanner.next();switch (input) {case "Salam":

System.out.println("Hi!");break;

case "Khdahafez":System.out.println("Bye!");break;

default:System.out.println("Ha?!");break;

}

System.out.println(input);

Page 24: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

24

Example(2)String input = "Nader and Simin, A Separation";char ch = input.charAt(0);int i = input.indexOf("Nader");int j = input.lastIndexOf("Simin");String newS = input.replace("Separation", "Reconciliation");String sth = newS + ch + i + j;System.out.println(sth);

Page 25: Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

25

String methodscharAtconcat plus (+) operatorcontainsstartsWithendsWithindesxOf first index of sthlastIndexOfreplacesubstringlengthsplit