1 / 21

What will each of the following lines print?

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

jjohansen
Download Presentation

What will each of the following lines print?

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. 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? false 48 Johnny 3.47 true

  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; type identifier 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 _______________________ 96 6 + 10 * 9 6 % 10 / 3.2 1,875 6 – 10 – 9 -13 5 6 – 10 / 9 0 6 / 10 10 / 6 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. 4.03125 12.9 / 3.2 3.2 / 12.9 0.24806201550387597 7.29 6 + 12.9 / 10

  21. Assignment • Problets website.

More Related