Operators Chapter 4 - Lecture Slides Math, Conditional operators, conversions They aren’t all just objects to me, some of them are primitive.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
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”
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Variables Chapter 9 - Student Naming, data types, instance variables, math and conversions.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CPS120: Introduction to Computer Science Operations Lecture 9.
Mathematical Calculations in Java Mrs. G. Chapman.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Mathematical Calculations in Java Mrs. C. Furman.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Doing math In java.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Operators.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
What are Operators? Some useful operators to get you started.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Programming Principles Operators and Expressions.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
CompSci 230 S Programming Techniques
Lecture 3 Java Operators.
University of Central Florida COP 3330 Object Oriented Programming
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
2.5 Another Java Application: Adding Integers
University of Central Florida COP 3330 Object Oriented Programming
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
Type Conversion, Constants, and the String Object
Operators and Expressions
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Examples of Primitive Values
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.
Chapter 2: Java Fundamentals
Chapter 2 Programming Basics.
5.03 Apply operators and Boolean expressions
Primitive Data Types and Operators
Arithmetic Expressions & Data Conversions
Presentation transcript:

Operators Chapter 4 - Lecture Slides Math, Conditional operators, conversions They aren’t all just objects to me, some of them are primitive.

Math

(c) by Elizabeth Sugar Boese 3 Math Math operators work as you would expect, with two that may be new to you: In Java, addition also works on strings by concatenating them together. - String fullname = "Elizabeth" + "Boese"; fullname is equal to “ElizabethBoese” - String result = "Version" + 2.0; result is equal to “Version2.0” OperatorJava codeDescription +op1 + op2 Adds op1 and op2 ; also used to concatenate strings -op1 - op2 Subtracts op2 from op1 *op1 * op2 Multiplies op1 by op2 /op1 / op2 Divides op1 by op2 %op1 % op2 Computes the remainder of dividing op1 by op2

(c) by Elizabeth Sugar Boese 4 Math OperatorJava codeDescription +op1 + op2 Adds op1 and op2 ; also used to concatenate strings -op1 - op2 Subtracts op2 from op1 *op1 * op2 Multiplies op1 by op2 /op1 / op2 Divides op1 by op2 %op1 % op2 Computes the remainder of dividing op1 by op2 The modulus operator % returns the int remainder after dividing op1 by op2 - int result = 9 % 3; result is equal to ___________ When would the use of the modulus function be useful? When do you use this technique in your daily life?

(c) by Elizabeth Sugar Boese 5 Java arithmetic Integer division  Throw away the remainder!  9 / 4 = Modulus  9 % 4 =  How would you test to see if the int variable named val is even? Widening conversions  9.0 / 4 =

(c) by Elizabeth Sugar Boese 6 Math class Can use the constants and methods from the Math class  Math.PI  Math.pow( double x, double y )  Math.round( double d )  Math.sqrt( double d )

(c) by Elizabeth Sugar Boese 7 Mathematical Expressions Express the following in Java: 1 time + 3mass rate 50 + amount

(c) by Elizabeth Sugar Boese 8 Relational Operators Relational operators return either true or false based on the operands Note that the greater-than-or-equal-to operator has the = AFTER the > and the less-than-or-equal-to operator has the = AFTER the < This is Java, so on an exam do not try to put  or  as our keyboards don't have these symbols!!! You have been warned. Note the difference between the two equal signs here and the assignment statement that contains only one equal sign. Examples: OperatorUseDescription >op1 > op2 Returns true if op1 is greater than op2 >=op1 >= op2 Returns true if op1 is greater than or equal to op2 <op1 < op2 Returns true if op1 is less than op2 <=op1 <= op2 Returns true if op1 is less than or equal to op2 ==op1 == op2 Returns true if op1 and op2 are equal !=op1 != op2 Returns true if op1 and op2 are not equal

(c) by Elizabeth Sugar Boese 9 Logical Operators < Less than x < 5 <= Less than or equal to x <= 5 == Equal x == 5 != Not equal x != 5 >= Greater than or equal to x >= 5 > Greater than x > 5 Trials: when x = -3, when x = 5, when x = 6

(c) by Elizabeth Sugar Boese 10 Careful testing Equality!! Two primitive values (except floating point data types) are the same under == if their values are the same For two class-type variables, == is true ONLY if the variables REFER TO THE SAME OBJECT!

(c) by Elizabeth Sugar Boese 11 Conditional Operators Conditional operators return either true or false based on boolean operands Examples OperatorUseDescription &&op1 && op2 Returns true if op1 and op2 are both true ||op1 || op2 Returns true if either op1 or op2 is true !!op Returns true if op is false

(c) by Elizabeth Sugar Boese 12 Truth Tables a b !a!ba&& ba || b!a&&ba || (b && !a) FF FT TF TT

(c) by Elizabeth Sugar Boese 13 The wonderful instanceof Operator Test whether a variable is a particular class type by using the instanceof operator Variable instanceof class-type B instanceof JButton B instanceof MyOwnButton Mb instanceof JFrame Can not check if something is an instance of a primitive data type!

(c) by Elizabeth Sugar Boese 14 Expressions Precedence “order of operations”  Parenthesis first  unary operators (pos/neg, if before variable, !)  *, /, %  +, -  >= instanceof  == !=  &&  ||  =  3 * ( 2 + ( 3 – 4 * 2 + (5-1 ) ) ) =

Conversions

(c) by Elizabeth Sugar Boese 16 String to numbers Call methods on the class ( Integer, Double, Character, etc.) Class:Method Integer parseInt( String ) DoubleparseDouble( String ) Examples int value = Integer.parseInt( string ); double value = Double.parseDouble( string ); int numShirts = Integer.parseInt( textfieldNumShirts.getText( ) );

(c) by Elizabeth Sugar Boese 17 Numbers to String Call method from the String class: String.valueOf( number ) Examples To set the text inside a JTextField, we have to send it a String (not an int or double ). So we can convert a number to a String with String.valueOf( number ) tfcartTotal.setText( String.valueOf( totalCost ) ); What's another 'hack' way to convert a number to a String ? append the number to an empty String (2 dbl-quotes)  String tfcartTotal.setText( “” + totalCost );

(c) by Elizabeth Sugar Boese 18 Summary Math  Arithmetic  Relational operators  Logical  Equality  Conditional  Order of precedence Conversions  String to number  Number to String