1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Building Java Programs
Computer Programming w/ Eng. Applications
Return values.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Pemrograman Dasar - Data Types1 OPERATOR. Pemrograman Dasar - Data Types2 Arithmetic operator  + - * /  / operator denotes integer division if both.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
Data types, declarations, and expressions in Java.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 More on Assignment and Console Input Overview l Increment & Decrement Operators l Short-cut Operators l Casting l Math class l Console Input (TextIO.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
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.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
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,
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
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.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Mathematical Calculations in Java Mrs. C. Furman.
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 *
The Math Class Methods Utilizing the Important Math Operations of Java!
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,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
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.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
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.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Variables, Operators, and Expressions
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
TMF1414 Introduction to Programming
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Fundamental of Java Programming Basics of Java Programming
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Numerical Data Types.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Expressions and Assignment
elementary programming
Data Types and Expressions
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The Math Class l Evaluating an arithmetic expression with simple math function-Example l Casting

2 Assignment Statement variable = expression; l The value of constants / variables / expression in the right side of the = operator, is assigned to the variable in the left side of the = operator. l Examples: a = 5; b = a; c = a + b; l The left hand side of the = operator must be a variable. l It cannot be a constant or an expression. l Example of an invalid assignment statement : a + b = c ;

3 Assignment Statement l Java allows multiple assignment. int start, end; int width = 100, height = 45, length = 12; start = end = 0; l By default, Java takes whole numbers to be of type int and real numbers to be of type double. However, we can append a letter at the end of a number to indicate its type. l Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L, but we should prefer L): float maxGrade = 100f ; // now holds ‘100.0’ double temp = 583d ; // holds double precision float temp = 5.5 ; // ERROR! // Java treats 5.5 as a double long x = 583l ; // holds 583, but looks like 5,381 long y = 583L ; // This looks much better!

4 Increment or Decrement Operators Increment/decrement operations ( count = count +1 ) are very common in programming. Java provides operators that make these operations shorter. OperatorUse Description ++ op ++ Increments op by 1; evaluates to a value before incrementing op Increments op by 1; evaluates to a value after incrementing -- op -- Decrements op by 1; evaluates to a value before decrementing op Decrements op by 1; evaluates to a value after decrementing

5 Increment or Decrement Operators Example : 1. What is the value of j and i after executing the following code? i = 1; j = 5; j = ++i; 2. What is the value of j and i after executing the following code? i = 10; j = 50; j = i--; 3. What is the value of j and i after executing the following code? i = 5; j = 10; i++; ++j;

6 Short Hand Operators Java also provides a number of operators that can be used as a short-cut for performing arithmetic operations on a variable and assigning the result to the same variable. Operator Short-FormEquivalent to += op1 += op2 op1 = op1 + op2 -= op1 -= op2 op1 = op1 - op2 *= op1 *= op2 op1 = op1 * op2 /= op1 /= op2 op1 = op1 / op2 %= op1 %= op2 op1 = op1 % op2 Example : Instead of writing a = a + 5 We can write a += 5 If the variable name on both sides of assignment operator are same then bring the operator, before the = operator. a = a + 5

7 The Math class l Java provides a number of mathematical function as static methods in the Math class. E The best approximate value of e, the base of the natural log PI The best approximate value of  abs (a) Returns the absolute value of a, a can be double, float, int or long. cos (a) sin (a) tan (a) Returns the trigonometric cosine/sine/tangent of an angle in radians exp (a) Returns the exponential number e raised to the power of a log (a) Returns the natural logarithm (base e) of a max (a, b) min (a, b) Returns the greater/smaller of two values, a and b can double, float, int or long pow (a, b) Returns value of the first argument raised to the power of the second random () Returns a random value in the range 0.0 and 1.0 round (a) Returns the closest long to the argument, the argument can be double or float toDegrees(a) toRadians(a) Converts an angle in radians to the degrees Converts an angle in degrees to the radians

8 Evaluating Arithmetic Expression With Simple Math functions - Example class Expressions { public static void main(String[]args) { double area, circumference; int radius=3; area = Math.PI * Math.pow(radius, 2); circumference = 2 * Math.PI * radius; System.out.println("Area = " + area); System.out.println("Circum. =" + circumference); }

9 Casting We learnt earlier that the following division 5 / 2 results in 2 Because the / operator is operating between 2 integer type constants, the result will be an integer. To get 2.5, we need to convert either 1 or both the operands to double. Then the division will look like 5.0 / 2.0 But what if we have integer variables to divide each other, like a / b ? For this, cast operator is used. (double) a / (double) b

10 Casting (Cont’d) Conversion of primitives is accomplished by (1) assignment and/or (2) explicit casting: int total = 100; float temp = total; // temp now holds When changing type that will result in a loss of precision, an explicit ‘cast’ is needed. This is done by placing the new type in parenthesis: float total = 100F; int temp = total; // ERROR! int start = (int) total;