Download presentation
Presentation is loading. Please wait.
1
Arithmetic Expressions
Chapter 2 Arithmetic Expressions 2/19 & 2/23 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 1 1
2
Chapter Topics Arithmetic Operators Standard Mathematical Methods
The Order of Operations The Division Operator / The Remainder Operator % The Data Type of an Arithmetic Expression Standard Mathematical Methods Casting A Problem Solved: Wind Chill Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 2 2
3
Objectives After studying this chapter, you should be able to:
Write Java arithmetic expressions representing algebraic expressions Identify type of an arithmetic expression Write a type cast, identify when it is necessary Use class Math to evaluate mathematical functions such as square root, power Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 3 3
4
Arithmetic Operations and the Math Class
4 4
5
Addition and Subtraction Operators
As binary operators As used in algebraic expressions int y = ; As unary operators Equivalent to binary operation with 0 as first operand int sum = +5; // sum is +5 int negSum = -sum //negSum is -5 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 5 5
6
Multiplication Operator *
Always used as a binary operator When consecutive operations occur Multiplied from left to right in order of appearance When real numbers are being multiplied the order of operations can be significant No implied multiplication: 3(x + y) Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 6 6
7
The Division Operator /
Always a binary operator Type of result (the quotient) depends on types of operands When both are integers, result is the integer portion of the quotient When either or both are doubles, the result is double To force a decimal answer, divide by a double Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 7 7
8
The Remainder Operator %
Results in the remainder after a division Note it is not the decimal portion of a division expression Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 8 8
9
Order of Operations Operators in an arithmetic expression execute in the following order: Operators within parentheses The unary operators + and - The operators *, /, and % The binary operators + and – Operators at the same level execute from left to right Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 9 9
10
What is the Value of? 9.2 + -2.5 * 2.0 1 + 5 % 2 4 + 2 / 5 + 1
( ) / 4.0 ( ) / 4 10 10
11
Participation What is the Value of?
4.2 / / 2.0 11 % % 2 / 5 + 6 (6 + 16) / (5 + 6) (9 + 6) / 5.0 11 11
12
Lab Participation(Skip)
E = mc2 is a formula to find energy, E, in newtons, that is derived from mass, m,in grams. C is the speed of light. Write a program in input the mass then find E. Make the speed of light a named constant. You can use 300,000,000 m/s for the speed of light. 12 12
13
Programming Example The formula for finding the nth term in an arithmetic sequence is: xn = a + d(n-1) Where a is the first term, d is the difference. Write a program to input a, d and n the find the nth term of the series. 13 13
14
Look at Exam Review On my website. 14
15
Standard Mathematical Methods
Java provides a collection of methods to do mathematical tasks Math is the name of the class Call to methods is Math.method_name( … ) Examples Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 15 15
16
Standard Mathematical Methods
Args and return type are double: Math.sqrt(x) -- square root of x Math.pow(x,y) – x to the y power Math.log(x) – ln x Math.log10(x) – log10x Math.exp(x) – ex Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 16 16
17
Standard Mathematical Methods
These return same type as argument: Math.abs(x) -- |x| Math.max(x,y) – maximum of x & y Math.min(x,y) – minimum of x & y Math.round(x) – Returns the nearest whole number to x. If x is double it returns a long. If x is float it returns an int Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 17 17
18
Standard Mathematical Methods
Figure 3-2 Call to method Math.sqrt Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 18 18
19
Example-Quarterback Ages
Write a program to ask for the ages of two quarterbacks. Output the difference their ages as a positive number. Input qbAge1 qbAge2 diff = | qbAge1 –qbAge2 | Output diff 19 19
20
Write a program to find the 4th root of a number input by the user.
20 20
21
Participation What is wrong with the following?
a.) y = (1/2) * Math.sqrt(x); b.) y = sqrt(38.0); c.) y = Math.exp(2,3); d.) y = math.sqrt( b*b – 4*a*c)/ (2*a)
22
Participation Example
(Skip/Already Did) Write a program to find the 4th root of a number input by the user. fourthRoot.c
23
Problem (Skip/Already Did)
Write a program to find the length of side C of a right triangle, given the lengths of sides A and B. Write the program. It is started for you on the next slide. 23 23
24
24 24
25
Casting 25 25
26
Conversion between Numeric Types
Conversion in expressions may happen implicitly or be required explicitly Consider Variable is a double Literal is an int The int is coerced implicitly, converted to a double Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 26 26
27
Conversion between Numeric Types
However, this does not work in the opposite direction In expressions If all values are of same type, result is of that type If one of the values is a floating-point, the result is floating-point Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 27 27
28
Casting Conversion can be forced with type casting What was illegal
Can be made legal by preceding the desired type in parentheses This is considered a unary operator Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 28 28
29
Program Example Write a program that asks for the number of cars in a neighborhood and the number of households. It should output the average number of cars per household. Let's write an algorithm first. Can use casting to get an accurate double answer. 29 29
30
Solved Problem: Wind-Chill
(Skip) Consider the formula for wind chill where T = current temp in deg F V = velocity of wind in mph c1 = 35.74, c2 = , c3 = 35.75, c4 = Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 30 30
31
Solved Problem: Wind-Chill
(Skip) View listing of wind chill calculation program Listing 3-1 and output.(p of text) Note the sections of the program Describe the program Get input data Compute wind-chill Display results Also notice the comments in the program. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 31 31
32
Questions What does the % operator do in Java?
What is a unary operator? What is wrong with this statement? How could it be made legal? int i = 1.5; How can I find x2.5 in Java? 32 32
33
Using mobaxterm to Transfer Your Program to onyx
Make a directory on onyx to put you program in mkdir myProg4 Open the directory in the left area of mobaxterm Drag the file from the PC’s folder to myProg4 mobaxterm 33 33
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.