What are they? Why do we need them?

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for.
The Fundamental Property of Rational Expressions
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
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.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Expressions, Data Conversion, and Input
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Copyright © Curt Hill Numerics Representation and Errors.
Copyright © – Curt Hill Types What they do.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Copyright Curt Hill Casts What are they? Why do we need them?
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Variables, Operators, and Expressions
CompSci 230 S Programming Techniques
7.2 Arithmetic Expressions
Digital Logic & Design Adil Waheed Lecture 02.
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions.
Side Effect Operators Changing the value of variables
More important details More fun Part 3
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.
Assignment and Arithmetic expressions
What are they? Why do we need them?
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Arithmetic Operator Operation Example + addition x + y
Type Conversion, Constants, and the String Object
Increment and Decrement
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Chapter 2: Basic Elements of Java
College of Computer Science and Engineering
Digital Logic & Design Lecture 02.
Arithmetic Expressions & Data Conversions
Chapter-3 Operators.
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.
Chapter 3 Operators and Expressions
Chapter 7 Expressions and Assignment Statements.
Arithmetic Operations
Primitive Types and Expressions
OPERATORS in C Programming
Operator King Saud University
OPERATORS in C Programming
Arithmetic Expressions & Data Conversions
Presentation transcript:

What are they? Why do we need them? Casts What are they? Why do we need them? Uses Scanner Copyright © 2005-2009 Curt Hill

Mixed Mode Arithmetic Multiple types in one arithmetic expression 3.5 * 2 A real and integer constant Most programming languages allow some form of mixed mode C and C++ are quite liberal Java somewhat more restrictive Copyright © 2005-2009 Curt Hill

Better Example Suppose: Scanner inp = new Scanner(System.in); int a=2, b = inp.nextInt(); double d = inp.nextDouble(); d = d*b – a; // Legal a = b*d – a; // Not legal What is the difference? One loses information and the other does not! Copyright © 2005-2009 Curt Hill

Conversions Java will perform certain conversions automatically Converting an integer value to a double value is automatic Doubles are stronger This conversion does not lose anything The conversion from double to int will not be done automatically Copyright © 2005-2009 Curt Hill

When is the conversion done? Consider the following: int a=2, b=3; double d = 3; d = a/b; What do we get? From an algebra view: 2/3 should be .6666… Actually we get zero Copyright © 2005-2009 Curt Hill

Why zero? Java only looks at the immediate pair of operands and refuses to look ahead So it first does a/b, which is 2/3 Since both are integers, we do integer division This gives a zero Copyright © 2005-2009 Curt Hill

Observations Java is very short sighted Even though the resulting expression of: d = a/b*d; is double, it does no conversion until needed It avoids some thornier problems by doing so Copyright © 2005-2009 Curt Hill

So what is the fix? How do we get 2/3 instead of zero? A cast A cast is a conversion of type from one type to another The variable type is not changed! We extract the value and change the type of that value Copyright © 2005-2009 Curt Hill

The Java Cast The form of a cast is the type in parenthesis This is a unary operator, it takes the operand on its right and converts the value into the appropriate type: (double)a/b It has very high precedence so it occurs before the division Copyright © 2005-2009 Curt Hill

The 2/3 problem solved We have two ways to accomplish what we wanted: d = (double)a/b; d = a/(double)b; These work, but this does not: d = (double)(a/b); The damage is already done Copyright © 2005-2009 Curt Hill

More on casts The above example helped us to strengthen a weak type (int) into a stronger type (double) We can also go the other way: int a=2, b=inp.nextInt(); double d = inp.nextDouble(); a = b*d – a; // Not legal a = (int) (b*d-a); // Legal Copyright © 2005-2009 Curt Hill

Rounding How is a double converted into an int? The pentium may round in four ways: Towards zero (truncation) Toward positive infinity (ceiling) Towards negative infinity (floor) Towards closest (regular rounding) Java only believes in truncation! (int)0.99999 is zero! How do we round? Simple: add 0.5 prior to the cast (int)(0.9999 + 0.5) is (int)(1.4999) is 1 Copyright © 2005-2009 Curt Hill

Dollars and Cents Suppose we hold a monetary value in a double Many manipulations will produce fractional cents Loan payment calculations Interest calculations Since we cannot split the penny, how do we round? Copyright © 2005-2009 Curt Hill

Rounding Dollars and Cents We may only round to an integer, so we first make the quantity an integer number of cents, not dollars Multiply by 100 Add the 0.5 Cast Divide by 100 Like so: d = (int)(d*100 + 0.5)/100.0; Copyright © 2005-2009 Curt Hill

Precedence Chart Again Highest to lowest ( ) - + casts (Unary) * / % + - (Binary arithmetic) = Yes, there are quite a few more! Copyright © 2005-2009 Curt Hill