Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Java Planning our Programs Flowcharts Arithmetic Operators.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
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.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
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.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
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.
Mathematical Calculations in Java Mrs. G. Chapman.
Java Data Types Assignment and Simple Arithmetic.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC 107 – Programming For Science. The Week’s Goal.
Mixing integer and floating point numbers in an arithmetic operation.
02 Variables1November Variables CE : Fundamental Programming Techniques.
Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Mathematical Calculations in Java Mrs. C. Furman.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Building java programs, chapter 3 Parameters, Methods and Objects.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
What are Operators? Some useful operators to get you started.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
CS 201 Lecture 2: Elementary Programming Tarik Booker CS 201 California State University, Los Angeles.
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.
CSC Programming for Science Lecture 5: Actual Programming.
CompSci 230 S Programming Techniques
Today’s topic: Arithmetic expressions.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Introduction to Computer Science / Procedural – 67130
Assignment and Arithmetic expressions
OPERATORS (2) CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Numerical Data Types.
Data Types, Concatenation, PEMDAS, Shortcuts, and Comments
CS 1054 Introduction to Programming in Java
Presentation transcript:

Today’s topic:  Arithmetic expressions

Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary (one arg) operators  -, +  Order of operations just like you’d expect:  Please Excuse My Dear Aunt Sadie.  Can use () to override.

Arithmetic expressions  How would we code the following?  Let n be 2 and k be 2n+1.

Arithmetic expressions  How would we code the following?  Let n be 2 and k be 2n+1. int n = 2; int k = 2*n+1; Or int n, k; n = 2; k = 2 * n + 1;

Arithmetic expressions  How would we code the following?  Let n be 2 and k be 2n+1. int n = 2; int k = 2*n+1; Note: The rhs of the equals is always evaluated first and is assigned to the lhs. Therefore, you cannot say: int 2 = n; Or 2*n+1 = k;

Arithmetic expressions  Example from physics: F = M A (force = mass x acceleration).  How would we code this in Java?

Arithmetic expressions  Example from physics:  F = M A (force = mass x acceleration).  How would we code this in Java? double mass = 12.0; double acc = 13.9; double force = mass * acceleration; System.out.println( "force = " + force );

Differences between int and double  int (integer) and double (real numbers) are different.  int is a subset of double but ints are much faster to calculate than doubles  So use ints whenever you can but be aware of the following: int n = 5 / 2; int k = 9 / 10;

Differences between int and double  5 is an int.  5.1 is a double.  5.0 is a double.  5.7f is a float  Range of values:  int-2 billionto+2 billion  double4.9x to1.8x  float1.4x to3.4x10 38

Converting doubles to ints  When we convert, we will typically lose something.  Consider converting 12.9 to an int. There is no int  So the compiler will issue a message. To inform the compiler that we really wish to do this, we can use a cast. int i; double d = 12.9; i = (int) d; //What do you think is in i now?

Converting doubles to ints int i; double d = 12.9; i = (int) d; //What do you think is in i now? //how can we round instead?

Let’s check what we’ve learned so far (using jGrasp). class FirstExample { public static void main ( String[] params ) { //arithmetic//output}}

ints and mod (%)  What is the result of the following: int k = 5 / 2; What is the remainder? % is used to calculate the remainder. int remainder = 5 % 2;

Float vs. double  Recall range of values:  double4.9x10-324to1.8x  float1.4x10-45to3.4x10+38  Recall that we used a cast to convert a double to an int:  double d = 12.9;  int i = (int) d;  This is because a double won’t fit into an int. Will a double fit into a float?

Increment and decrement operators (++ and --)  What does the following do:  int i = 12;  i = i + 2;

Increment and decrement operators (++ and --)  We often see code like the following: int value = 12; … value = value + 1; … …

Increment and decrement operators (++ and --)  We often see code like the following: int value = 12; … value = value + 1; … … int value = 12; … value++; //post increment … ++value; //pre increment …value++;

Increment and decrement operators (++ and --)  We often see code like the following: int value = 12; … value = value - 1; … … int value = 12; … value--; //post decrement … --value; //pre decrement …value--;

Increment and decrement operators (++ and --)  We can use these operators in expressions as well (but don’t get carried away): int where = 92; int k = 7; where = k++; int j = --where;

Convert the following to Java expressions:

Write a program that... class MyProgramTemplate { public static void main ( String param[] ) { System.out.println( "hi there" ); } //end main } //end class

Write a program that Declares a variable for the radius of a circle with a value of Calculates the circumference. 3. Calculates the area. 4. Prints out the radius, circumference, and area. class MyProgramTemplate { public static void main ( String param[] ) { System.out.println( "hi there" ); } //end main } //end class

Write a program that Declares a variable for the radius of a circle with a value of Calculates the circumference. 3. Calculates the area. 4. Prints out the radius, circumference, and area. class MyProgramTemplate { public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of //Calculate the circumference. //Calculate the area. //Print out the radius, circumference, and area. } //end main } //end class

Write a program that... class MyProgramTemplate { public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of //Calculate the circumference. //Calculate the area. //Print out the radius, circumference, and area. } //end main } //end class What types of things are radius, circumference, and area?

Write a program that... public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of double radius = 12.7; //Calculate the circumference. //Calculate the area. //Print out the radius, circumference, and area. } //end main

Write a program that... public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of double radius = 12.7; //Calculate the circumference. double circ = ?; //Calculate the area. //Print out the radius, circumference, and area. } //end main

Write a program that... public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of double radius = 12.7; //Calculate the circumference. double circ = 2 * 3.14 * radius; //Calculate the area. //Print out the radius, circumference, and area. } //end main

Write a program that... public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of double radius = 12.7; //Calculate the circumference. double circ = 2 * Math.PI * radius; //Calculate the area. //Print out the radius, circumference, and area. } //end main

Write a program that... public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of double radius = 12.7; //Calculate the circumference. double circ = 2 * Math.PI * radius; //Calculate the area. double area = Math.PI * radius * radius; //Print out the radius, circumference, and area. } //end main

Write a program that... public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of double radius = 12.7; //Calculate the circumference. double circ = 2 * Math.PI * radius; //Calculate the area. double area = Math.PI * radius * radius; //Print out the radius, circumference, and area. System.out.println( " radius = " + radius ); ? } //end main

Write a program that... public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of double radius = 12.7; //Calculate the circumference. double circ = 2 * Math.PI * radius; //Calculate the area. double area = Math.PI * radius * radius; //Print out the radius, circumference, and area. System.out.println( " radius = " + radius ); System.out.println( " circumference = " + circ ); System.out.println( " area = " + area ); System.out.println( " Bye."); } //end main

Write a program that... public static void main ( String param[] ) { System.out.println( "hi there" ); //Declare a variable for the radius of a circle with a value of double radius = 12.7; //Calculate the circumference. double circ = 2 * Math.PI * radius; //Calculate the area. double area = Math.PI * radius * radius; //Print out the radius, circumference, and area. System.out.print( " radius = " + radius + ", " ); System.out.print( " circumference = " + circ + ", " ); System.out.println( " area = " + area + "." ); System.out.println( " Bye."); } //end main