1 / 10

Basics and arrays

Basics and arrays. Operators: Arithmetic: +, -, *, /, % Relational: <, <=, >, >=, ==, != Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32), long(64), float(32), double(64), boolean Numeric type conversion float f = (float)10; int I = (int)f;. Control structure.

dutch
Download Presentation

Basics and arrays

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. Basics and arrays • Operators: • Arithmetic: +, -, *, /, % • Relational: <, <=, >, >=, ==, != • Logical: &&, ||, ! • Primitive data types • Byte(8), short(16), int(32), long(64), float(32), double(64), boolean • Numeric type conversion • float f = (float)10; • int I = (int)f;

  2. Control structure • Selection if( condition ) { statements; } else { statements; } Switch( integer) { Case 1: statements; Case 2: statements; … Default: .. } • Repetition • while( condition) { … } • do { • …. • }while(condition); • for ( x = 0; x < 10; x++) • { • statements; • }

  3. String type: String • String is a predefined class in the Java library • It is not primitive data type • It is known as a reference type • Declare a string • String message = “welcome!”; • Concatenate strings • String s = “Chapter”+2; • message +=“ and Java is fun”; • System.out.println(“i+j is” +(i+j)); // i=1, j = 2

  4. Getting input from input Dialogs • String input = JOptionPane.showInputDialog(null, “Enter an input”, “Input Dialog memo”, JOptionPane.QUESTION_MESSAGE); • JOptionPane.showMessageDialog(null, input, “Example”, JOptionPane.INFORMATION_MESSAGE); • Converting string to numbers • int val = Integer.parseInt(input); • double d = Double.parseDouble(input);

  5. Methods • Creating a method: • modifier returntype methodname(list of parameters) • { • } • public static int max( int int1, int int2) • { • if( int1 > int2) return int1; • else retun int2; • } • primitive data are passed by value ( No Pointer!)

  6. Calling a method • int larger = max(3,4); • System.out.println(max(3,4)); • Static method can be called directly, it doesn’t need a class object to call it • In the same class, it can be called by the method name • In other class definition, it can be called by ClassName.max(3,4);

  7. The Math class • public static double sin(double x) • public static double cos( double x) • public static double exp( double a) • public static double log( double a) • public static double pow( double a, double b) • public static double sqrt( double a) • Min, max, abs and random methods

  8. The random methed • Math.random() generate a random double number 0<= Math.random <1.0 • Generate random number between 0 and 9 • int I = (int)(Math.random()*10); • Generate number between 50 and 99 • Int I = 50 + (int)(Math.random()*50); • In genral, generate any random number between a and a+b, excluding a+b • a + Math.random()*b

  9. Generate random characters • Every character has a unique Unicode between 0 and FFFF in hexadecimal (65535) in decimal • Generate random integer between 0 – 65535 • (int) (Math.random()*(65535+1) • Generate random lower case letter • Unicode for a (int) ‘a’ • Unicode for z is (int) ‘z’ • Random letter between a-z • (char) (‘a’ + Math.random()*(‘z’ =‘a’ +1))

  10. Guess a number • Write a program that generate a random number between 0-100 • Ask user to guess it, • Print message that state either the number user guess is higher, lower • Repeat until user enter the correct nmber.

More Related