210 likes | 230 Views
What will each of the following lines print?. System.out.println("number" + 6 + 4 + 5); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6 + 5 + 4); 15 System.out.println(6 + "number" + 5 + 4); 6number54
E N D
What will each of the following lines print? System.out.println("number" + 6 + 4 + 5); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6 + 5 + 4); 15 System.out.println(6 + "number" + 5 + 4); 6number54 System.out.println(6 + 5 + 4 + "number); 15number
Chapter 2 Variables, Constants, Assignments, Expressions
data – How are they different? false 48 Johnny 3.47 true
Memory Storage • In order to reserve space in memory for the variable, the computer has to know what type of data it will be. • Declare = to tell what type of data the variable will hold and its name • Initialize = assign the variable a value using the = sign
Primitive Data types Primitive Data Types: Simple data types the type of data that is stored in a variable: whole number int decimal number double true or false value ( 1,0) boolean Open up DataTypes in Dr. Java
String data type String is a class. It is an object variable. We can create String objects. String holds words enclosed in quotation marks. String name = “Sally”;
Review of Declaring Variables int sum; type identifier This statement declares sum. It reserves a space in memory large enough to store an int.
Assignments Statements We are initializing sum to 120. We use the assignment operator ( = ) to give a value to a variable. sum = 120; identifer assignment value operator
Create Variables What type of data should be used in the following? (double, int, char, boolean, or a class) 1. Your age: 2. Your bank account balance: 3. Your middle name: 4. Your height: 5. The speed limit:
Arithmetic Operators int and double operators: + addition - subtraction * multiplication / division % modulus or remainder
Arithmetic Expressions Expressions: combinations of operators and operands to perform calculations. 6 + 7 / 3 * 8 =
2 Types of Division 1. integer division – when both operands are of type integer, we truncate the fractional part. result = 7 / 3; 2. floating point division – if one or both operands are floating point numbers, then the fractional part is kept. double dresult = 7.0 / 3.0; result 2 dresult 2.333333333…
Integer and Double division Try this in the interactions pane 6/4 6.0 / 4 13 / 2 13.0 / 2
Modulus Operator % % (mod) remainder operator – returns the remainder when you divide 2 integers or doubles. Example: int result = 7 % 3; 2 3 7 6 1 Remainder result 1
Order of Operations Java has to follow the order of operations when computing an expression As a reminder if operators are on the same level, we work left to right. Assignments work right to left. Parenthesis Multiplication, Division, Modulus (L to R) Addition, Subtraction, String Concatenation (L to R) Assignment operator
Operator Precedence • Expressions are solved according to an order of operations. PEMDAS otherwise they are solved left to right. You should always use parenthesis to assure correctness. * / % first from left to right + - next left to right 14 + 16 / 3 % 2; (14 + 16) / 3 % 2; 3 * 6 - 4 / 2); 16 0 7
Website Go to the internet http://www.problets.org/about/topics/index.html Click on expression in java
Operator Precedence a + (b-c) * d – e 3 1 2 4 a % (b * c) * d * e 2 1 3 4
int a = 6, b = 10, c = 9; double w = 12.9, y = 3.2; 1. a + b * c _______________________ 2. a % b / y _______________________ 3. a - b – c _______________________ 4. a - b / c _______________________ 5. a / b _______________________ 6. b / a _______________________ 96 6 + 10 * 9 6 % 10 / 3.2 1,875 6 – 10 – 9 -13 5 6 – 10 / 9 0 6 / 10 10 / 6 1
int a = 6, b = 10, c = 9; double w = 12.9, y = 3.2; 1. w/y _______________________ 2. y/w_______________________ 3. a + w / b _______________________ 4. 4.03125 12.9 / 3.2 3.2 / 12.9 0.24806201550387597 7.29 6 + 12.9 / 10
Assignment • Problets website.