Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 2 Elementary Programming
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
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.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 Chapter 2: Elementary Programming Shahriar Hossain.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Expressions, Data Conversion, and Input
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
LESSON 6 – Arithmetic Operators
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Chapter 2 Elementary Programming. 2 Objectives  To write Java programs to perform simple calculations  To obtain input from the console using the.
Chapter 2 Elementary Programming
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Mathematical Calculations in Java Mrs. G. Chapman.
Mathematical Calculations in Java Mrs. C. Furman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Lecture 3 Java Operators.
Lecture 3: Operators, Expressions and Type Conversion
Object Oriented Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Numerical Data Types.
Expressions and Assignment Statements
Expressions and Assignment
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.
CS2011 Introduction to Programming I Elementary Programming
Data Types and Expressions
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in JAVA: V

Floating Point Data Types

double Data Type Data type that can hold numbers with fractional values – e.g. 3.14, 98.6 Doubles can be used to represent many values: –Money (but see warning below) –distance –weight, etc.

double Example // Double Example Program public class Double { public static void main( String args[] ) { double var1, var2, var3, sum; var1 = 87.25; var2 = 92.50; var3 = 96.75; sum = var1 + var2 + var3; System.out.println ("Sum: " + sum); } Sum: 276.5

5 Numeric type ranges in Java Integers Floating point values

Example: Find an Average Suppose you want to determine a student’s average. int totalTests = 4; double average = / totalTests;

Example: Find an Average Problem #1: Operator Precedence –By rules of operator precedence, 100/4 is evaluated first. Hence, average is set to: 302. –To solve this problem, use (): int totalTests = 4; double average = ( )/ totalTests;

Example: Find an Average Problem #2: –90, 92, 95, 100 and 4 are all integers. Hence, this is integer division. –Integer division can result in data truncation (or loss of data.) –Hence, average is set to: 94.0, but the students real average is –There are actually three ways to solve this problem.

Rules of Promotion Promotion: when mixing integers and doubles, all the values are promoted to doubles. In our average example, there are three ways to force promotion, and get the right answer of 94.25: 1. change totalTests to a double: double totalTests = 4.0; double average = ( )/ totalTests;

Rules of Promotion 2.Use a double literal for one of the values on the right hand side double average = ( ) / 4; 3. Use a Cast Operator int totalTests = 4; double average = ( )/(double)totalTests; In this case, totalTests is explicitly cast to a double. And, because we have one double, everything else is promoted. Note that you can also use the (int) cast to cast a double to an integer.

11 constants final int TOTAL_GRADES = 4; Used to avoid “magic numbers” in code. –Only need to change in one place If not, best case must search entire code, worst case you miss a few occurrences. –Choose meaningful names for your constants as you would for any other identifier Unlike variables, you cannot change the value of a symbolic constant. Should be in ALL CAPS (style).

12 more on casting In an assignment statement, you can assign a value of a “less expressive” type to a variable which is “more expressive”. For example: –int gets byte –double gets int –double gets float You must explicitly cast a value of a “more expressive” type to a variable which is “less expressive”. For example: –byte gets (byte) int –int gets (int) double –float gets (float) double –If you do not explicitly cast these values, you will have a syntax error in Java.

Warning about floating point values Floats may be represented differently from what you think by the computer –E.g. 1.9 to you may be –1.9 will not necessarily equal 1.9! In critical calculations for the same reason –E.g..1 added 10 times often will not add up to 1 –Use long integers instead and keep track of where your decimal point is (e.g. $1.75 should be stored as 175)

14 Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

15 Shortcut Assignment Operators OperatorExampleEquivalent +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8

16 Increment and Decrement Operators OperatorNameDescription ++varpreincrementThe expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++postincrementThe expression (var++) evaluates to the original value in var and increments var by 1. --varpredecrementThe expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. var--postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.

17 Increment and Decrement Operators, cont.

18 Increment and Decrement Operators, cont. Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i.