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.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Arithmetic Calculations
Building Java Programs
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Computer Programming w/ Eng. Applications
Return values.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development.
Numerical Data Recitation – 01/30/2009
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
Topic 10 return values, Math methods Based on slides bu Marty Stepp and Stuart Reges from " Thinking like a computer.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Python Programming: An Introduction to Computer Science
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
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.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,
CSC Programming I Lecture 5 August 30, 2002.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of the equals is an expression. Expressions can be anything.
Mathematical Calculations in Java Mrs. G. Chapman.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Introduction to Computer Programming Math Random Dice.
Variables and Expressions, continued CMSC 201. Expressions Anything on the right hand side of an assignment is an expression. An expression is anything.
Introduction to Java Primitive Types Operators Basic input and output.
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
1 CS 177 Week 2 Recitation Slides Introduction. 2 Announcements.
Mathematical Calculations in Java Mrs. C. Furman.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
The Math Class Methods Utilizing the Important Math Operations of Java!
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Math Class Mrs. C. Furman September 2, Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Chapter 4 Mathematical Functions, Characters, and Strings
Lecture 6: While Loops and the Math Class
Chapter 4 Mathematical Functions, Characters, and Strings
Math Methods that return values
Multiple variables can be created in one declaration
Primitive and Reference Data Values
Working with Text and Numbers in Java
Chapter 4: Mathematical Functions, Characters, and Strings
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Arithmetic Expressions & Data Conversions
Building Java Programs
Unit 3: Variables in Java
Using java libraries CGS3416 spring 2019.
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Arithmetic Expressions & Data Conversions
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

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 in Java that represent numbers: int – Integer numbers int – Integer numbers double – Numbers with a decimal double – Numbers with a decimal Ex. Ex. double myValue = 10.5;//store 10.5 in the variable double myValue = 10.5;//store 10.5 in the variable int myInt = 7;//stores 7 in the variable myInt int myInt = 7;//stores 7 in the variable myInt The math operators for int and double are: The math operators for int and double are: * multiplication * multiplication / division / division + addition + addition - subtraction - subtraction % remainder (only for int) % remainder (only for int)

Casting When data types are converted, we use a technique called casting to tell java to do the conversion for us When data types are converted, we use a technique called casting to tell java to do the conversion for us When conversion results in a loss of data, the casting must be told to java When conversion results in a loss of data, the casting must be told to java Ex. Ex. double dVal = 10.5; int iVal = dVal;//error, you will lose 0.5 To tell java that you are OK with losing the 0.5 you have to rewrite the line as: To tell java that you are OK with losing the 0.5 you have to rewrite the line as: int iVal = (int)dVal;//(int) tells java you are aware of the possible loss of data Ex. Ex. int iVal = 7; double dVal = iVal; No problem this time, no information would be lost when storing an int in a double. No problem this time, no information would be lost when storing an int in a double.

What is printed int iVal1 = 6; int iVal2 = 10; double dVal1 = 4.0; double dVal2 = 3.0;  System.out.println( iVal2 / (int)dVal1); // 2  System.out.println( iVal2 / (double)iVal1); //  System.out.println( (double)iVal2 / (int)dVal1); //2.5  System.out.println( (int)dVal2 / (int)dVal1); //1  System.out.println( (int)dVal1 / iVal1); //0

Integer Remainder So what happens to the decimal when dividing integers? So what happens to the decimal when dividing integers? It is possible to find out how much was left over in the division, also known as the remainder It is possible to find out how much was left over in the division, also known as the remainder For example, For example, 5 / 3 is 1 remainder 2 5 / 3 is 1 remainder 2 7 / 2 is 3 remainder 1 7 / 2 is 3 remainder 1

(%) Mod Operator With integers, the % operator determines the remainder after division has occurred With integers, the % operator determines the remainder after division has occurred Ex. Ex. 10 % 6 = 4 10 % 6 = 4 12 % 4 = 0 12 % 4 = 0 32 % 5 = 2 32 % 5 = 2 18 % 5 = 3 18 % 5 = 3

Mod Operator Mod Operator has many uses, one common one is telling time Mod Operator has many uses, one common one is telling time If 350 minutes have past since midnight, what time is it? If 350 minutes have past since midnight, what time is it? There are 60 minutes in an hour There are 60 minutes in an hour 350 / 60 = / 60 = % 60 = % 60 = 50 The time is 5:50am The time is 5:50am

New - Shortcut Operators Some operators are shortcuts to common expressions: Some operators are shortcuts to common expressions: OperatorExpression Equivalent To += x += y; x = x + y; -= x -= y; x = x – y; *= x *= y; x = x*y; /= x /= y; x = x / y; ++x++; x = x+1; --x--; x = x – 1;

The Math Class The Math class is a special class called a static class, there is no need to create a Math object, instead we ask the class directly The Math class is a special class called a static class, there is no need to create a Math object, instead we ask the class directly Static class Static class Math.PI//value of PI Math.PI//value of PI Non static class Non static class String PI = “3.14”;//make a new string String PI = “3.14”;//make a new string

Math class All of the buttons you find on your calculator are in the Math class All of the buttons you find on your calculator are in the Math class sin, cos, tan sin, cos, tan sqrt, pow, etc. sqrt, pow, etc. Of course you can find them all in the JavaDoc Of course you can find them all in the JavaDoc Another things you’ll need to use in the Math class is random numbers Another things you’ll need to use in the Math class is random numbers

Numeric Operations The Math class can be used by putting the following line under the package statement: The Math class can be used by putting the following line under the package statement: import java.lang.Math; import java.lang.Math; import – command tells java to get code from another library import – command tells java to get code from another library

