7 public class Example 1 public static void
: דוגמה 7 public class Example 1 { public static void main(String[] args){ • טבלת משתנים int num; • הגדרה • שמות num = 3; • סקופ int num 1 = 1, num 2; • השמה • פלט num 2 = num 1; System. out. println("num = " + num); System. out. println("num 1 = " + num 1); System. out. println("num 2 = " + num 2); } } scope of num 1 and num 2
טבלת סיכום טיפוסים מספריים 11 טיפוס מספר ה bytes ערכים byte short int long float 1 2 4 8 4 -27 עד -172 -215 עד -1152 -231 עד -1312 -263 עד -1632 double 8
1 דוגמה 14 /* 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
2 דוגמה 15 : התוכנית הבאה מדגימה את סדר הפעולות של הפעולות האריתמטיות 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
3 דוגמה 16 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
אופרטורים יחסיים 17 אופרטורים המשווים בין שני מספרים ונותנים תשובה 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.
דוגמה 18 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(); x<y is true System. out. println("x<y is " + (x < y)); System. out. println("x>y is " + (x > y)); x>y is false System. out. println("x<=y is " + (x <= y)); x<=y is true System. out. println("x>=y is " + (x >= y)); x>=y is false System. out. println("x==y is " + (x == y)); x==y is false System. out. println("x!=y is " + (x != y)); x!=y is true } } Enter the first number: 10 Enter the second number: 20
אופרטורים לוגיים 19 אופרטורים לוגיים פועלים על ערכים מטיפוס לוגי )בוליאני( וגם . (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 * הערכת האופרטורים הלוגיים
דוגמה 20 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
דוגמה 22 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
if - דוגמה ל 28 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. "); } } }
The Debugger Create new project • • • Create new project; name it Max 3 public static void main(String[] args) { Scanner reader = new Scanner(System. in); int a, b, c; // input variables int max; // max value Create new class; name it Max 3; mark the generation of main method option System. out. println("Please enter three integers: "); Copy/Paste the following code to the class The Scanner class provides possibility to read from command line user input next. Int method parse user input to int type after number and enter pressed Try to execute ones the program (enter user input in Console window, bottom of the screen) a = reader. next. Int(); b = reader. next. Int(); c = reader. next. Int(); max = a; if (b > max) max = b; if (c > max) max = c; System. out. println("The max value is: " + max);
The Debugger Add breakpoints • • • A breakpoint suspends the execution of a program at the location where the breakpoint is set. Double click in front of a = reader. next. Int(); line of code, on place of the blue circle on the figure. You've just created a breakpoint. To delete it double-click on the blue circle. Create another breakpoint on theif (b > max) line of code.
The Debugger Debug the code • Press the bug button on the button bar, or choose Run | Debug from the menu bar.
The Debugger Debug the code • The IDE will switch to Debug Perspective, view with different windows layout than in coding mode - this perspective more convenient for debugging tasks.
The Debugger Debug the code • The execution of the program paused on the first breakpoint o on the blue circle appears blue arrow o current line marked with green • The JVM still not executed the current line
The Debugger Debug the code • In the buttons bar and the Run menu you can see debug main actions tools o Resume - for resuming program execution o Terminate - to kill the programm o Step Into - to enter inside logic, the method, of the current line (more of this after learning methods) o Step Over - execute current line and go to next one o Step Return - in case you inside some inner method, execute the whole method and pause execution after
The Debugger Debug the code • • On the top right window of the Debug perspective one of the existent tabs is Variables tab Click on this tab and remember what you see there (arg and reader variables)
The Debugger Debug the code • • • Press the Step Over button The JVM will execute the current line, so the execution means reading user input from command line Enter 13 in the Console window and press enter
The Debugger Debug the code • • Now pay attention what happened in the Variables tab - the new assigned variable a appears there with the assigned value, 13 o You can also hover with mouse pointer over the a variable in the code area and you also will see the current a's value, 13. In such a way you can always examine current state of your variables
The Debugger Debug the code • • Now press the Resume button. After pressing Resume, debugger execute the code till the end of the application execution, or till some breakpoint will hit, or till some exception will raise. In our case it will be next breakpoint hit on the line of code: if (b > max) (of course before you should finish interaction with the program as user - enter second and third integers in Console window, 14
The Debugger Debug the code • • • Current state of the Variables As we can see b is bigger than max, so the expression inside the if evaluates to true, therefore, then statement, max = b, should be executed. Let's press the Step Over button the execution moved to the max = b line of code.
The Debugger Debug the code • • Let's press again Step Over and will see the change of variable max in the Variables window after new assignment max = b; Eclipse automatically marked change in variable value with yellow color to pay our attention
The Debugger Debug the code • Now let's press the resume button and the application will run till the end and will terminated. The console window:
- Slides: 43