debugger public class Test public static void mainString
- דוגמא לביצוע פעולות בסיסיות ב debugger : העתיקו את הקוד הבא לסביבת העבודה public class Test { public static void main(String[] args) { int x = 10; int y = 20; int z = x + y; foo(x, y); foo(x, z); } private static void foo(int x, int y) { System. out. println("parameters are: " + x + ", " + y); int sum = x + y; System. out. println("sum is: " + sum); } } 6
חלון הדיבאג Execution Stack Variables, breakpoints, expressions Source Console 7
1 דוגמה : התוכנית הבאה מדגימה את סדר הפעולות של הפעולות האריתמטיות 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 9
2 דוגמה 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 10
אופרטורים יחסיים אופרטורים המשווים בין שני מספרים ונותנים תשובה 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. 11
דוגמה 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 המחלקה . Java בהתקנת ניתן Java ומחלקות נוספות הכלולות ב Math מידע מפורט על מחלקת למצוא בקישור http: //download. oracle. com/javase/6/docs/api/java/lang/Math. html 13
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(); } } 20
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 <= 0) || (b <= 0) || (c <= 0) || (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. " ); } } 21
if - דוגמה נוספת ל עליכם לקלוט מספר מהמשתמש ולבדוק האם המספר בין : תרגיל . )לא כולל( והאם הוא זוגי ולהדפיס הודעה מתאימה 10 - ל 0 || אסור להשתמש באופרטור && או : שימו לב public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter the first number: "); int num = sc. next. Int(); if ( num > 0 ) if ( num < 10 ) if ( num % 2 == 0 ) System. out. println("num is between 0 and 10 and even"); } 22
- Slides: 23