Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle."— Presentation transcript:

1 Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle

2 2 Sample Java Program: Time.java Let’s continue examining the components of the second program from Lab 1 … Let’s continue examining the components of the second program from Lab 1 …

3 3 Sample Java Program: Time.java 1 class Time { 2 public static void main ( String[] args ) { 3 float distance, speed, time; 4 output out = new output(); 5 input in = new input(); 6 out.writeln( "Please enter distance (in miles)" ); 7 distance = in.readfloat(); 8 out.writeln( "Please enter speed (in MPH)" ); 9 speed = in.readfloat(); 10 time = distance / speed; 11 out.writeln( "The time taken to travel " + distance + " miles going at " + speed + " MPH is " + time + “ hours " ); 12 } //end main 13 } //end Time class 1 class Time { 2 public static void main ( String[] args ) { 3 float distance, speed, time; 4 output out = new output(); 5 input in = new input(); 6 out.writeln( "Please enter distance (in miles)" ); 7 distance = in.readfloat(); 8 out.writeln( "Please enter speed (in MPH)" ); 9 speed = in.readfloat(); 10 time = distance / speed; 11 out.writeln( "The time taken to travel " + distance + " miles going at " + speed + " MPH is " + time + “ hours " ); 12 } //end main 13 } //end Time class

4 4 Sample Java Program: Time.java Line 10: time = distance / speed; Line 10: time = distance / speed; Line 10 is a typical example of how to implement a simple mathematical operation in a Java program. Line 10 is a typical example of how to implement a simple mathematical operation in a Java program. Specifically: Specifically: The value stored in the variable distance is divided by the value stored in the variable speed. The value stored in the variable distance is divided by the value stored in the variable speed. The result is stored in the variable time. The result is stored in the variable time. Operators.java (Lab 2, problem 1) demonstrates implementing 4 additional mathematical operations in Java. Operators.java (Lab 2, problem 1) demonstrates implementing 4 additional mathematical operations in Java.

5 5 Arithmetic Operators There are two types of arithmetic operators: There are two types of arithmetic operators: Binary Binary Operate on two operands Operate on two operands For example, the expression a + 7 consists of two operands, a and 7, and the binary operator + For example, the expression a + 7 consists of two operands, a and 7, and the binary operator + Unary Unary Operate on a single operand Operate on a single operand For example, the expression - 8 consists of a single operand, 8, and the unary operator - For example, the expression - 8 consists of a single operand, 8, and the unary operator -

6 6 Arithmetic Operators (Binary) Java operation Arithmetic operator Algebraic expression Java expression Addition+ a + 7 Subtraction– p – q Multiplication*xy x * y Division/ x/y or x ÷ y x / y Remainder(modulus)% r mod s r % s

7 7 Arithmetic Operators (Unary) Java operation Arithmetic operator Algebraic expression Java expression Identity++a+a Sign inversion ––x–x

8 8 Arithmetic Operators Remainder operator (%) Remainder operator (%) The remainder, or modulus, operator returns the remainder of integer division. The remainder, or modulus, operator returns the remainder of integer division. It requires two operands, both of which must be integers. It requires two operands, both of which must be integers. Examples: Examples: output out = new output(); int result = 21 % 5; out.writeln(result);// displays 1 output out = new output(); int result = 21 % 5; out.writeln(result);// displays 1 output out = new output(); int num1 = 15, num2 = 4; out.writeln(num1 % num2);// displays 3 output out = new output(); int num1 = 15, num2 = 4; out.writeln(num1 % num2);// displays 3

9 9 Arithmetic Operators Division operator (/) Division operator (/) The division operator merits special attention because it behaves differently depending on the types of its operands. The division operator merits special attention because it behaves differently depending on the types of its operands. If at least one of the operands is a floating-point type (e.g., float or double), the result (i.e., quotient) is a floating-point type. If at least one of the operands is a floating-point type (e.g., float or double), the result (i.e., quotient) is a floating-point type. Examples: Examples: 9.0 / 2 yields 4.5 9 / 2.0 yields 4.5 9.0 / 2.0 yields 4.5 9.0 / 2 yields 4.5 9 / 2.0 yields 4.5 9.0 / 2.0 yields 4.5

10 10 Arithmetic Operators Division operator (/) Division operator (/) If both of the operands are integer types (e.g., int), the result is an integer type. If both of the operands are integer types (e.g., int), the result is an integer type. Examples: Examples: 9 / 2 yields 4 9 / 2 yields 4 1 / 2 yields 0 1 / 2 yields 0 10 / 5 yields 2, but 10.0 / 5 yields 2.0 10 / 5 yields 2, but 10.0 / 5 yields 2.0 Important note: Important note: In integer division (e.g., int / int), the result is not rounded; rather, it is truncated. Any part after the decimal point is literally thrown away. In integer division (e.g., int / int), the result is not rounded; rather, it is truncated. Any part after the decimal point is literally thrown away.

11 11 Order of Operations in Java The rules for the order in which operations are carried out in Java are essentially the same as those for carrying out operations in algebra. The rules for the order in which operations are carried out in Java are essentially the same as those for carrying out operations in algebra. So … who remembers “PEMDAS”? So … who remembers “PEMDAS”? Now we just have to figure out where to put the remainder operator … Now we just have to figure out where to put the remainder operator …

12 12 Operator Precedence Operator(s)Operation(s) Order of evaluation (precedence) +–Identity Sign inversion Evaluated first. */%MultiplicationDivisionRemainder Evaluated second. If an expression contains several operators of this type, they are evaluated from left to right. +–AdditionSubtraction Evaluated third. If an expression contains several operators of this type, they are evaluated from left to right.

13 13 Converting Algebraic Expressions to Java Expressions Algebraic expression: Algebraic expression: y = mx + b y = mx + b Java expression: Java expression: float y, m, x, b; y = m * x + b; Or: y = (m * x) + b;// with optional // parentheses float y, m, x, b; y = m * x + b; Or: y = (m * x) + b;// with optional // parentheses

14 14 Converting Algebraic Expressions to Java Expressions Algebraic expression: Algebraic expression: y = 2x 2 + 1 y = 2x 2 + 1 Java expression: Java expression: float y, x; y = 2 * x * x + 1; Or: y = 2 * (x * x) + 1;// with optional // parentheses float y, x; y = 2 * x * x + 1; Or: y = 2 * (x * x) + 1;// with optional // parentheses

15 15 Converting Algebraic Expressions to Java Expressions Algebraic expression: Algebraic expression: Java expression: Java expression: float y, x; y = 1 / (x – 1); float y, x; y = 1 / (x – 1);

16 16 Converting Algebraic Expressions to Java Expressions Algebraic expression: Algebraic expression: Java expression: Java expression: Your turn! Your turn!

17 17 Converting Algebraic Expressions to Java Expressions Algebraic expression: Algebraic expression: Java expression: Java expression: Your turn! Your turn!


Download ppt "Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle."

Similar presentations


Ads by Google