1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while int float double main... Two ids, Tax and Rate Can’t be used as identifiers They are reserved word $ “ ;,... No special chars No numbers 1 123
2 A variable: A name of a location in memory to hold value A value of what? int a; String A; 245
3 Text String TextTel: EBCDICE385937A40F2 F8F7F2F4F9 ASCII54656C2A Binary:
4 ASCII 7-bit code 7-bit code American National Standard Code for Information Interchange Arabic ASCII BinaryHex Char ASCII BinaryHex a b c …………… A B C ….…….… : A #
5 BitsMinimumMaximum Integers Short Signed Unsigned0255 Integer Signed ,76832,767 Unsigned065,535 Long Signed 32 -2,147,483,6482,147,483,647 Unsigned04,294,967,295 BitsRange Floating Points Single Double Numbers inside Computers short Java’s Types int long float double
6 8 primitive data types in Java short i,j; int no; long sum; float total; double tax; char grade; byte b; boolean yes_or_no; i = 3; j = 3.14; tax = 8.25/100; grade = ‘A’; grage = ‘B’; grade = 97; yes_or_no = true; yes_or_no = false; Every variable has to be declared before we use it.
7 Variables and Assignments L-value = R-value Assignment operator Everything that can receive a value Everything that can give a value variable function expression literal The data types in both sides have to be consistent. If not, type-casting will occur.
8 Assignment examples L-value = R-value total = i+j; average = total/2; sum = average*10*tax_rate; i = gcd(24,18); illegal 3 = j; gcd(24,18) = 2+3; i+1 = j+3; (3+7)*2-4-2*5;i = 3/2-0.5;i = i = 3/ ; i+2;i = JOptionPane.showInputDialog("Input y") i = Integer.parseInt( );
9 More on Assignments L-value = R-value This operation itself will give a value L-value = R-value L-value = a = b = c = i+j+3+gcd(15,6); illegal: a = b+1 = c = i+j+3+gcd(15,6);
10 Arithmetic operators in Java +-*/% (x % y) is the remainder of x divided by y Relational and logical operators in Java Resulting true or false +-*/% (x % y) is the remainder of x divided by y >===!= && ||! &|^ (20 < y) && (x <= i)
11 Logical Operators (1 || 0) ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x = 5) (((x % 2) == 0) && ((x % 3) == 0)) || && ! Assume x = 10 true false
12 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F
13 The Syntax of if and if/else statements if ( condition ) { statement list; } if ( condition ) { statement list1; } else { statement list2; } Indentation indicates that the statements in the statement list are at the level next to the if/else statement. Reserved words A Boolean expression (logical expression). In Java, 0 is false, any non-zero values will be considered as true. true false A reserved word can’t be used as an identifier
14 Example double Cash=0; Cash = Double.parseDouble( JOptionPane.showInputDialog("Cash: ")); if (Cash <= 0) JOptionPane.showMessageDialog(null, "You don't have any money!"); Price = (2.99*5)*(1+0.08); if ((Cash-Price) < 0) JOptionPane.showMessageDialog(null, "You don't have enough money!"); else JOptionPane.showMessageDialog(null, "Your change: "+ (Cash-Price)); Initialize Cash to value 0;
15 A compound statement: a group of statements double Cash=0; Cash = Double.parseDouble( JOptionPane.showInputDialog("Cash: ")); if (Cash <= 0) JOptionPane.showMessageDialog(null, "You don't have any money!"); Price = (2.99*5)*(1+0.08); if ((Cash-Price) < 0) { JOptionPane.showMessageDialog(null, "You don't have enough money!"); JOptionPane.showMessageDialog(null, "Bring more money!"); } else JOptionPane.showMessageDialog(null, "Your change: "+ (Cash-Price));
16 Save some time: avoid doing the same thing twice double Cash=0; double Change; Cash = Double.parseDouble( JOptionPane.showInputDialog("Cash: ")); if (Cash <= 0) JOptionPane.showMessageDialog(null, "You don't have any money!"); Price = (2.99*5)*(1+0.08); Change = Cash-Price; if (Change < 0) { JOptionPane.showMessageDialog(null, "You don't have enough money!"); JOptionPane.showMessageDialog(null, "Bring more money!"); } else JOptionPane.showMessageDialog(null, "Your change: "+ (Change));