4.3 Arithmetic Operators and Mathematical Functions

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Return values.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Pemrograman Dasar - Data Types1 OPERATOR. Pemrograman Dasar - Data Types2 Arithmetic operator  + - * /  / operator denotes integer division if both.
Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the.
Chapter 4  Fundamental Data Types 1 Chapter 4 Fundamental Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
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,
Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;
1 Intro to Computer Science I Chapter 2 Fundamental Data Types Java scripting with BeanShell.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Chapter 4: Fundamental Data Types. To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware.
Chapter 4 Numeric types & arithmetic Strings: reading & writing.
Chapter 3 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the int and double types.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 3 – Fundamental Data Types.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To recognize.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
CSC Programming I Lecture 5 August 30, 2002.
Fall 2006Slides adapted from Java Concepts companion slides1 Fundamental Data Types Advanced Programming ICOM 4015 Lecture 4 Reading: Java Concepts Chapter.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Review 2.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Doing math In java.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
CSE 110: Programming Language I Afroza Sultana UB 1230.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Arithmetic Expressions
Variables, Operators, and Expressions
A final variable is a constant
Expressions.
2008 Sixth Grade Competition Countdown Round
Chapter 4 – Fundamental Data Types
BIL 104E Introduction to Scientific and Engineering Computing
Chapter Goals To understand integer and floating-point numbers
Lecture Notes – Basics (Ch1-6)
Assignment and Arithmetic expressions
Introduction to Programming
Java Programming: From Problem Analysis to Program Design, 4e
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Chapter 4 – Fundamental Data Types
Chapter 2: Basic Elements of Java
Arithmetic Expressions
Arithmetic Expressions & Data Conversions
Wednesday 09/23/13.
Data Types and Expressions
Chapter 5 – Decisions Big Java by Cay Horstmann
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
6.2 for Loops Example: for ( int i = 1; i
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Introduction to Computer Science and Object-Oriented Programming
Data Types and Expressions
Presentation transcript:

4.3 Arithmetic Operators and Mathematical Functions Four basic operators: addition: + subtraction: - multiplication: * division: / Parentheses control the order of subexpression computation: (a + b) / 2 Multiplication and division bind more strongly than addition and subtraction: Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Increment and Decrement items++ is the same as items = items + 1 items-- subtracts 1 from items Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

/ is the division operator Integer Division / is the division operator If both arguments are integers, the result is an integer. The remainder is discarded 7.0 / 4 yields 1.75 7 / 4 yields 1 Get the remainder with % (pronounced “modulo”) 7 % 4 is 3 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Example: Integer Division final int PENNIES_PER_NICKEL = 5; final int PENNIES_PER_DIME = 10; final int PENNIES_PER_QUARTER = 25; final int PENNIES_PER_DOLLAR = 100; // Compute total value in pennies int total = dollars * PENNIES_PER_DOLLAR + quarters * PENNIES_PER_QUARTER + nickels * PENNIES_PER_NICKEL + dimes * PENNIES_PER_DIME + pennies; // Use integer division to convert to dollars, cents int dollars = total / PENNIES_PER_DOLLAR; int cents = total % PENNIES_PER_DOLLAR; Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

To compute xn, you write Math.pow(x, n) Powers and Roots Math class: contains methods sqrt and pow to compute square roots and powers To compute xn, you write Math.pow(x, n) However, to compute x2 it is significantly more efficient simply to compute x * x To take the square root of a number, use Math.sqrt; for example, Math.sqrt(x) In Java,                        can be represented as (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a) Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Analyzing an Expression Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Mathematical Methods Function Returns square root power xy ex Math.sqrt(x) square root Math.pow(x, y) power xy Math.exp(x) ex Math.log(x) natural log Math.sin(x), Math.cos(x), Math.tan(x) sine, cosine, tangent (x in radians) Math.round(x) closest integer to x Math.min(x, y), Math.max(x, y) minimum, maximum Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Cast converts a value to a different type: Cast and Round Cast converts a value to a different type: double balance = total + tax; int dollars = (int) balance; Math.round converts a floating-point number to nearest integer: long rounded = Math.round(balance); // if balance is 13.75, then rounded is set to 14 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 4.2 Cast Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Arithmetic Expressions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Self Check 4.6 What is the value of n after the following sequence of statements? n--; n++; Answer: One less than it was before.

Self Check 4.7 What is the value of 1729 / 100? Of 1729 % 100? Answer: 17 and 29

Self Check 4.8 Why doesn’t the following statement compute the average of s1, s2, and s3? double average = s1 + s2 + s3 / 3; // Error Answer: Only s3 is divided by 3. To get the correct result, use parentheses. Moreover, if s1, s2, and s3 are integers, you must divide by 3.0 to avoid integer division: (s1 + s2 + s3) / 3.0

Self Check 4.9 What is the value of Math.sqrt(Math.pow(x,  2)  + Math.pow(y, 2)) in mathematical notation? Answer:              

Self Check 4.10 When does the cast (long) x yield a different result from the call Math.round(x)? Answer: cast does NOT round, simply truncates the decimal. There will be a difference when the fractional part of x is ≥ 0.5

Self Check 4.11 How do you round the double value x to the nearest int value, assuming that you know that it is less than 2 ·109? Answer: By using a cast: (int) Math.round(x)