Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 2 A variable: A name of a location in memory to hold value A value of what? int a; String A; 245  11110101 245  0010 0010 0010 0100 0010 0101

3 3 Text String TextTel:2287249 EBCDICE385937A40F2 F8F7F2F4F9 ASCII54656C2A2032 3837323439 Binary: 0101 0100 0110 0101 0110 1100 0010 1010 0010 0000 0011 0010 0011 0010 0011 1000 0011 0111 0011 0010 0011 0100 0011 1001

4 4 ASCII 7-bit code 7-bit code http://www.dynamoo.com/technical/ascii.htm American National Standard Code for Information Interchange Arabic ASCII BinaryHex 0010 00002020 1010 00012121 2010 00102 3010 00112323 4010 01002424 5010 01012525 6010 01102626 7010 01112727 8010 10002828 9010 10012929 Char ASCII BinaryHex a110 000161 b110 001062 c110 001163 …………… A100 000141 B 100 001042 C 100 001143 ….…….… :011 10103A #010 001123

5 5 BitsMinimumMaximum Integers Short Signed 8 -128127 Unsigned0255 Integer Signed 16 -32,76832,767 Unsigned065,535 Long Signed 32 -2,147,483,6482,147,483,647 Unsigned04,294,967,295 BitsRange Floating Points Single32 -3.4  10 -38 3.4  10 38 Double64 -1.7  10 -308 1.7  10 308 Numbers inside Computers short Java’s Types int long float double

6 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 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 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/2.0-0.5; i+2;i = JOptionPane.showInputDialog("Input y") i = Integer.parseInt( );

9 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 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 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 12 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F

13 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 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 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 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));


Download ppt "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."

Similar presentations


Ads by Google