Java Data Types Assignment and Simple Arithmetic.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
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.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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.
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.
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,
Operaciones y Variables
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Unit 3: Java Data Types Math class and String class.
Java Data Types Data types, variable declaration, and initialization.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
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.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Mathematical Calculations in Java Mrs. G. Chapman.
CSC 107 – Programming For Science. The Week’s Goal.
Primitive Data Types. Identifiers What word does it sound like?
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.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Chapter 2 Variables.
Mathematical Calculations in Java Mrs. C. Furman.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
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.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
CSC Programming for Science Lecture 5: Actual Programming.
Variables, Operators, and Expressions
Expressions.
Chapter 2 Basic Computation
Lecture 3 Java Operators.
Expressions.
Object Oriented Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Chapter 2 Basic Computation
Math in C The math blocks you've used in Scratch can all be recreated in C!
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Numerical Data Types.
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Expressions and Assignment
Data Types and Expressions
Primitive Types and Expressions
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Java Data Types Assignment and Simple Arithmetic

Some recap You can think of a variable as a container that you could use more than once to hold different values at different times during program execution. Variables are commonly used to store the results of arithmetic operations and an assignment statement is the place for doing such operations in a program. int a = 2; int b = 3; int c = a * b;// c = 6

Operators + for addition! - for subtraction! * for multiplication! / for division! () for grouping! Order of operations Multiplication and division are performed before addition and subtraction. Other than that, operations are performed from left to right. int a = 3 * / 4;// a = 9

Arithmetic with variables and numbers (constants) Arithmetic may include both variables and numeric values. Given: int myInteger1 = 10; int myInteger2 = 20; int myInteger3 = 30; double myDouble1 = 10.0; double myDouble2 = 20.0; double myDouble3 = 30.0; These statements would be valid: int myInteger = 4 + myInteger3; = 34 double myDouble = myDouble2 * myDouble3; 20.0 * 30.0 = 600.0

ARGH! NOT MORE CONVERSION! What about statements like this? myDouble1 = myInteger1 – myDouble2; 10 – 20.0 Type conversion shows itself once again. The previous statement is valid, the integer converts to a double and the subtraction is performed, no questions asked. All types on the right side are promoted to the type present which can hold the most information.

MY MIND!! This would not be valid: myInteger1 = myInteger2 / myDouble1; The results on the right are promoted to a double, but this can’t be stored in an integer. In general, it’s best not to rely on automatic conversion. When in doubt, explicitly cast all variables to the type you want. Like so: myInteger1 = myInteger2 / (int)myDouble1;

More casting confusion. Consider these two statements: myInteger1 = myInteger2 * (int) myDouble1; myInteger1 = (int)(myInteger2 * myDouble1); Although they might seem identical, the two statements will output different results, because casting (which may result in data loss) occurs at different points in the statement. Let’s say: myInteger2 = 2; myDouble1 = 1.5; This would output like so: 2 * (int) 1.5 = 2 * 1 = 2 (int)(2 * 1.5) = (int)(2.0 * 1.5) = (int)(3.0) = 3

Division with integers Consider: myInteger1 = myInteger2 / myInteger3; Division potentially leads to an answer with a fractional part. However, when integers are divided, only the whole part of the answer is kept. The result is a truncated integer value, and it is correct to store it in another integer variable. Let’s say myInteger2 = 3; myInteger3 = 4; Then: myInteger1 = (int)(3 / 4) = (int)(0.75) = 0

Modulus Would make for a really cool name for a robot or something. But it is also how you can get the remainder of an integer division operation. % - modulus operator. Given: myInteger3 = 30; myInteger2 = 20; Then: myInteger1 = myInteger3 % myInteger2; = 30 % 20 = (30 / 20) = 1, remainder 10 myInteger1 = 10;

Incrementing/Decrementing The variable on the left side of the = sign can be used in the arithmetic on the right: myInteger1 = myInteger1 + 1; myDouble1 = myDouble1 – 5.0; Things like that happen so much that there are shortcuts to it. myInteger++;//Increments by 1 ++ can increment a value by decrements by one. myDouble1 -= 5.0;//Decrements by 5 You could use any arithmetic symbol like this.

Declaration and arithmetic You are allowed to perform arithmetic as an assignment to a variable you just declared. int myInteger4 = myInteger2 + myInteger3; However, this is not recommended. This sort of practice may encourage the declaration of multiple variables with the same name as you go along your code. This would result in a syntax error! Keep it in mind that each variable you declare MUST have a different name than the others. It’s recommended that when you’re writing code later on, you declare all of the variables you would need at the top of the code, and then do assignment on the values later on.

Arithmetic inside print statements Assuming variables have been declared and assigned values, it is possible to write: System.out.println(myInteger1 + myInteger2); However, it shows lack of foresight and it has the shortcoming that the result of the arithmetic is not available for future use since it has not been saved in another variable. This is more acceptable: int myAnswer; myAnswer = myInteger1 + myInteger2; System.out.println(myAnswer);

Floating Point Arithmetic Dangers Recall that numeric data is stored internally as binary numbers. The results of some decimal arithmetic may not have exact representations in binary. Ergo, ‘answers’ made by the system may be incorrect. This is due to the way floating point values are stored in binary. However, the answer will be so close to what you expect that it is nearly insignificant in most applications. For example, you may get when you are expecting 0.5.

Lab Questions on the assignment sheet. Show what the output of each question would be. If the question cannot output anything (that is, there is an error in the question), show what the error is. If there is something stylistically wrong with the code, but you think it would still work, just show the output.