Arithmetic Expressions & Data Conversions

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Expressions, Data Conversion, and Input
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Mathematical Calculations in Java Mrs. G. Chapman.
Mathematical Calculations in Java Mrs. C. Furman.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Doing math In java.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Expressions.
Chapter 7: Expressions and Assignment Statements
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Lecture 3 Java Operators.
Expressions.
Java Statements Java control structure is entrenched in statements:
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 7: Expressions and Assignment Statements
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 Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operator Operation Example + addition x + y
Type Conversion, Constants, and the String Object
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Arithmetic Expressions in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operators in C
Objects and Primitive Data
Arithmetic Expressions & Data Conversions
Expressions and Assignment
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
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
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Arithmetic Expressions & Data Conversions

Arithmetic Expressions An expression is a combination of one or more operands and their operators Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder % With arithmetic expressions, two ints will always result in an int but if either or both operands associated with an arithmetic operator are floating point (decimal), the result is a floating point.

Division and Remainder If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) Any attempt to divide by zero will produce a run-time error called an ArithmeticException. The program crashes when it tries to do a divide by zero operation. 14 / 3 equals? 4 8 / 12 equals? When you divide an int by an int in Java, the result is an int. This is referred to as integer division. The decimal portion of the answer is ignored. The technical way to say it is that the result is truncated, which means that the decimal point of the answer is discarded. It is never rounded. For instance, if the you divided two ints and the answers was 13.99999999., in Java the int division answer is 13. If either of the operands is a double, the result will be a double but if both operands are ints, the result if an int.

Modulus (Remainder) The remainder operator (%), known as modulus, returns the remainder after dividing the second operand into the first The modulus (or mod) operator appears often on the AP Computer Science A exam. White is not deliberately taught in most math classes, it is something that you are familiar with. The modulus operator uses the percent system, and produces the remainder after doing a division. Back in grade school you have been taught that the answer to14 divided by 3 was 4 R2, where the R stood for remainder. With the modulus, always think long-division The modulus operator is great for determining if a number is even or odd. If someNumber % 2 = 0, then someNumber is event. Or, if someNumber % 2 = 1, then someNumber is odd.

result = total + count / max - offset; Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right Parentheses can be used to force the evaluation order Java handles all mathematical calculations using the order of operations that you learning in math class. The order of operations does exactly that: it tells you the order to evaluate any expression that may contain parentheses, multiplication, division, addition, subtraction, and so on.

Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side

Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count count = count + 1; Then the result is stored back into count (overwriting the original value)

Data Conversions Sometimes it is convenient to convert data from one type to another For example, we may want to treat an integer as a floating point value during a computation Conversions must be handled carefully to avoid losing information Widening conversions are safest because they usually do not lose information (int to double) Narrowing conversions can lose information (double to int)

Data Conversions In Java, data conversions can occur in three ways: assignment conversion arithmetic promotion casting Assignment conversion occurs when a value of one type is assigned to a variable of another Only widening conversions can happen via assignment Arithmetic promotion happens automatically when operators in expressions convert their operands

result = (double) total / count; Casting Casting is the most powerful, and dangerous, technique for conversion Both widening and narrowing conversions can be accomplished by explicitly casting a value To cast, the type is put in parentheses in front of the value being converted For example, if total and count are integers, but we want a floating point result when dividing them, we can cast total: result = (double) total / count; On the AP CSA exam, you will be tested on the correct way to cast variables. Casting is a way to tell a variable to temporarily become a different data type for the sake of performing some action, such as division. The most common types of casts are from an int to a double or a double to an int.

Try an example result = total / count; result = ? int total = 16; count 5; double result; result = total / count; result = ? result = (double) total / count; result = ? Result = (double) (total / count); result = ? Let’s try an example. Let’s say that total and count are ints and have been initialized with the values 16 and 5. Result has been declared as a double.