Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.

Similar presentations


Presentation on theme: "OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous."— Presentation transcript:

1 OOP (pre) Basic Programming

2 Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous print println will display string to the screen with the newline character adding at the end of string public class HelloWorld { public static void main(String args[ ]) { System.out.println(“Hello World!”); } System.out.print(“string to be printed”); or System.out.println(“string to be printed”);

3 Primitive Data Types in the Java TypeDescription booleantrue or false. byte8-bit two’s-complement integer with values between –2 7 and 2 7 –1 (–128 to 127) short16-bit twos-complement integer with values between –2 15 and 2 15 –1 (–32,768 to 32,767) char16-bit Unicode characters from \u0000 to \uFFFF. For alpha-numerics, these are the same as ASCII with the high byte set to 0 int32-bit two’s-complement integer with values between –2 31 and 2 31 –1 (–2,147,483,648 to 2,147,483,647) long64-bit twos-complement integer with values between –2 63 and 2 63 –1 (–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) float32-bit single precision floating-point numbers using the IEEE 754-1985 standard (+/– about 10 39 ). double64-bit double precision floating-point numbers using the IEEE 754-1985 standard (+/– about 10 317 ).

4 Reading from Keyboard String str1 = “”; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); str1 = in.readLine(); } catch(IOException e) { System.out.println(e); } System.out.println(“You enter “ + str1);

5 Reading from KB using Scanner Class import java.util.Scanner; public class cond { public static void main(String[] args) { System.out.println("Enter your number:"); Scanner sc = new Scanner(System.in); int x = sc.nextInt(); sc.close(); System.out.println(“x = “ + x); }

6 Parsing String to int String str1 = ""; int x = 0; Scanner sc = new Scanner(System.in); str1 = sc.nextString(); sc.close(); x = Integer.parseInt(str1); System.out.println(“You enter “ + x);

7 Operations on Boolean Expressions OperationName =Assignment ==Equality !=Inequality !Logical NOT &AND |OR ^XOR &&Logical AND ||Logical OR ?:if-then-else

8 Relational Operators OperatorBoolean Result <Less than <=Less than or equal to >Greater than >=Greater than or equal to public class QuickTest { public static void main(String args[ ] ) { System.out.println(“5 is greater than 6: ” + (5>6)); System.out.println(“6 is greater than or equal to 3: ” + (6>=3)); System.out.println(“8 is less than 10: ” + (8<10)); }

9 if Statement expression is evaluated to be boolean value: true, false –If it is true, the following statement will be executed –If it is false, the following statement will be skipped if (expression) statement; int score = 55; //try to play with this value if (score >= 50) System.out.println(“ Pass ”); System.out.println(“ Fail ”); Note: Let’s see what would happen to this code.

10 If-else Statement expression is evaluated to be boolean value: true, false –If it is true, statement 1 will be executed –If it is false, statement 2 will be executed if (expression) statement1; else statement2; int score = 55; //try to play with this value if (score >= 50) System.out.println(“ Pass ”); else System.out.println(“ Fail ”); Note: Now what happens.

11 Conditional Operator expression1 must evaluate to a Boolean value –If it is true, then expression2 is evaluated –If it is false, then expression3 is evaluated expression1 ? expression2 : expression3; int score = 55; System.out.println((score>=50) ? “ Pass ” : “ Fail ”); int score = 55; String result = (score>=50) ? “Pass” : “Fail”; System.out.println(result); or

12 switch Statements Control expression that drives a switch statement and the label values for each case must evaluate to a –byte, short, char, or int switch (expression) { case C1: statement1; break; case C2: statement2; break; default: statementD; }

13 switch Statements (cont) What happens if we do not have “break” switch (1) { case 1: System.out.println ("one"); case 2: System.out.println (“two"); default: System.out.println (“Default"); }

14 switch Statements (cont) What happens if we do not have “break” switch (1) { case 1: System.out.println ("one"); break; case 2: System.out.println (“two"); break; default: System.out.println (“Default"); }

15 switch Statements (cont) Notice that –blocks are not required for the statements associated with each case because a label identifies the start of each one and a break statement marks the end. switch (strike) { case 0: case 1: System.out.println("Got a 0 or 1, want to do the same thing"); break; case 2: System.out.println("Got a 2, do something different"); break; default: System.out.println("Value out of range"); }


Download ppt "OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous."

Similar presentations


Ads by Google