Arithmetic expressions containing Mathematical functions.

Slides:



Advertisements
Similar presentations
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Advertisements

ME1107 Computing Y Yan
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
1 Algorithms and Problem Solving. 2 Outline  Problem Solving  Problem Solving Strategy  Algorithms  Sequential Statements  Examples.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example:
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Using the Java programming language compiler. Review of relevant material from previous lectures From previous lectures: A computer can only execute machine.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Shorthand operators.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
The character data type char
Lecture 2: Static Methods, if statements, homework uploader.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Mixing integer and floating point numbers in an arithmetic operation.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
Computer Science I Lab 1 ISMAIL ABUMUHFOUZ | CS 180.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
Operators and Expressions
An Introduction to Java – Part I, language basics
The Boolean (logical) data type boolean
Building Java Programs
The for-statement.
Consider the following code:
Presentation transcript:

Arithmetic expressions containing Mathematical functions

Scientific calculations often involve Mathematical functions, such as sin, cos, tan, log, and so on. Examples: sin and ln functions on a calculator

Arithmetic expressions containing Mathematical functions (cont.) Programming languages allow you to embed Mathematical functions within a arithmetic expression. Furthermore, a computer allows you to use variables are arguments to a Mathematical function

Arithmetic expressions containing Mathematical functions (cont.) In other words: Suppose you have define a variable x as follows: You can write expressions like these: double x = 2.0; sin(x) computes the sin of x sin(2*x) computes the sin of 2*x

Arithmetic expressions containing Mathematical functions (cont.) Note: The names of the sin, cos, tan, ln, and other Mathematical functions in Java is a little bit different than what you are used to. They are given below

Java's function library The Java programming language has an extensive collection of functions organized in various libraries Note: a bit of history In Mathematics, the sin() operation is called a function In Java, the sin() operation is implemented by a Java method In fact, we used the words "function" and "method" interchangeably in Computer Science. Before the word method became popular, other words used were: subroutine, procedure and function

Java's function library The documentation of the functions are online at the following website: x.html We will now look at the Mathematical functions which are documented on this webpage: lang/Math.html

Java's Mathematical functions Here are some of the names of Java's Mathematical functions:

Using Java's Mathematical functions Example: compute √2 Math.sqrt(2.0) = √2

Using Java's Mathematical functions (cont.) You can apply a Mathematical function on a floating point variable Examples: double a; Math.sqrt(a) Math.sqrt(a+1) will compute the squared root on the value that is current stored in the variable a will compute the squared root on the value a+1

A note on "computing values" in a computer program We just learned how to compute √2 Consider the following Java program: public class CompVal { public static void main(String[] args) { Math.sqrt(2.0); // Computes √2 }

A note on "computing values" in a computer program (cont.) Interesting result: When you compile and run this program, you will see.... absolutely nothing !!!

A note on "computing values" in a computer program (cont.) Example Program (Demo above code) –Prog file: CompVal.java How to run the program: Right click on link and save in a scratch directory To compile: javac CompVal.java To run: java CompVal

A note on "computing values" in a computer program (cont.) The reason is: Unlike a calculator that always shows the result of a computation on its display.... A computer will only show the result of a computation when it is told !!!

A note on "computing values" in a computer program (cont.) Example: This program will print: (which is the decimal value of √2) public class CompVal2 { public static void main(String[] args) { System.out.println( Math.sqrt(2.0) ); // Print !! } }

A note on "computing values" in a computer program (cont.) Programming facts: A computed value is not printed If you want to print a computed value, use a print statement A computed value is not stored If you want to store (save) a computed value, use an assignment statement

A note on "computing values" in a computer program (cont.) Example: storing a computed value public class CompVal3 { public static void main(String[] args) { double a; a = Math.sqrt(2.0); // Save computed value in variable System.out.println(a); // You can print the saved value later }

A real-life example: programming the a,b,c-formula Quadratic equation: Solutions: x 1 = x 2 =

A real-life example: programming the a,b,c-formula (cont.) The Mathematical expressions are written in Java as follows: written as: ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a) written as: ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a)

A real-life example: programming the a,b,c-formula (cont.) Here is a Java program to compute the famous a,b,c- formula: public class Abc { public static void main(String[] args) { double a, b, c, x1, x2; // Define 5 variable a = 1.0; b = 0.0; c = -4.0; x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a); x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);

A real-life example: programming the a,b,c-formula (cont.) System.out.print("a = "); System.out.println(a); System.out.print("b = "); System.out.println(b); System.out.print("c = "); System.out.println(c); System.out.print("x1 = "); System.out.println(x1); System.out.print("x2 = "); System.out.println(x2); }

A real-life example: programming the a,b,c-formula (cont.) Example Program: (Demo above code) –Prog file: Abc.java How to run the program: Right click on link and save in a scratch directory To compile: javac Abc.java To run: java Abc