Hello java This program prints Hello World on

  • Slides: 45
Download presentation

 תוכנית ראשונה שימו לב לסימני. Hello. java הקלידו את התוכנית הבאה בקובץ .

תוכנית ראשונה שימו לב לסימני. Hello. java הקלידו את התוכנית הבאה בקובץ . ואותיות גדולות וקטנות , פיסוק /* This program prints "Hello World!" on the screen */ public class Hello { public static void main(String[] args) { System. out. println("Hello World!"); } } 4

 טבלת סיכום טיפוסים ערכים bytes מספר ה טיפוס -172 to -27 1 byte

טבלת סיכום טיפוסים ערכים bytes מספר ה טיפוס -172 to -27 1 byte -1152 to -215 2 short -1312 to -231 4 int -1632 to -263 8 long Unicode chars 2 char from 1 bit to 1 byte depending on padding boolean from 1. 40129846432481707 e-45 to 3. 40282346638528860 e+38 (positive or negative) 4 float 4. 94065645841246544 e-324 d to 1. 79769313486231570 e+308 d (positive or negative) 8 double true, false 20

1 דוגמה /* This program demonstrates addition and multiplication between integers. */ public class

1 דוגמה /* This program demonstrates addition and multiplication between integers. */ public class Operators. Example 1{ public static void main(String[] args){ int a = 3, b = 5; int c; c = a + b; System. out. println("c = "+ c); c = c * 2; System. out. println("c = "+ c); } } c=8 c = 16 23

2 דוגמה : התוכנית הבאה מדגימה את סדר הפעולות של הפעולות האריתמטיות public class

2 דוגמה : התוכנית הבאה מדגימה את סדר הפעולות של הפעולות האריתמטיות public class Operators. Example { public static void main(String[] args) { int a = 18; int b = 42; int first = (a + b) * 2; int second = a + b * 2; System. out. println("first number is: " + first); System. out. println("second number is: " + second); } } first number is: 120 second number is: 102 24

 פתרון import java. util. Scanner; /* This program accepts a 3 -digit number

פתרון import java. util. Scanner; /* This program accepts a 3 -digit number from the user * reverses the digits and prints the result */ public class Reverse { public static void main(String[] args) { // Read a number from the user. Scanner sc = new Scanner(System. in); System. out. print("Enter a 3 -digit number: "); int num = sc. next. Int(); // divide the number into ones, tens and hundreds. int ones = num % 10; int tens = (num % 100) / 10; int hundreds = num / 100; // calculate the reverse number int reverse. Num = (ones * 100) + (tens * 10) + hundreds; System. out. println("The reverse number is " + reverse. Num); } } Enter a 3 -digit number: 951 The reverse number is 159 26

 אופרטורים יחסיים אופרטורים המשווים בין שני מספרים ונותנים תשובה 6 יש Java ב

אופרטורים יחסיים אופרטורים המשווים בין שני מספרים ונותנים תשובה 6 יש Java ב . (false או true) בוליאנית Operator Name Description x<y Less than true if x is less than y, otherwise false. x>y Greater than true if x is greater than y, otherwise false. x <= y Less than or equal to true if x is less than or equal to y, otherwise false. x >= y Greater than or equal to true if x is greater than or equal to y, otherwise false. x == y Equal true if x equals y, otherwise false. x != y Not Equal true if x is not equal to y, otherwise false. 27

 דוגמה import java. util. Scanner; // This program compares two numbers with relational

דוגמה import java. util. Scanner; // This program compares two numbers with relational operators public class Relational. Operators { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter the first number: "); int x = sc. next. Int(); System. out. print("Enter the second number: "); int y = sc. next. Int(); System. out. println("x<y is " + (x < y)); System. out. println("x>y is " + (x > y)); System. out. println("x<=y is " + (x <= y)); System. out. println("x>=y is " + (x >= y)); System. out. println("x==y is " + (x == y)); System. out. println("x!=y is " + (x != y)); } } Enter the first number: 10 Enter the second number: 20 x<y is true x>y is false x<=y is true x>=y is false x==y is false x!=y is true 28

 אופרטורים לוגיים פועלים על ערכים מטיפוס לוגי )בוליאני( וגם . (false או true)

אופרטורים לוגיים פועלים על ערכים מטיפוס לוגי )בוליאני( וגם . (false או true) נותנים תשובה בוליאנית Operator Name Description x && y And True if both x and y are true, otherwise false. x || y Or True if at least one of x or y are true, otherwise false. !x Not True if x is false, otherwise false. . מתבצעת משמאל לימין Or ו And * הערכת האופרטורים הלוגיים 29

 דוגמה import java. util. Scanner; // This program demonstrates logical operators. // It

דוגמה import java. util. Scanner; // This program demonstrates logical operators. // It reads two integers from the user and checks if // they are larger than 10. public class Logical. Operators { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter the first number: "); int x = sc. next. Int(); System. out. print("Enter the second number: "); int y = sc. next. Int(); System. out. println("(y<10)&&(x<10) is " + ((y<10) && (x<10))); System. out. println("(y<10)||(x<10) is " + ((y<10) || (x<10))); boolean state; state = ((y < 10) || (x < 10)); System. out. println("state is " + state); } } Enter the first number: 10 Enter the second number: 9 (y<10)&&(x<10) is false (y<10)||(x<10) is true state is true 30

 דוגמה public class Use. Math { public static void main(String[] args){ // This

דוגמה public class Use. Math { public static void main(String[] args){ // This is an example of using Math methods double x = Math. abs(-3); x = Math. pow(x, 2 ); x = Math. max(x, Math. PI); System. out. println("max( (|-3|)^2 , Pi ) = " + x); x = Math. random(); System. out. println("A random number between 0 and 1: "+ x); } } max( (|-3|)^2 , Pi ) = 9. 0 A random number between 0 and 1: 0. 9764623048094814 הכלולות Java ומחלקות רבות נוספות כלולות בספריות Math המחלקה . JRE בהתקנת ה ניתן Java ומחלקות נוספות הכלולות ב Math מידע מפורט על מחלקת למצוא בקישור http: //download. oracle. com/javase/6/docs/api/java/lang/Math. html 33

 שינוי צורת הסתכלות של טיפוסים פרימיטיביים Casting • ניתן לחשוב על : כאל

שינוי צורת הסתכלות של טיפוסים פרימיטיביים Casting • ניתן לחשוב על : כאל שינוי צורת הסתכלות double : 48. 84 Int : 48 int: 48 (Narrowing Primitive Conversion) double: 48. 0 (Widening Primitive Conversion) Int : 48 char: ‘ 0’ WAT! 35

if - דוגמה ל import java. util. Scanner; public class Triangle { public static

if - דוגמה ל import java. util. Scanner; public class Triangle { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter the first number: "); double a = sc. next. Int(); System. out. print("Enter the second number: "); double b = sc. next. Int(); System. out. print("Enter the third number: "); double c = sc. next. Int(); if ((a + b <= c) || (a + c <= b) || (b + c <= a)) { System. out. println("There is no triangle with these sides. "); } else{ System. out. println("There is a triangle with these sides. "); } } } 42

import java. util. Scanner; דוגמה // compute n! using while and for sentences. public

import java. util. Scanner; דוגמה // compute n! using while and for sentences. public class Factorial { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter a non-negative number: "); int n = sc. next. Int(); // 1. using while int fact = 1; int i = 1; while ( i <= n ) { fact = fact * i; i = i + 1; } System. out. println("n!=" + fact); // 2. using for fact = 1; for ( i = 1; i <= n; i = i + 1){ fact = fact * i; } System. out. println("n!=" + fact); } } 44