1 / 33

Chapter 2: Java Fundamentals cont’d

Chapter 2: Java Fundamentals cont’d. Spring 2006-2007 Lory Al Moakar. Outline. 2.1 The Parts of a Java Program 2.2 The print and println Methods, and the Java Standard Class Library 2.3 Variables and Literals 2.4 Primitive Data Types 2.5 Arithmetic Operators

Download Presentation

Chapter 2: Java Fundamentals cont’d

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

  2. Outline • 2.1 The Parts of a Java Program • 2.2 The print and println Methods, and the Java Standard Class Library • 2.3 Variables and Literals • 2.4 Primitive Data Types • 2.5 Arithmetic Operators • 2.6 Combined Assignment Operators • 2.7 Conversion Between Primitive Types • 2.8 Creating Named Constants with final • 2.9 The String Class • 2.10 Scope • 2.11 Comments • 2.12 Programming Style • 2.13 Reading Keyboard Input • 2.14 Dialog Boxes • 2.15 Common Errors to Avoid

  3. Review • Parts of the program • Comments: start with // • Are ignored by the compiler • Class header : public class Name • Method header: public static void main( String args[] ) • Statements

  4. Review • System.out.print: displays on the screen what we put in “” • System.out.println: same as print except that it moves the cursor to a new line after displaying the text

  5. Review

  6. Review • Variables: • Declare: type and identifier • Initialize: give it value ~ literal • Literals: • Identifiers: • Start with letter, _, or $ • Have only numbers, letters, _ and $ • Cannot be a keyword

  7. Printing a variable • 2 ways: • On its own: System.out.println( identifier); • Combined with text: System.out.println( “The result is: “+ identifier );

  8. Example // This program has a variable. public class Variable { public static void main(String[] args) { int value; value = 5; System.out.print("The value is "); System.out.println(value); } }

  9. 2.4 Primitive Data Types • Each variable has a data type which is the type of data that can be stored in the memory location allocated • Here is a list of primitive(numeric) data types:

  10. 2.4 Primitive Data Types cont’d No digits after decimal point 7 digits after decimal point 15 digits after decimal point

  11. Declaration revisited • Datatype Variablename; • Examples: • int hours; • byte minutes; • short month; • float average; • double distance

  12. The Integer Data Types // This program has variables of several of the integer types. public class IntegerVariables{ public static void main(String[] args) { int checking; // Declare an int variable named checking. byte miles; // Declare a byte variable named miles. short minutes; // Declare a short variable named minutes. long days; // Declare a long variable named days. checking = -20; miles = 105; minutes = 120; days = 185000; System.out.println("We have made a journey of " + miles + " miles."); System.out.println("It took us " + minutes + " minutes."); System.out.println("Our account balance is $" + checking); System.out.println("About " + days + " days ago Columbus " + "stood on this spot."); } } We have made a journey of 105 miles. It took us 120 minutes. Our account balance is $-20. About 185000 days ago Columbus stood on this spot. No Commas

  13. The Floating Data Types // This program demonstrates the double data type. public class Sale { public static void main(String[] args) { double price, tax, total; price = 29.75; tax = 1.76; total = 31.51; System.out.println("The price of the item " + "is " + price); System.out.println("The tax is " + tax); System.out.println("The total is " + total); } } The price of the item is 29.75 The tax is 1.76 The total is 31.15

  14. The Floating Data Types • float number = 23.5; • float number = 23.5f; • float number = 23.5F; ERROR CORRECT CORRECT

  15. The boolean Data Type • a boolean variable can have two values: • true • false • Example: boolean bool; bool = true; System.out.println(bool); bool = false; System.out.println(bool); true false

  16. The char Data Type • used to store characters • character literals are enclosed in single quotes • Example: char letter; letter = 'A'; System.out.println(letter); letter = 'B'; System.out.println(letter); A B

  17. The char Data Type • Characters are internally represented by numbers. • Java uses Unicode which is a set of numbers that are used as codes for representing characters • The codes are found in appendix A on the CD

  18. Example char letter; letter = 65; System.out.println(letter) letter = 66; System.out.println(letter); A B

  19. Variable Assignment and Initialization • assignment statement is used to put a value into a variable. Ex: x = 15; • always put the identifier on the left of the equal sign and the literal to the right of the equal sign. Ex: 15 = x; is incorrect • you can assign the value of one identifier to another. Ex: x = y; now x has the value stored in y

  20. Valid Variable Declaration • int month =2, days = 28; • float distance = 35.2f; • int c = 8, y =10, x, v=2;

  21. 2.5 Arithmetic Operators • 3 types of operator: • unary: require a single operand. • There is one unary operand: -9 (the negation operator) • --5 is 5 • binary: require two operands • ternary: require three operands

  22. Arithmetic operators

  23. Examples of Statements with Arithmetic Operators • amount = x + y; • amount = 562+543; • Minutes = 800 – call; • Tip = bill * 0.18; • Slice = cake /8; Notice that the identifier is to the left of the = Notice that the arithmetic operator is to the right of the =

  24. Example int counter = 0; //counter =0 counter = counter + 1; //counter =1 counter = counter + 39; //counter=40 counter = counter – 8; //counter=32 counter = counter * 5; //counter=160 counter = counter / 2; //counter= 80

  25. The % operator • Returns the remainder of the division • Examples; • 4%5 is 4 • 30%6 is 0 • 22%7 is 1 • 3205%100 is 5 • 3205%10 is 5

  26. Exercise • Write the following in a Java file: double amount = 137/5; System.out.println(“Amount is : “ + amount ); amount = 137.0/5; System.out.println(“Amount is : “ + amount );

  27. Integer Division • Dividing an integer by an integer gives an integer  the remainder is ignored • Examples: • 5/4 is 1 • 17/3 is 5

  28. Operator Precedence • What is the result of: • Polynomial = 1+2*3+ 6/2 -2; • Is it ? • (1+2)*3 + 6/(2-2) • 1+(2*3) +(6/2)-2 • (1+2)*3 + (6/2)-2

  29. Precedence Rules • Always evaluate * , / and % before + and – • Always negate before any calculations • *, / and % have same precedence • + and – have same precedence • If equal precedence then evaluate from left to right except for negations where we evaluate from right to left

  30. Precedence examples • Polynomial = 1+2*3+ 6/2 – 2; • Polynomial has the value of 1+6+3-2=8 • Polynomial = –1 + 5 – 2; // 2 • Polynomial = –(–3) + –(–5); //8

  31. Grouping with parentheses • You can use parentheses to force the evaluation of a formula • Examples: • x * ( y + z*z ) instead of x*y + z*z • x * ( y * ( z + 165 ) + 85 ) – 65 • Average = (a +b +c ) /3;

  32. The Math class • value = Math.pow( x,y); // now value holds x to the power of y • value = Math.sqrt( x); //now value holds the square root of x

  33. Combined Assignment Operators

More Related