Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.

Similar presentations


Presentation on theme: "Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions."— Presentation transcript:

1 Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions

2 Computer Programming with Java2 Contents Variables Primitive Types Assignment Statements Constants Arithmetic Operators The String class Documentation Keyboard and Screen I/O

3 Computer Programming with Java3 Variables Definition Variables in a program are used to store data such as numbers and letters. Values are the number or letter or other data item in a variable A variable can have its value changed. Identifiers are the variable names and must satisfy the spelling rules in Chapter 1.

4 Computer Programming with Java4 public class EggBasket { public static void main(String[] args) { int numberOfBaskets, eggsPerBasket, totalEggs; System.out.println("Enter the number of eggs in each basket:"); eggsPerBasket = SavitchIn.readLineInt(); System.out.println("Enter the number of baskets:"); numberOfBaskets = SavitchIn.readLineInt(); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); eggsPerBasket = eggsPerBasket - 2; totalEggs = numberOfBaskets * eggsPerBasket; … Display 2.1: A Simple Java Program

5 Computer Programming with Java5 Primitive Types Primitive Data Types integers: byte, short, int, long e.g.) 0, 1, -1, 2, -2 floating point: float, double e.g.) 9.9, 3.14159, -5.63 others: char, boolean e.g.) char char symbol; symbol= ‘A’; e.g.) boolean true or false

6 Computer Programming with Java6

7 7 Assignment Statements Assignment operator Variable = Expression or Values; e.g.) amount = 3.99; score = numberOfCards + handicap; Declaration and Initialization Specialized assignment operators +=, -=, /=, *=, %= e.g.) amount += 5; equals to amount = amount + 5;

8 Computer Programming with Java8 Assignment Statements (1) Assignment Compatibilities Principle of Compatibility you cannot put a value of one type in a variable of another type. you can assign a value of any type on the following list to a variable of any type that appears further down the list byte -> short -> int -> long -> float -> double if you want to assign a value of type double to a variable of type int, you must change the type of the value using a type cast

9 Computer Programming with Java9 Assignment Statements (2) Type Casting the changing of the type of a value from its normal type to some other type. (Type_Name) Expression e.g.) double guess = 7.8; int answer = (int) guess; (correct) double distance = 9.0; int points = distance; (incorrect)

10 Computer Programming with Java10 Constants Number constants It is literal values like 2 or 3.7 and its value do not change in a Java program. integer e.g.) 2, 3, 0, -3, 752, +12 (correct) 1,000 (incorrect) floating-point e.g.) 2.1, 3.14159, 865000000.0 8.6510 8 : 865000000.0 or 8.65e8

11 Computer Programming with Java11 Constants (1) Named Constants a name be defined for constant Type Variable = Constant; e.g.) double PI = 3.14159;

12 Computer Programming with Java12 public class CircleCalculation { public static void main(String[] args) { double radius; //in inches double area; //in square inches System.out.println("Enter the radius of a circle in inches:"); radius = SavitchIn.readLineDouble(); area = 3.14159 * radius * radius; System.out.println("A circle of radius " + radius + " inches"); System.out.println("has an area of " + area + " square inches."); } Display 2.9: Comments and Indenting

13 Computer Programming with Java13 Arithmetic Operators Basic Operators +, -, *, / it can used to combine variables and/or numbers Arithmetic Expression

14 Computer Programming with Java14 Arithmetic Operators (1) Increment and Decrement Operators it can be used to increase or decrease the value of a variable by one. ++, -- e.g.) count = count + 1; count++; count = count - 1; count--;

15 Computer Programming with Java15 Arithmetic Operators (2) Precedence Rules (pp. 140-143) when the computer is deciding which of two operators to perform first and the order is not dictated by parentheses, then it does the operator of higher precedence before the operator of lower precedence. Some operators have equal precedence, in which the order of operations is determined by the left-to-right order of the operators.

16 Computer Programming with Java16 Higher Precedence First: the unary operators: +, -, ++, --, ! Second: the binary arithmetic operators: *, /, % Third: the binary arithmetic operators: +, - Fourth: the boolean operators:, = Fifth: the boolean operators: ==, != Sixth: the boolean operators: & Seventh: the boolean operators: | Eighth: the boolean operators: && Ninth: the boolean operators: || Lowest Precedence Display 3.13: Precedence Rules

17 Computer Programming with Java17 The String Class String class it can be used to store and process strings of characters. String methods see text pp.55-59 zero-based index String tmp = “Java is fun.”; char tmp_len = tmp.length();

18 Computer Programming with Java18 public class StringDemo { public static void main(String[] args) { String sentence = "Text processing is hard!"; int position; position = sentence.indexOf("hard"); System.out.println(sentence); System.out.println("012345678901234567890123"); System.out.println("The word \"hard\" starts at index " + position); sentence = sentence.substring(0, position) + "easy!"; System.out.println("The changed string is:"); System.out.println(sentence); } Display 2.7: Using the String class

19 Computer Programming with Java19 The String Class (1) Escape characters it escape from the usual meaning of a character. \”: double quote, \’: single quote, \\: backslash, \n: new line, \r: carriage return, \t: tab e.g.) The word “hard” starts at index 19 System.out.println(“The word “hard” starts at index “+19); -> “The word” //incorrect System.out.println(“The word \”hard\” starts at index “+19); //correct

20 Computer Programming with Java20 Documentation Documentation style Truth on S/W development A Project: Development (20~30%), Maintenance (70~80%) if the program is not easy to read and understand, it will not be easy to maintain it. Rule of thumb Use meaningful names for variables Self-documenting thanks to a very clean style and very well-chosen variable names (and other names)

21 Computer Programming with Java21 Documentation (1) Comments //, /*…*/: Things written into your program that help a person understand the program, but that are ignored by the compiler. e.g.) String sentence; //Spanish version /*This program should only be used…. */ Indenting

22 Computer Programming with Java22 Keyboard and Screen I/O I/O input and output of program data Screen Output System.out.{println, print}() e.g.) System.out.println(“Enter the number of eggs..:”); System.out.println(“Lucky number = “+13+”Secret number = “+ number);


Download ppt "Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions."

Similar presentations


Ads by Google