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.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
Advertisements

Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
LAB SESSION-5 Topics covered  User input using keyboard. java  User input using JoptionPane  Arithmetic expression…………….  Math class……………………………  Testing………………………………….
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.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
Creating a Power Point Presentation Jason Flear November 29, 2004.
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.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
Copyright © Curt Hill Mathematics Functions An example of static methods.
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,
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
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,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Method Abstraction You can.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
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.
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically.
Introduction to Computer Programming Math Random Dice.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Using Java Class Library
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.
APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall.
Lecture 5.2 Primitive Expression Patterns. © 2006 Pearson Addison-Wesley. All rights reserved A pattern is a general template that can be applied.
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.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
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.
Random Numbers. Are a series of numbers that have no pattern to them Ex) 7, 31, 4, 9, 8, 99… Random Numbers are used in… - Computer Games - Lotteries(6-49)
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.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Computer Science 112 Fundamentals of Programming II.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in Math Class Functions.
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.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Chapter 4 Mathematical Functions, Characters, and Strings
Swap Pattern Example Code Pattern: Swap preAssertion
Chapter 5 The if Statement
Lecture 6: While Loops and the Math Class
Math Class Your favorite class!.
Chapter 4 Mathematical Functions, Characters, and Strings
Building Java Programs
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)
Chapter 5 Methods.
Lecture 6: While Loops and the Math Class
Using java libraries CGS3416 spring 2019.
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

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. This means there is only one copy. You cannot create an object of the class. You will use the class name to call the methods.

Math Class methods Tested on the AP Method Summary abs(double x) Math.abs(36.0);           Returns the absolute value of a double value. abs(int x) Math.abs(36);           Returns the absolute value of an int value. pow(double base, double exponent) Math.pow(2,4);           Returns the value of baseexponent. sqrt(double x) Math.sqrt(4);           Returns the square root of a number.

Mathematical expressions to Java

Math.random() The Math.random() method returns random double numbers in the range >=0.0 to <1.0 .   x= Math.random(); // assigns random number to x x = 0.0 to 0.99999999999999999999999 To get a desired number you multiply and cast to an int. int n = (int)(Math.random()* 10) This will produce a number starting at 0 inclusive and up to 10 exclusive. Number will be from 0 to 9

int n = (int)(Math.random()* 10) Starts at 0 and goes to < 10. Because 0-9 is 10 numbers. This will produce a number starting at 0 inclusive and up to 10 exclusive. Number will be from 0 to 9

Math.random To include the 10 You would add + 1   int n = (int)(Math.random()*10+1) This will produce a number starting at 1 inclusive and up to 10 exclusive. Number will be from 1 to 10

Math.random Create a number starting with 20 and up to 50 not including 50. The + will be the number you start with. 20 + 30 = 50   int n = (int)(Math.random()*30 )+20; Produce 20 to 49 29 + 20 = 49 This will produce a number starting at 20 inclusive and up to 50 exclusive.

Generating Random Numbers Generate a number from 65 inclusive and 90 exclusive    A random decimal between 0 inclusive & 5 exclusive A random integer between 10 inclusive & 45 exclusive A random integer between 3 inclusive & 12 inclusive int num = (int) (Math.random() * 25) + 65 24 + 65 = 89 65 to 89 int num = (int)(Math.random()*5); int num = (int)(Math.random()*35); + 10 34+10 = 44 10 to 44 int num = (int)(Math.random()*10+3) 9 +3= 12 3 to 12