Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.

Slides:



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

Types and Arithmetic Operators
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 Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
Data types and variables
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Data Types, Expressions and Functions (part I)
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
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.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Chapter 2: Using Data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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[]
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,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
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.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
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.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 Elementary Programming
Object Oriented 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.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Primitive and Reference Data Values
Numerical Data Types.
Arithmetic Expressions & Data Conversions
Chapter 3 Numerical Data
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.
Data Types and Expressions
Primitive Types and Expressions
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Chapter 3 Numerical Data

Topics Variables Numeric data types Assignment Expressions

Numeric Data and Operations So far, our programs have used only String data It is common to use computers for numeric calculations We can write literal numbers into our programs - just write the number We need a way to store numeric data - variables

Variables In mathematical expressions, we often use x and y to represent unknown values and to make a placeholder for many different values –y = x A variable is used in a similar way in a program –A variables has a Java identifier for a name.

Variables In the last chapter, we declared variables whose location stored the address of an object Now we need to learn to declare variables to store numeric data. A declaration sets aside memory locations to store data values. –These memory locations are called variables, and x and y are the names we associate with the memory locations. As before, we need to say what kind of data the variable represents.

Variables A variable has three properties: –A memory location to store the value. –The type of data stored in the memory location. –The name used to refer to the memory location.

Variable Declarations The syntax for declaring variables is ; where is a sequence of identifiers separated by commas. Every variable we use in a program must be declared.

Numeric Types There are six numeric data types in Java: –We nee to be able to use two basic kinds of numbers. –We have several types for each kind of number to allow for different ranges of values. Integers have discrete values (whole numbers). –byte –short –int –long Real numbers are needed for values that have fractional parts. –float –double

Assignment First we need to declare a variable. int a; We assign a value to a variable by using an assignment statement. a = 5; At the time a variable is declared, it can also be initialized. int count = 10, height = 34; Do not confuse mathematical equality and assignment. The following is not valid Java code: = x;

Varaibles for Objects and Numbers The only difference between a variable for numbers and a variable for objects is the contents in the memory locations. –For numbers, a variable contains the numeric value itself. –For objects, a variable contains an address where the object is stored.

Memory diagram for numeric data For numeric data, the actual value is stored in the memory location associated with the variable

Object Variables For objects, the location (memory address) for the object is stored in the memory location associated with the variable

Comparison

Variables We use the new command to create an object. –Objects are called reference data types, because the contents are addresses that refer to memory locations where the objects are actually stored. Numerical data are called primitive data types. –We don't need to use new with primitive types. –You do have to give the primitive variable a value.

Assignment Numeric DataObjects

Arithmetic Expressions OperationOperatorExpressionResult Addition+x + y17 Subtraction-y - z4.5 Multiplication*x * y70 Division/y / x0.7 Modulo (remainder) %x % y3 Assume x=10, y=7, z=2.5

Division and Modulo Division works differently for integer and floating point types –for floating point types you get a floating point result (what your calculator would give you) –for integers, you get an integer result –integer division and modulo together are what you first learned when you learned division in grade school 27 divided by 6 is 4 with a remainder of 3 so 27 / 6 gives a result of 4 and 27 % 6 gives a result of 3

Evaluation order In an expression with more than one binary operator, we need rules to tell us what order to do them in –What is 5 + x * 16 / y + 5 ? Precedence rules tell us which of two different operations should get done first –What is * 3 ? Associativity rules tell us which order operations of the same type get done in –What is 16 / 2 / 2 ?

Precedence rules for arithmetic operators and parentheses OrderGroupOperatorRule firstSubexpression()innermost first left to right Unary operation+, -single operand multiplicative operators *, /, %left to right lastadditive operators +, -left to right

Evaluation of Expressions Subexpression evaluation x + 3 * y

Mixed-mode Arithmetic What happens when the operands in your expression have different types? –x = 3.45 / 2 The hardware supports only single-type operations The value of a variable has to be stored in the appropriate format. Type conversions are used to convert mixed-type expressions to single type expressions

Type Conversions Widening conversions happen automatically –promote a value from one type into another which can represent a larger range of values converting an int to a double Narrowing conversions have to be programmed explicitly –information will be lost converting a double to an int –cast operator is the name of the type of the result enclosed in parentheses –(int)2.34 –fractional part will be truncated

Rules for arithmetic promotion Unary Operators 1.byte and short operands are converted to int Binary Operators 1.If either operand has type double, the other will be converted to a double 2.Otherwise, if either operand has type float, the other will be converted to a float 3.Otherwise, if either operand has type long, the other will be converted to a long 4.Otherwise, both operands are converted to int will be converted to an int

Constants If we want a variable to remain fixed, we use a constant. A constant is declared in a manner similar to a variable, but with the additional reserved word final. final double PI = ; final int MONTHS_IN_YEAR = 12;

Literal Constants If a literal constant contains a decimal point, it is of type double by default. To designate a literal constant of type float, append a letter f or F to the number: 2 * PI * F

Scientific Notation Numbers in scientific notation, such as Number x 10 exponent are expressed in Java using the syntax E 12.40e E-102

Getting Numerical Input Values Wrapper classes are used to perform necessary type conversions, such as converting a String object to a numerical value.

Getting Numerical Input Values radiusStr = JOptionPane.showInputDialog(null, "Enter radius:"); radius = Double.parseDouble(radiusStr);

The Math Class The Math class in the java.lang package contains class methods for commonly used mathematical functions. Some methods available in the Math class: –sqrt, abs, round –sin, cos, asin, … Math has the constant Math.PI in it