Arithmetic Expressions

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CSC Programming I Lecture 5 August 30, 2002.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
PRESENTATION 11 What Is Algebra. ALGEBRAIC EXPRESSIONS An algebraic expression is a word statement put into mathematical form by using variables, arithmetic.
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Doing math In java.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Variables, Operators, and Expressions
Introduction to Programming
CMPT 120 Topic: Python’s building blocks -> More Statements
Expressions.
ARITHMETIC IN C Operators.
Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
INSPIRING CREATIVE AND INNOVATIVE MINDS
Introduction to Programming
BIL 104E Introduction to Scientific and Engineering Computing
2.5 Another Java Application: Adding Integers
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
A First Book of ANSI C Fourth Edition
Introduction to Programming
Arithmetic Operators in C
Arithmetic Expressions & Data Conversions
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Chapter 2 Variables.
Lecture3.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Core Objects, Variables, Input, and Output
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Types and Expressions
Other types of variables
4.3 Arithmetic Operators and Mathematical Functions
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

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

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

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

Arithmetic Operations and the Math Class 4 4

Addition and Subtraction Operators As binary operators As used in algebraic expressions int y = 4 + 5; 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

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

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

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

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

What is the Value of? 9.2 + -2.5 * 2.0 1 + 5 % 2 4 + 2 / 5 + 1 (1 + 4 + 5) / 4.0 (1 + 4 + 5) / 4 10 10

Participation What is the Value of? 4.2 / 2.0 + 5.0 / 2.0 11 % 5 + 12 % 2 6 + 16 / 5 + 6 (6 + 16) / (5 + 6) (9 + 6) / 5.0 11 11

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

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

Look at Exam Review On my website. 14

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

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

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

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

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

Write a program to find the 4th root of a number input by the user. 20 20

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)

Participation Example (Skip/Already Did) Write a program to find the 4th root of a number input by the user. fourthRoot.c

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

Casting 25 25

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

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

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

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

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 = 0.6215, c3 = 35.75, c4 = 0.4275 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 30 30

Solved Problem: Wind-Chill (Skip) View listing of wind chill calculation program Listing 3-1 and output.(p. 50-52 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

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

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