Arithmetic You can perform arithmetic with numbers and/or variables. Java follows mathematical order of operations (PEMDAS). Example: 4 + 36 / 2  this.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Constants A constant is a variable whose value is expected to remain the same throughout a program It is considered “good programming style” to use constants.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
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  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
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.
Basic Input/Output and Variables Ethan Cerami New York
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
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?
Input, Output, and Processing
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Variables and Expressions CMSC 201 Chang (rev )
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Mathematical Calculations in Java Mrs. G. Chapman.
Java Data Types Assignment and Simple Arithmetic.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
If Statements If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on.
Mixing integer and floating point numbers in an arithmetic operation.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Chapter 2 Variables.
Mathematical Calculations in Java Mrs. C. Furman.
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.
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
Welcome to AP Computer Science A We use the Java language, but this class is much more rigorous than Intro to Java More programming, but also more theory.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Operators.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
int [] scores = new int [10];
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
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.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
Chapter 2 Variables.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Math in C The math blocks you've used in Scratch can all be recreated in C!
Chapter 2 Edited by JJ Shepherd
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Data Types, Concatenation, PEMDAS, Shortcuts, and Comments
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.
Other types of variables
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Arithmetic Expressions & Data Conversions
Python Creating a calculator.
Presentation transcript:

Arithmetic You can perform arithmetic with numbers and/or variables. Java follows mathematical order of operations (PEMDAS). Example: / 2  this equals 22, not 20.

We input data from whoever is using the program by using the SavitchIn “class” A “class” is another Java program (usually written by someone else) that you can use without having to rewrite it each time To ask the user for an integer, we use: SavitchIn.readLineInt( ) Example: int testScore = 0; System.out.println(“Enter test score: “); testScore = SavitchIn.readLineInt( );

The SavitchIn command changes, depending on what kind of variable you are asking for –Int: SavitchIn.readLineInt( ) –Double: SavitchIn.readLineDouble( ) –Char: SavitchIn.readLineNonwhiteChar( ) (why nonWhite?? This way if the user types spaces -- “white space” -- before whatever they type, Java ignores the spaces) –String: SavitchIn.readLine( ) Demo

Type Casting Let’s say you have a double that you need to convert into an integer This can be accomplished by type casting: int x = 0; double y = 3.7; x = (int)y; // the (int) is where the type casting occurs

Integer vs. Double division Guess the result of the following: int x = 5; int y = 2; int z = x/y; Dividing with integers: the decimal part of the answer is truncated (erased). To be accurate, use at least one double, or one decimal in your division.

int x = 5; int y = 2; double z = x/y; what does z equal now? 2.0

int x = 5; double y = 2; double z = x/y; what does z equal now? 2.5

int x = 5; int y = 2; double z = (double)(x/y); what does z equal now? 2.0 why? because x/y = 2, then 2 is type-casted (converted) into a double.

int x = 5; int y = 2; double z = (double)x/y; what does z equal now? 2.5 why? Notice the slight change in parentheses from the previous slide. Now x is type-casted from 5 to 5.0. Next, 5.0/2 = 2.5. Remember, as long as one of the two numbers being divided is a double, then the result is a double.

Variables do not have to be used in order to work with doubles and integers. For example: System.out.println(9/4); (this would display 2) System.out.println(9.0/4); (this would display 2.25) System.out.println(9/4.0); (this would display 2.25) System.out.println(9.0/4.0); (this would display 2.25) Notice that as long as one of the two numbers being divided is a double, then the answer will be a double.

Modulus Division Using the modulus symbol, %, determines the remainder 10 % 6 = 4 13 % 5 = ? = % 100 = ? = % 2 = ? = 0 27 % 2 = ? = 1 (any even #) % 2 = ? = 0 (any odd #) % 2 = ? = 1

Assignment (InputNumbers) –Ask the user to enter 2 numbers. – Display the product, and quotient of the two numbers. – Display the first number, squared. – Display the second number, cubed. – Display the name of the drummer for the Canadian rock band Rush, followed by “rules.” – All of the results should look like the following, for example: “The sum of 6 and 7 is 13.”

More Assignments Yay (files should be named with this format: P117num4) P : –#4 (Ignore “Create… application to”; look up formula online) –#5 (remember: “floating point value” is another name for a double) –#6 –#7 (hints: use both modulus division and integer division; how many seconds are in an hour?) –#11 (start by asking user for # of gallons used, starting odometer #, and ending odometer #) –#12 (ask the user how many of each coin; don’t worry about the decimal problem for now)