Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 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

2 Chapter 2 Variables, Constants, Assignments, Expressions

3 data – How are they different? Johnny

4 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

5 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

6 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”;

7 Review of Declaring Variables int sum; typeidentifier This statement declares sum. It reserves a space in memory large enough to store an int.

8 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

9 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:

10 Arithmetic Operators int and double operators: + addition -subtraction * multiplication /division % modulus or remainder

11 Arithmetic Expressions Expressions: combinations of operators and operands to perform calculations. 6 + 7 / 3 * 8 =

12 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…

13 Integer and Double division Try this in the interactions pane 6/4 6.0 / 4 13 / 2 13.0 / 2

14 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

15 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

16 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

17 Website Go to the internet http://www.problets.org/about/topics/index.html Click on expression in java

18 Operator Precedence a + (b-c) * d – e 3 1 2 4 a % (b * c) * d * e 2 1 3 4

19 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_______________________ 6 + 10 * 9 6 % 10 / 3.2 6 – 10 – 9 6 – 10 / 9 6 / 10 10 / 6 96 1,875 -13 5 0 1

20 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. 12.9 / 3.2 3.2 / 12.9 6 + 12.9 / 10 4.03125 0.24806201550387597 7.29

21 Assignment Problets website.


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

Similar presentations


Ads by Google