Advanced Programming in Java

52
Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Spring 2014

description

Advanced Programming in Java. Peyman Dodangeh Sharif University of Technology Spring 2014. Agenda. Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays. Review. Variables Primitive data types Operators Methods - PowerPoint PPT Presentation

Transcript of Advanced Programming in Java

Advanced Programming in Java

Advanced Programming in JavaPeyman DodangehSharif University of TechnologySpring 2014AgendaReviewUser inputScannerStrong type checkingOther flow-control structuresswitchbreak & continueStringsArrays

Spring 2014Sharif University of Technology2ReviewVariablesPrimitive data typesOperatorsMethodsParameter passingCall by valueConditionsIf, else, else ifLoopswhiledo-whileforSpring 2014Sharif University of Technology3User InputPrint on consoleSystem.out.printlnHow to read from console?ScannerExample:Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();double d = scanner.nextDouble();

Spring 2014Sharif University of Technology4ExampleScanner scanner = new Scanner(System.in);int a = scanner.nextInt();int b = scanner.nextInt();long pow = power(a,b);System.out.println(pow);Spring 2014Sharif University of Technology5Type 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)Spring 2014Sharif University of Technology6Direct Type ConversionThe arrows are transitiveAll other conversions need an explicit castboolean is not convertiblechar is a special typeSpring 2014Sharif University of Technology7bytecharshortintlongfloatdoublebooleanType Conversion GridSpring 2014Sharif University of Technology8

Reference: http://docstore.mik.ua/orelly/java-ent/jnut/ch02_04.htm#javanut3-ch-2-tab-4

8Type ConversionSpring 2014Sharif University of Technology9N : 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

9Examplei = 123456789; //a big integerf = i; //f stores an 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 typesSpring 2014Sharif University of Technology1010Floating Point, Some NotesDouble.NaNdouble nan = 0.0/0.0;Infinitydouble inf = Double.MAX_VALUE*2;Negative infinitydouble inf = Double.MAX_VALUE*(-2);Double.NEGATIVE_INFINITYDouble.POSITIVE_INFINITYFormatting a doubleSystem.out.format("min double = %5.2f%n", ff);ComparisonCompare doublesUsing == with float or double is an anti-patternAn infinite loop:for (float f = 10f; f != 0; f -= 0.1) {System.out.println(f);}

Spring 2014Sharif University of Technology12Numeric AssignmentsNumeric SuffixDouble d = 123.54d;Float f = 123f;Long l = 123123 l; byte b = 127;//NothingAssignment OverflowLarge long to intLower bits are usedNo runtime errorLarge double to integerBrings a max intSwitch statementAn alternative to if-elseBetter structureBefore Java 1.7When the condition is a numeric or an ordinal variableWith Java 1.7Strings are also allowedSpring 2014Sharif University of Technology14switch exampleswitch (i) {case 1:System.out.println("1");break;case 2:System.out.println("2");break;default:System.out.println("default");}

Spring 2014Sharif University of Technology15Scanner 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");}}Spring 2014Sharif University of Technology16BreakBreaks the execution of a loop

while(true){int nextInt = scanner.nextInt();if(nextInt==0)break;...}Spring 2014Sharif University of Technology17ContinueStops the execution of the body of the loop and continues from the beginning of the loop

for(int i=0;i