Math Class Mrs. C. Furman September 2, 2010. Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of.

Slides:



Advertisements
Similar presentations
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Advertisements

Introduction to Computers and Programming Introduction to Methods in Java.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
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.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
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.
Computers and Scientific Thinking David Reed, Creighton University Functions and Randomness 1.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Topic 12 more if/else, cumulative algorithms, printf Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
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,
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.
Exposure Java-A 2006 Chapter 4 Slides Using Methods and Parameters
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
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.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
The Math Class Methods Utilizing the Important Math Operations of Java!
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.
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
Review for Nested loops & Math class methods & User defined methods.
Let’s not leave anything to chance. How that process to generate random numbers takes places requires some complicated statistics that are outside the.
Creating and Using Class Methods. Definition Class Object.
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.
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
Chapter 4 Mathematical Functions, Characters, and Strings
Lecture 6: While Loops and the Math Class
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Mathematical Functions, Characters, and Strings
Building Java Programs
Math Methods that return values
Building Java Programs
Topic 10 return values, Math methods
Chapter 3 Methods.
Working with Text and Numbers in Java
Building Java Programs
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.
Java's Math class Method name Description Math.abs(value)
Building Java Programs
Topic 10 return values, Math methods
Chapter 5 Methods.
Lecture 6: While Loops and the Math Class
Building Java Programs
Building Java Programs
Using java libraries CGS3416 spring 2019.
Random Numbers while loop
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Math Class Mrs. C. Furman September 2, 2010

Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of y abs()returns the absolute value sqrt()returns the square root round()rounds the nearest whole number max(x,y)returns bigger of x and y min(x,y)returns smaller of x and y random()returns a double in the range [0, 1.0)

All methods in the Math class are static. All methods in the Math class are static. Static methods do not require the creation of an object to invoke them (use them). Static methods do not require the creation of an object to invoke them (use them). Static methods are invoked through the class name. Static methods are invoked through the class name. When we have methods that will give the same result regardless of the object, we use static methods. We would want sqrt() method to compute the square root of the number the same every time, regardless of the individual object that may be created. When we have methods that will give the same result regardless of the object, we use static methods. We would want sqrt() method to compute the square root of the number the same every time, regardless of the individual object that may be created.

Math.floor(3.254) Math.ceil(2.45) Math.pow(2,7) Math.abs(-9) Math.sqrt(256) Math.round(3.6) Math.max(5,7) Most Math methods return a double, but some do return integer values. We use the class name, Math, to call these methods, because they are static. = 3.0 = 3.0 = = 9.0 = 16 = 4.0 = 7

//math return methods import static java.lang.Math.*; public class MathMethods { public static void main ( String[] args ) { System.out.println(Math.floor(3.254));//= 3.0 System.out.println(Math.ceil(2.45));//= 3.0 System.out.println(Math.pow(2,7)); //= System.out.println(Math.abs(-9));//= 9 System.out.println(Math.sqrt(256));//= 16.0 System.out.println(Math.sqrt(144));//= 12.0 System.out.println(Math.round(3.6));//= 4 System.out.println(Math.max(5,7));//= 7 System.System.out.println(Math.max(5,-7));//= 5 System.out.println(Math.min(5,7));//= 5 System.out.println(Math.min(5,-7));//= -7 } Type this into Dr. Java and run it. Change the numbers and see how it changes your output.

Some of the programming that is done in the real world is with games. Games must have the ability to vary or be random; thus, you must have the ability to generate random numbers. Math.random(); // returns a random number //between 0 up to, but not //including 1.

public class RandomDemo { public static void main ( String[] args ) { double dblans; int intans; dblans = Math.random() * 10; intans = (int)Math.random() * 10; //this line needs help System.out.println("\nMath.random()"); System.out.println( dblans ); System.out.println( intans ); //why does it always output 0? } Math.random() * 10; returns a double between 0 and (int) Math.random() * 10; When we typecast a double as an int, we get a number between 0 and 9 inclusively

public class RandomDemo { public static void main ( String[] args ) { double dblans; int intans; dblans = Math.random() * 10; intans = (int)(Math.random() * 10); System.out.println("\nMath.random()"); System.out.println( dblans ); System.out.println( intans ); } How does the addition of parenthesis change this program?

Random Numbers How can you use Math.random() to simulate rolling a die? How can we get it to return the numbers 1 – 6? How can you use Math.random() to simulate rolling a die? How can we get it to return the numbers 1 – 6? (int)(Math.random()*6) + 1;