Math Methods double Math.ceil( double value ) double Math.ceil( double value ) This method rounds up a floating point number This method rounds up a floating point number It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double ceil = Math.ceil( 10.9 ); //returns 11.0 double ceil = Math.ceil( 10.9 ); //returns 11.0 double ceil = Math.ceil( 3.1 ); //returns 4.0 double ceil = Math.ceil( 3.1 ); //returns 4.0 double ceil = Math.ceil( -1.9 ); //returns -1.0 double ceil = Math.ceil( -1.9 ); //returns -1.0 int ceil = (int)Math.ceil( -1.2 ); //returns -1.0 int ceil = (int)Math.ceil( -1.2 ); //returns -1.0

Math Methods double Math.floor( double value ) double Math.floor( double value ) This method rounds down a floating point number This method rounds down a floating point number It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double floor = Math.floor( 3.1 ); //returns 3.0 double floor = Math.floor( 3.1 ); //returns 3.0 double floor = Math.floor( 10.9 ); //returns 10.0 double floor = Math.floor( 10.9 ); //returns 10.0 double floor = Math.floor( -1.2 ); //returns -2.0 double floor = Math.floor( -1.2 ); //returns -2.0 int floor = (int)Math.floor( -1.9 ); //returns -2.0 int floor = (int)Math.floor( -1.9 ); //returns -2.0

Math Methods double Math.pow( double base, double exponent ) double Math.pow( double base, double exponent ) This method returns the power of (base) exponent This method returns the power of (base) exponent It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double power = Math.pow( 2, 3 ); //returns 8.0 double power = Math.pow( 2, 3 ); //returns 8.0 double power = Math.pow( 3, 4 ); //returns 81.0 double power = Math.pow( 3, 4 ); //returns 81.0 double power = Math.pow( 0.1, 5 ); //returns double power = Math.pow( 0.1, 5 ); //returns int power = (int)Math.pow( 1.5, 2 );//returns 2.25 (it is cast to 2) int power = (int)Math.pow( 1.5, 2 );//returns 2.25 (it is cast to 2)

Math Methods double Math.abs( double value ) double Math.abs( double value ) This method returns the absolute value of value This method returns the absolute value of value It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double absolute = Math.abs( 100 ); //returns double absolute = Math.abs( 100 ); //returns double absolute = Math.abs( -4.5); //returns 4.5 double absolute = Math.abs( -4.5); //returns 4.5 double absolute = Math.abs( 0.57 ); //returns 0.57 double absolute = Math.abs( 0.57 ); //returns 0.57 int absolute = (int)Math.abs( ); //returns 1234 int absolute = (int)Math.abs( ); //returns 1234

Math Methods double Math.sqrt( double value ) double Math.sqrt( double value ) This method returns the square root of value This method returns the square root of value It also returns a floating point so be sure to cast if you’re putting the result in an int It also returns a floating point so be sure to cast if you’re putting the result in an int Ex. Ex. double root = Math.sqrt( 100 ); //returns 10.0 double root = Math.sqrt( 100 ); //returns 10.0 double root = Math.sqrt( 2 ); //returns double root = Math.sqrt( 2 ); //returns double root = Math.sqrt( 0.25 ); //returns 0.5 double root = Math.sqrt( 0.25 ); //returns 0.5 int root = (int)Math.sqrt( 16.0 ); //returns 4 int root = (int)Math.sqrt( 16.0 ); //returns 4

Making a Formula in Java The quadratic formula: The quadratic formula: Using the order of operations we get, Using the order of operations we get, Math.pow( b, 2 ))Math.sqrt( – 4*a*c–1*b+()/ 2*ax1 = x2 = (–1*b – Math.sqrt( Math.pow( b, 2 ) – 4*a*c )) / 2*a Can you rewrite the other solution?

Math.random() Random numbers can be generated from the random method of the Math Class Random numbers can be generated from the random method of the Math Class static double random(); static double random(); returns a random number between 0.0 (inclusive) and 1.0 (exclusive) returns a random number between 0.0 (inclusive) and 1.0 (exclusive) Ex. Ex. double randomNumber = Math.random() * 100; double randomNumber = Math.random() * 100; This is a random number between 0.0 and This is a random number between 0.0 and

Using Math.random Ex. Ex. double randomNumber = Math.random() * 35; double randomNumber = Math.random() * 35; Between 0.0 and Between 0.0 and double randomNumber = Math.random() * ; double randomNumber = Math.random() * ; Between 10.0 and Between 10.0 and int randomNumber = (int)(Math.random() * 20) + 5; int randomNumber = (int)(Math.random() * 20) + 5; Between 5 and 24 Between 5 and 24 don’t forget to cast with ints don’t forget to cast with ints Notice the range is 5 to 24! Notice the range is 5 to 24! Math.random() * 20 returns 0 to … Math.random() * 20 returns 0 to … (int)(Math.random() * 20) chops off all decimals (int)(Math.random() * 20) chops off all decimals Returns 0 to 19 Returns 0 to 19

Be Careful When Casting! (int)Math.random() * ; (int)Math.random() * ; returns 5 every time your program is run: returns 5 every time your program is run: Generate a random value, say Generate a random value, say Cast it to an it Cast it to an it Now it has the value 0 Now it has the value 0 0 * 20 = 0 0 * 20 = = = 5

Using Math.random If you want to generate a number in the range [a,b)  not including b (as doubles) If you want to generate a number in the range [a,b)  not including b (as doubles) Math.random()*(b – a) + a Math.random()*(b – a) + a If you want to generate a number in the range [a,b] (as ints) If you want to generate a number in the range [a,b] (as ints) (int)(Math.random()*(b – a+1)) + a (int)(Math.random()*(b – a+1)) + a