©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Operators.

Slides:



Advertisements
Similar presentations
This is Java Jeopardy.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
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.
JavaScript, Third Edition
Introduction to C Programming
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;
CS0007: Introduction to Computer Programming Introduction to Arrays.
Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
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.
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?
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
CSC Programming I Lecture 5 August 30, 2002.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Primitive Variables.
Chapter 2 Variables.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Mathematical Calculations in Java Mrs. C. Furman.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
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.
Recap……Last Time [Variables, Data Types and Constants]
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects.
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,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
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.
Lecture 4 – Scanner & Style
Chapter 2 Variables.
Lecture 2b- Java Fundamentals
Building Java Programs
Lecture 2: Data Types, Variables, Operators, and Expressions
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
OPERATORS (1) CSC 111.
Escape Sequences What if we wanted to print the quote character?
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Building Java Programs
Chapter 2 Variables.
Data Types, Concatenation, PEMDAS, Shortcuts, and Comments
Building Java Programs Chapter 2
Lecture 2b- Java Fundamentals
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Building Java Programs Chapter 2
Chapter 2 Variables.
Building Java Programs
Presentation transcript:

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Operators

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-2 Arithmetic Operators Java has five (5) arithmetic operators. OperatorMeaningTypeExample + AdditionBinary total = cost + tax; - SubtractionBinary cost = total – tax; * MultiplicationBinary tax = cost * rate; / DivisionBinary salePrice = original / 2; % ModulusBinary remainder = value % 5;

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-3 Arithmetic Operators The operators are called binary operators because they must have two operands. Each operator must have a left and right operator. See example: Wages.javaWages.java The arithmetic operators work as one would expect. It is an error to try to divide any number by zero. When working with two integer operands, the division operator requires special attention.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Wages.java // This program calculates hourly wages plus overtime. public class Wages { public static void main(String[] args) { // The calculated regular wages. double regularWages; // The base pay rate. double basePay = 25; // The hours worked less overtime. double regularHours = 40; // Overtime wages double overtimeWages; // Overtime pay rate double overtimePay = 37.5; // Overtime hours worked double overtimeHours = 10; // Total wages double totalWages; regularWages = basePay * regularHours; overtimeWages = overtimePay * overtimeHours; totalWages = regularWages + overtimeWages; System.out.println("Wages for this week are $" + totalWages); }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-5 Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is 0.5… But, that’s wrong. The answer is simply 0. Integer division will truncate any decimal remainder.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-6 Operator Precedence Mathematical expressions can be very complex. There is a set order in which arithmetic operations will be carried out. OperatorAssociativityExampleResult - (unary negation) Right to left x = ; * / % Left to right x = % 3 * ; Left to right x = – * 3; 23 Higher Priority Lower Priority

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-7 Grouping with Parenthesis When parenthesis are used in an expression, the inner most parenthesis are processed first. If two sets of parenthesis are at the same level, they are processed left to right. x = ((4*5) / (5-2) ) – 25; // result =

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-8 Combined Assignment Operators Java has some combined assignment operators. These operators allow the programmer to perform an arithmetic operation and assignment with a single operator. Although not required, these operators are popular since they shorten simple equations.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2-9 Combined Assignment Operators OperatorExampleEquivalentValue of variable after operation += x += 5;x = x + 5; The old value of x plus 5. -= y -= 2;y = y – 2; The old value of y minus 2 *= z *= 10;z = z * 10; The old value of z times 10 /= a /= b;a = a / b; The old value of a divided by b. %= c %= 3;c = c % 3; The remainder of the division of the old value of c divided by 3.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Creating Constants Many programs have data that does not need to be changed. Littering programs with literal values can make the program hard do read and maintain. Replacing literal values with constants remedies this problem. Constants allow the programmer to use a name rather than a value throughout the program. Constants also give a singular point for changing those values when needed.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Creating Constants Constants keep the program organized and easier to maintain. Constants are identifiers that can hold only a single value. Constants are declared using the keyword final. Constants need not be initialized when declared; however, they must be initialized before they are used or a compiler error will be generated.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Creating Constants Once initialized with a value, constants cannot be changed programmatically. By convention, constants are all upper case and words are separated by the underscore character. final int CAL_SALES_TAX = 0.725;