CSCE 102 – Chapter 11 (Performing Calculations) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-12-22 Benito Mendoza 1 By Benito Mendoza.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

Computer Programming w/ Eng. Applications
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
The Web Warrior Guide to Web Design Technologies
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
CIS101 Introduction to Computing Week 10 Spring 2004.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
CIS101 Introduction to Computing Week 10. Agenda Your questions Final exam and final project CIS101 Student Survey Class presentations: Your Mad Libs.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JavaScript Assignment Statements Expressions Global Functions CST 200 JavaScript.
JavaScript 101 Lesson 3: Variables and Arithmetic.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
JavaScript Functions. CSS Inheritance Which formatting applies? x y z input { display: block; } input.pref { background:red; } If you have a selector.
Tutorial 11 1 JavaScript Operators and Expressions.
CSCE 102 – Chapter 8 (Objects and Variables ) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
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.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
A variable is a name for a value stored in memory.
Math class Random() method Floor method Top-level parseInt function
Chapter 6 JavaScript: Introduction to Scripting
Computing and Statistical Data Analysis Lecture 2
2.5 Another Java Application: Adding Integers
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Functions Comp 205 Fall ‘17.
Programming the Web using XHTML and JavaScript
JavaScript: Functions
JavaScript Functions.
Basic Elements of C++ Chapter 2.
Number and String Operations
A First Book of ANSI C Fourth Edition
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Core Objects, Variables, Input, and Output
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
JavaScript: Introduction to Scripting
Primitive Types and Expressions
Data Type Conversion ICS2O.
Presentation transcript:

CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza Department of Computer Science & Engineering

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 2 Calculator Goal: create a calculator that determines the area and perimeter of a rectangle Need: Input fields for width and length Output fields for area and perimeter Ability to perform calculations Ability to invoke calculation function

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 3 Calculator Mathematical operators + Addition - Subtraction * Multiplication / Division

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 4 Calculator Math with constants var result1, result2, result3 result1 = result2 = 2 * 4 result3 = 21 / 7

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 5 Calculator Math with variables var result1, result2, result3 result1 = result2 = 4 * result1

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 6 Calculator Changes are not retroactive var x, y, z x = 12 y = 5 z = x + y What’s the value of z at this point? x = 6 Now what’s the value of z?

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 7 Calculator Spaces ignored x=3*4 Equivalent

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 8 Calculator Problems: Text box values are stored as strings Can’t do mathematical operations on strings To convert a string into a real numeric value use the parseFloat() function Use parseInt() to convert a string into an integer value

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 9 Calculator var xStr = “75.2”, yStr=“10.1” var xNum=parseFloat(xStr) var yNum=parseFloat(yStr) var z = xNum + yNum Ch11-Ex-01

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 10 Calculator Running totals var x x = 0 … x = x + 1

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 11 Calculator Remember order of operations in an assignment statement x = x + 1 Do this first Then store result here

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 12 Calculator var x … x = 35 … x = x + 15 … Value of x before the next statement is executed? Value of x just after the previous statement is executed?

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 13 Calculator Increment operator ++x = 4 x = x + 1x++ (result 5) Ch11-Ex-02

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 14 Calculator Decrement operator –x = 4 x = x - 1x-- (result 3)

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 15 Calculator Equivalent forms: x++ and ++x x-- and --x Unless used in an assignment statement…

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 16 Calculator If operator follows variable then Assignment first Increment second x = 5 y = x++ Result: y = 5, x = 6

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 17 Calculator If operator precedes variable then Increment first Assignment second x = 5 y = ++x Result: y = 6, x = 6

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 18 Calculator Combination assignment operators x += 5 (x = x + 5) x -= 5 (x = x – 5) x *= 5 (x = x * 5) x /= 5 (x = x / 5)

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 19 Calculator Problem: decrement operator has specific use in XHTML syntax Can’t use in JavaScript and stay compliant with XHTML Solution: put JavaScript code in an external JavaScript file (filename.js) Include only JavaScript, not elements Call by

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 20 Calculator Precedence of operations (order of operations) Increment and decrement Multiplication and division Addition and subtraction Combination Left to right for multiples of the same operation Use parentheses to override

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 21 Calculator * ( 2 * 4 )

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 22 Calculator * * ( 2 * 4 ) – ( 7 * 8 ) – ( 7 * 8 ) – –

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 23 Calculator (9 + 2) * * 8 (9 + 2) * 4 – ( 7 * 8 ) (9 + 2) * 4 – 56 (11 * 4) – –

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 24 Calculator The Math object Math.methodname(…) Math methods: Square root – Math.sqrt(x) Power – Math.pow(x,n) Round – Math.round(x) [if.5 round up, else down] Floor – Math.floor(x) [truncates decimal portion] PI – Math.PI [as in Math.PI*radius] Random – Math.random() [value between 0 and 1] Ch11-Ex-03

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 25 Returning Values from Functions Scope of a variable Global – can be used anywhere in the source document Local – can be used only within a specific function

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 26 Returning Values from Functions Ch11-Ex-04

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 27 Returning Values from Functions Ch11-Ex-05 var firstAnswer = 93.7 var secondAnswer = "New York" alert("Answer #1 is "+firstAnswer) alert("Answer #2 is "+secondAnswer) function testVar() { var secondAnswer="Florida" alert("Answer #2 in testVar is "+secondAnswer) } alert("Answer #1 is "+firstAnswer) alert("Answer #2 is "+secondAnswer) testVar() alert("Answer #2 is "+secondAnswer) Known only within the testVar function

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 28 Returning Values from Functions Declaring the variable (using var) makes it local Local variables can’t be changed outside their function That means other programming team members won’t write code that changes your variables Ch11-Ex-05

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 29 Returning Values from Functions Rules of Thumb Use var keyword to make a variable local Declare all global variables in section Declaring a variable without using var makes it global no matter where the declaration occurs Variables are known only to the web page where they are used, i.e., you can’t use variables from one page, load a new page, and expect those variable values to still be there – they won’t be.

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 30 Returning Values from Functions Similar to using parameters except… The function itself has a value x = 2 x = sqrt(4) The sqrt function is equivalent to the number 2 when called with a parameter of 4.

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 31 Returning Values from Functions So far we’ve used functions to do things and store the results in variables Now, we’re going to make the functions themselves have value.

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 32 Returning Values from Functions Declaration: function calcAvg(n1, n2, n3) { var average average = (n1 + n2 + n3) / 3 alert(“The average is “+average) } Call: calcAvg(1, 2, 3)

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 33 Returning Values from Functions Declaration: function calcAvg(n1, n2, n3) { var average average = (n1 + n2 + n3) / 3 return average } Call: x = calcAvg(1, 2, 3)

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 34 Returning Values from Functions Declaration: function calcAvg(n1, n2, n3) { return (n1 + n2 + n3) / 3 } Call: x = calcAvg(1, 2, 3) Ch11-Ex-06

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 35 Finally, the calculator Goal: create a calculator that determines the area and perimeter of a rectangle Need: Input fields for width and length Output fields for area and perimeter Ability to perform calculations Ability to invoke calculation function

CSCE 102 – Chapter 11 (Performing Calculations) Benito Mendoza 36 Debugging Rectangle Calculator function calcArea(l, w) { var area = l*w } function calcPerim(l, w) { var perimeter = 2*(l+w) } function calc(f) { var width = parseFloat(widthBox) var length = parseFloat(lengthBox) area = calcArea(length.value, width.value) areaBox.value = area perimeterBox.value = calcPerim(length.value, width.value) } Area and Perimeter Calculator Enter the width of the rectangle: Enter the length of the rectangle: <input type="button" value="Calculate" onclick="calc(document.calculatorForm)" /> The area is: The perimeter is: