Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,

Slides:



Advertisements
Similar presentations
Computation u When processing data we subject it to a variety of transformations or computations. u The type of data determines the set of valid operations.
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Java Planning our Programs Flowcharts Arithmetic Operators.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
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.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Shorthand operators.
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
LESSON 6 – Arithmetic Operators
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
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.
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.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Mixing integer and floating point numbers in an arithmetic operation.
APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall.
Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Doing math In java.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Operators.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Programming Principles Operators and Expressions.
Lesson 4 : Exponent Laws I Check it out... Can you see a short cut rule?
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
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.
CompSci 230 S Programming Techniques
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Building Java Programs
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
With Assignment Operator
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Expressions and Assignment
Chapter 2: Java Fundamentals
Data Types and Expressions
Building Java Programs
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Data Types and Expressions
Building Java Programs
Data Types and Expressions
Presentation transcript:

Programs That Calculate

Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation, division, multiplication, addition, subtraction)

Evaluate the expression: (5-8/2)*3+5  Java proceeds as follows: => (5-4)*3+5 => (1)*3+5 => 3+5 => 8

 Operations on values of the same type produce values of that type Example: 15/3=> 9/5 => 5 1 Note that when we divide 2 integers, the result is the integral quotient with any remainder being ignored!

Division by ZERO  In Math, division by zero is undefined  In Java, if we divide a number by ZERO, Java throws an arithmetic exception  If any exception is thrown during the execcution of a program, the programmer can write instructions to catch the exception I can’t cope with this!!!!

modulo  Useful Arithmetic operator written as %  Has same precedence as multiplication and divison  % gives the remainder Example 7%3 => 8%0 => 20%7 => 6 Throws an Arithmetic Exception 1

public class ArithmeticProg {public static void main(String[] args) { int i = 10, j = 20; System.out.println("Adding"); System.out.println(" i + j = " + (i + j)); System.out.println("Subtracting"); System.out.println(" i - j = " + (i - j)); System.out.println("Multiplying"); System.out.println(" i * j = " + (i * j)); System.out.println("Dividing"); System.out.println(" i / j = " + (i / j)); System.out.println("Modulus"); System.out.println(" i % j = " + (i % j)); }// main //ArithmeticProg class

Increment and Decrement Operators ++ operator adds one to the current value of an int or char. -- operator subtracts one to the current value of an int or char.  Neither operator works on doubles, booleans or Strings Example: n++; and ++n; are equivalent to n = n+1; n--; and --n; are equivalent to n = n-1; char c = ‘A’ c++; //will change the value c to ‘B’

Assignment Operators + = is equivalent to: = + Example x+= 3 is equivalent to x=x+ 3 x +=Y-2 is equivalent to x=x+(Y-2)

Assignment can also be combined with the other four basic arithmetic operators: -=*= /=%=  Example: x -=Y+3 is equivalent to x=x-(Y+3) n *= 5 is equivalent to n=n*5

Multiple assignments in one statement:  Operators are applied from right to left Example: i=j=k=1 is equivalent to i=(j=(k=1))

Using Math Methods  Math.abs(-4) => 4  Math.sqrt(25) => 5  Math.pow(2, 10) => 1024  Math.max(10, 2) => 10  Math.min(10, 2) => 2