Mathematical Calculations in Java Mrs. G. Chapman.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Expressions, Data Conversion, and Input
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Java Data Types Assignment and Simple Arithmetic.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Mathematical Calculations in Java Mrs. C. Furman.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Doing math In java.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Expressions.
Chapter 2 Basic Computation
Lecture 3 Java Operators.
Expressions.
Introduction to Computer Science / Procedural – 67130
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Chapter 2 Basic Computation
OPERATORS (1) CSC 111.
Escape Sequences What if we wanted to print the quote character?
Type Conversion, Constants, and the String Object
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Building Java Programs
Arithmetic Expressions & Data Conversions
Building Java Programs Chapter 2
Expressions and Assignment
Data Types and Expressions
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Data Types and Expressions
Building Java Programs
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Mathematical Calculations in Java Mrs. G. Chapman

Review of Declaring Variables int sum; typeidentifier This statement declares sum. It reserves a space in memory large enough to store an int.

Assignments Statements We are initializing sum to 120. We use the assignment operator ( = ) to give a value to a variable. This is not used for comparison. (Does sum equal 120?) sum = 120; identifer assignment value operator

Variables- The value of a variable can be changed. public class Geometry { public static void main (String [] args) { int sides = 7; System.out.println (“A heptagon has “ + sides + “ sides.”); sides = 10; System.out.println (“A decagon has “ + sides + “ sides.”); sides = 12; System.out.println (“A dodecagon has “ + sides + “ sides.”); } In Memory: sides

Output of previous code Output: A heptagon has 7 sides. A decagon has 10 sides. A dodecagon has 12 sides. What is happening in memory? When ever sides is assigned a new value, it writes over the previous value. So, when sides is assigned 10, it “forgets” the 7, and now holds the 10. This happens again when it is assigned 12… it “forgets” the 10 and now holds 12. Once it is assigned a new value, the old one is lost forever.

Assignment Statements sides = 10; The value on the right is given to the variable on the left. This does not mean “equals” for comparison!! The value that is assigned must be of the same type. sides = 5.5; This will generate an type mismatch error.

Arithmetic Expressions Expressions: combinations of operators and operands to perform calculations. The value calculated does not have to be a number, but often is.

Arithmetic Operators int and double operators: + addition -subtraction * multiplication /division % modulus or remainder

Modulus Operator % % (mod) remainder operator – returns the remainder when you divide 2 integers or doubles. Example: int result = 7 % 3; Remainder result 1

2 Types of Division 1. integer division – when both operands are of type integer, we truncate the fractional part. result = 7 / 3; 2. floating point division – if one or both operands are floating point numbers, then the fractional part is kept. double dresult = 7.0 / 3.0; result 2 dresult …

Order of Operations Java has to follow the order of operations when computing an expression As a reminder if operators are on the same level, we work left to right. Assignments work right to left. Parenthesis Multiplication, Division, Modulus Addition, Subtraction, String Concatenation Assignment

+ with Strings + is the concatenation operator with Strings. It is used to “add” Strings together. I put “add” in double quotes, because Strings are called immutable… meaning their value can never change. (Read next slide)

Strings are immutable This means that Strings can never change. When using methods or operators on Strings, we aren’t changing Strings rather creating new String. Recall: String class is very special. It is the only class that we can create objects without using the keyword “new”. When ever there is a String literal, or addition of Strings, etc. Strings are automatically created.

String addition System.out.println (“A dodecagon has “ + sides + “ sides.”); Working left to right… the String “A dodecagon has “ is being added with an int. The integer is “promoted” to a String… a new String is created: “12” The 2 Strings are added together: “A dodecagon has 12” Now we add this String with the String “ sides.” “A dodecagon has 12 sides.” This is a brand new String that is output to the user.

The Problem with Concatenation System.out.println (“The value is “ ); Every time Java sees a String it promotes what is being added to also be a String… so… When the first to pieces are added our result is a String: “The value is 24” 24 is no longer and int!! When this is concatenated with 25, we get our output above. Output: The value is 2425

Fixing the Problem We need to use parenthesis: System.out.println (“The value is “ + ( )); By using parenthesis, we force Java to add 24 and 25 first, resulting in 49. Then we do our String addition… 49 is promoted to a String, and our result is…. The value is 49

Type Conversions Sometimes we will need to change the type of a variable in order to use it with another variable or value. Values can be widened or narrowed. Widening: If you are going from a type that has a smaller memory storage to one with a larger memory storage, you are widening. int to double Narrowing: If you are going from a type with a larger memory storage to a smaller memory storage, you are narrowing. double to int *** When doing a Narrowing conversion, we run the risk of losing data.

Types of Conversions There are 3 types of conversions: 1. Arithmetic Promotion 2. Assignment Conversion 3. Type Casting

Arithmetic Promotion This is a widening conversion that will change the type so that arithmetic operations can be performed. A perfect example is concatenating a String with an int. Another example: double dresult = 7.0 / 2; 2 gets promoted to a double for floating point division. This type of conversion is done automatically, and does not require any intervention on the part of the programmer.

Assignment Conversion If we assign an int to a double, we are converting it to a double. int startingValue = 3; double targetValue = startingValue; The 3 stored in startingValue is changed to a double, and then assigned. We can not go the other way around: startingValue = targetValue; This will generate a type mismatch error.

Type Casting We can force a variable to change type by using typecasting. In front of the variable you wish to change, put the new type in parenthesis. Example: double ans = (double)7 / 3; This will change 7 into a double so that we are doing floating point division rather than integer division. ans = , rather than 2.0.

Typecasting a double an int public class Money { private double amount; public Money (double initialAmt) { amount = initialAmt; } public int dollarAmount() { return (int)amount; } By typecasting amount an int, we truncate (remove) the decimal portion of the number. It is NOT rounded. Ex. if amount = 5.50; the value being returned by return (int)amount; is 5.