©2004 Brooks/Cole Chapter 3 Assignment. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Assignment In the last chapter we learned how to declare.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Computer Programming w/ Eng. Applications
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
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.
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.
©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
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,
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
1 Intro to Computer Science I Chapter 2 Fundamental Data Types Java scripting with BeanShell.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC Programming I Lecture 5 August 30, 2002.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Mathematical Calculations in Java Mrs. G. Chapman.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
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.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
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.
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.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Variables, Operators, and Expressions
Arithmetic Expressions
Expressions.
Expressions.
Chapter 3 Assignment and Interactive Input.
BIL 104E Introduction to Scientific and Engineering Computing
Chapter 2 Assignment and Interactive Input
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
Introduction to Programming
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
A First Book of ANSI C Fourth Edition
Arithmetic Expressions & Data Conversions
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.
CS2011 Introduction to Programming I Elementary Programming
Data Types and Expressions
Data Types and Expressions
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

©2004 Brooks/Cole Chapter 3 Assignment

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Assignment In the last chapter we learned how to declare variables Now, we need to learn to give them values We use the assignment operator, =, to give a variable a value variable = expression; –Compute the value of expression –Store the result in memory associated with variable

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Values Stored in the Variables factor = 10.6; weight = 155.0l; totalWeight = factor * weight;

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Reusing a variable sum = 25; sum = sum + 10;

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Accumulation We often need to accumulate values into a variable –adding up ten numbers entered by a user in order to be able to compute the average SubTotals.java –concatenating multiple strings together in successive statements BuildAString.java

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Increment and Decrement Successively adding/subtracting one to/from a variable is common enough to have a special operator –prefix increment ++n or decrement --count k = ++n;n = n + 1; k = n; –posfix increment n++ or decrement count-- k = n++;k = n; n = n + 1;

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Compound Assignment Assignments like var = var oper expr are common enough that there are special operators to express them += -= *= /= %= Examples x *= y; ---> x = x * y; count -= 1; ---> count = count - 1;

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Assignment operator = is considered to be an operator –It is right associative a = b = c = 25; ---> a = (b = (c = 25)); –It has the lowest precedence of all the operators

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Coercion What happens if the expression to the right of the = is a different type than the variable on the left? –if the variable has type with a wider range than the expression, the value of the expression will be coerced to the variables type –if the variable's type has a narrower range, the compiler will report an error double x = 3 * 5 / 4;// OK int n = ;// NOT OK

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Mixed-mode arithmetic Expressions can have more than one type in them 2 * 3.4f / 6.78; The type of the expression is that of the widest-range operand –The type of individual subexpressions is determined by the type of the two operands in the subexpression It may have a narrower range than the complete expression 2 / 3 * 2.5 has an integer division in it even though the final result is double

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Math class Math is a class in the java.lang package which contains a number of useful functions –most of the functions you find on a calculator - square root, powers, logs, trig and inverse trig functions, –a function to round floating point numbers to the nearest int –a constant (Math.PI) to represent the value of pi

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Math methods MethodDescriptionReturns abs(x)absolute valuesame type as argument pow(x1, x2)x1 x2 double sqrt(x)square root of xdouble log(x)natural log of xdouble round(x)round xint if x float, long if x double random()random number between 0.0 and 1.0 double sin(x) cos(x) tan(x) x should be in radians double

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using and Passing Data to a Math Class Method Calling syntax Math.methodName( data) Math identifies the class the method is defined in methodName identifies the method data contains the data the method is to act on –can be a literal value,a variable or an expression Examples Math.abs(-6.7) Math.sqrt(y) Math.pow( x * y + z, i * 3)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Passing Data to the Math Class’ abs() Method

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Casting What if we need to convert a variable of one type to a narrower type? –We can use a cast operation (dataType)expression –Casting has a high precedence (int)v1 op v2 //casts v1 only –Floating point values are truncated when cast to an integer type (int)3.75// result is 3

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Constants We often use values in a program that have a specific meaning – is the value of pi –12 is the number of months in the year Programs can be more readable if these values are given names. Since these values don't change, the should be constants –use the modifier final in a declaration of a variable that should not change –By convention, names of constants are all uppercase