agenda review user input scanner strong type checking other flow-control structures switch break...
TRANSCRIPT
Advanced Programming in Java
2
AgendaReviewUser input
ScannerStrong type checkingOther flow-control structures
switchbreak & continue
StringsArrays
3
ReviewVariables
Primitive data typesOperatorsMethods
Parameter passingCall by value
ConditionsIf, else, else if
Loopswhiledo-whilefor
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();
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);
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)
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
8
Type Conversion Grid
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
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
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
12
switch exampleswitch (i) {case 1:
System.out.println("1");break;
case 2:System.out.println("2");break;
default:System.out.println("default");
}
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");
}}
14
BreakBreaks the execution of a loop
while(true){int nextInt = scanner.nextInt();if(nextInt==0)
break;...
}
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
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?
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;
}}
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);
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);
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);}*/
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
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!”
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);
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);
25
String methodscharAtconcat plus (+) operatorcontainsstartsWithendsWithindesxOf first index of sthlastIndexOfreplacesubstringlengthsplit