Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Chapter 2 Using Primitive Data Types and Using Classes.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Chapter 2 part #4 Operator
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Mathematical Calculations in Java Mrs. G. Chapman.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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[]
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
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,
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Doing math In java.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
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.
Expressions.
Lecture 3 Java Operators.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
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
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Arithmetic Operator Operation Example + addition x + y
Escape Sequences What if we wanted to print the quote character?
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions & Data Conversions
Expressions and Assignment Statements
Expressions and Assignment
elementary programming
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
Primitive Types and Expressions
Operator King Saud University
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education, Inc. Prepared by: Elliot Koffman, Temple University and Dorina Petriu, Carleton University

Copyright © 2003 Pearson Education, Inc. Slide Processing numeric data Arithmetic operatorMeaningExample + Addition is is 7.0 – Subtraction is is 3.0 * Multiplication 5 * 2 is * 2.0 is 10.0 / Division 5.0 / 2.0 is / 2.0 is / 2 is / 2 is 2 (integer division) % Remainder 7 % 4 is 3 (use only w/ int) 4 % 7 is 4

Copyright © 2003 Pearson Education, Inc. Slide 2-3 Integer Division and Remainder int data type + - * / = (assignment) % Used only with integers, gives remainder Examples of integer division and remainder 15 / 3 = 5, 15 % 3 = 0 15 / 2 = 7, 15 % 2 = 1 2 / 15 = 0, 2 % 15 = / 100 = 2, 299 % 100 = 99

Copyright © 2003 Pearson Education, Inc. Slide 2-4 Statements and Expressions Statement: an instruction that performs an operation. There are two kinds of statements: –data declarations - tell the Java compiler what kind of storage locations to allocate –executable statements - instruct the computer how to process the information in storage Expression: Java code that produces a result. Expressions are used mostly as parts of statements.

Copyright © 2003 Pearson Education, Inc. Slide 2-5 Assignment Statement Form: variable = expression ; Example: x = y + z; Interpretation: If variable is declared as a primitive type, the value of the expression is stored in variable. If variable is a declared as a class type, a reference to the object formed by expression is stored in variable. The previous value of variable is lost. The expression can be a variable, a constant, a literal, a method call returning a value, or a combination of the above connected by appropriate operators (such as +, -, * and / ).

Copyright © 2003 Pearson Education, Inc. Slide 2-6 Assignment Compatability In an assignment statement, the value of the expression must be assignment compatible with the variable, meaning that: Either the data types of the expression and variable are the same, or The expression’s type can be converted to the variable’s type. If this condition is met, the conversion (which is named assignment conversion) is performed automatically.

Copyright © 2003 Pearson Education, Inc. Slide 2-7 Effect of assignment statements sum = sum + item; before assignment: sum = 25.0; item = 5.0 after assignment: sum = 30.0; item = 5.0 newx = -x; before assignment: newx = 0.0; x = ; after assignment: newx = 5.123; x = ;

Copyright © 2003 Pearson Education, Inc. Slide 2-8 Data Type of Arithmetic Operation The type of the result of an arithmetic operation is double if an operand is type double. e.g., is 3.0 If both operands are type int, then the result is type int. e.g., 1 / 2 is 0.

Copyright © 2003 Pearson Education, Inc. Slide 2-9 Mixed-type assignments Mixed-type assignment statement: the assignment of an expression of one type to a variable of another type. Assignment conversion: automatic conversion of the expression value (after the expression was completely evaluated) to the variable type. Such a conversion must be a widening one (e.g., from int to double). int m = 3; int n = 2; double x, y; y = m + n; assignment conversion: y becomes 5.0 (not 5) x = y + m / n; m / n is 1 (not 1.5), is 6.0, assign 6.0 to x Example of invalid assignments due to possible loss of the fractional part (narrowing conversion is not allowed) int count; count = 3.6; invalid: can’t assign double to int count = count + 1.0; invalid: expr. result is double Error: Incompatible type for =. Explicit cast needed to convert double to int.

Copyright © 2003 Pearson Education, Inc. Slide 2-10 Type casting Casting: the most general form of conversion in Java. A cast is a Java operator specified by a type in parentheses, that is applied to the value of an expression. Type casting syntax: Form: (type) valueExample: double cost; int dollars; dollars = (int) cost; Interpretation: The cast in the example creates an int value by converting cost to an integer (which truncates any fractional part). The content of cost remains unchanged. More type casting examples: int count; count = (int) 3.6; int m = 7; int n = 2; double x; x = (double) m / n; int m = 7; int n = 2; double x; x = (double) (m /n); the cast operator creates a double value (i.e., 7.0 ) n is converted to a double by arithmetic promotion division produces the result 3.5, which is assigned to x the cast operator creates an int value (i.e. 3 ), which is assigned to count the integer division 7 / 2 gives the result 3 the cast operator creates a double value (i.e., 3.0 ), which is assigned to x

Copyright © 2003 Pearson Education, Inc. Slide 2-11 Rules for Evaluating Expressions Parentheses rule: Evaluate expressions in parentheses separately. Evaluate nested parens from the inside out. Operator precedence rule: Operators in the same expression are evaluated in the order determined by their precedence (from the highest to the lowest). OperatorPrecedence Left associative rule: Operators in the same expression and at the same precedence level are evaluated in left-to-right order. method callhighest precedence - (unary) new, type cast *, /, % +, - (binary) = lowest precedence

Copyright © 2003 Pearson Education, Inc. Slide 2-12 Evaluation of z – (a + b / 2) * w / y z - (a + b / 2) * w / y Operator, reason evaluated --/--- /, parens and precedence , parens *-- *, precendence, left assoc /-- /, precedence

Copyright © 2003 Pearson Education, Inc. Slide 2-13 Mathematical Formulas in Java a = bc not valid Java syntax –insert * Operator a = b * c; m = y - b x - a –insert ( ) and /m = (y - b) / (x - a);

Copyright © 2003 Pearson Education, Inc. Slide 2-14 Class TwoNumbers public class TwoNumbers { public static void main(String[] args) { double num1 = 8; double num2 = 6; System.out.println("First number is " + num1); System.out.println("Second number is " + num2); System.out.println("Sum is " + (num1 + num2)); System.out.println("Difference is " + (num1 - num2)); System.out.println("Product is " + (num1 * num2)); System.out.println("Quotient is " + (num1 / num2)); } }

Copyright © 2003 Pearson Education, Inc. Slide 2-15 Sample run of class TwoNumbers