CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.

Slides:



Advertisements
Similar presentations
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Advertisements

Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
LAB SESSION-5 Topics covered  User input using keyboard. java  User input using JoptionPane  Arithmetic expression…………….  Math class……………………………  Testing………………………………….
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Computer Programming Lab(5).
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.
Lecture 2: Static Methods, if statements, homework uploader.
Chapter 3 Introduction to Object-Oriented Programming: Using Classes.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Object Oriented Programming I
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,
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
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,
Topics Class Basics and Benefits Creating Objects Using Constructors Calling Methods Using Object References Calling Static Methods and Using Static Class.
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
***** 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.
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.
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.
10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
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.
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.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Floating Point ValuestMyn1 Floating Point Values Internally, floating point numbers have three pairs: a sign (positive or negative), a mantissa (has a.
ICS 3U Math Class. ActionScript 3 – Math Class Rounding ceil – closest integer >= number (rounds up) floor – closest integer
CS001 Introduction to Programming Day 6 Sujana Jyothi
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
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.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Chapter 5 The if Statement
Lecture 6: While Loops and the Math Class
Mathematical Functions
Formatting Output & Enumerated Types & Wrapper Classes
Building Java Programs
Building Java Programs
Building Java Programs
Functions, Part 2 of 2 Topics Functions That Return a Value
Building Java Programs
Object Oriented Systems Lecture 03 Method
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Chapter 3, cont Sept 20, 2004.
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
Chapter 5 Methods.
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
class PrintOnetoTen { public static void main(String args[]) {
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
The Random Class The Random class is part of the java.util package
Building Java Programs
Building Java Programs
Using java libraries CGS3416 spring 2019.
Lecture 03 & 04 Method and Arrays Jaeki Song.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS

Topic Math Library Class.

About the Math Library class The Math class provides static constants and static methods for performing common calculations. The Math class is in the java.lang package, so it does not need to be imported The methods of this class are all static. – A static method is a method that can be invoked (called) by using the name of the class that it belongs and the. Operator – For example to call the method random ( a method that produces random numbers) we use Math.random(); 3

Math Library class Checking the API documentation for random we see the description: public static double random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. 4

Math Library class Two static constants PI - the value of pi E - the base of the natural logarithm Example: System.out.println( Math.PI ); System.out.println( Math.E ); The output is:

Sample Methods of the Math Class Return typeMethod name and argument list dataTypeOfArgabs( dataType arg ) returns the absolute value of the argument arg, which can be a double, float, int or long. doublelog( double a ) returns the natural logarithm (in base e) of its argument. doublesqrt( double a ) returns the positive square root of a double pow( double base, double exp ) returns the value of base raised to the power exp

The Math min/max Methods Find smallest of three numbers: int smaller = Math.min( num1, num2 ); int smallest = Math.min( smaller, num3 ); See Example 3.16 in Text -MathMinMaxMethods.java Return typeMethod name and argument list dataTypeOfArgsmin( dataType a, dataType b ) returns the smaller of the two arguments. The arguments can be doubles, floats, ints, or longs. dataTypeOfArgsmax( dataType a, dataType b ) returns the larger of the two arguments. The arguments can be doubles, floats, ints, or longs.

Another Example program using the Math Class import java.util.Scanner; public class MyMathClass { public static void main(String[] args) { double exponent=4.0; double result=0.00; double sqrt=0.00; result=Math.pow(10, exponent); sqrt=Math.sqrt(exponent); System.out.println("The result is"+“ "+result); System.out.println("The square root of the number”+” “++exponent+” ”+” is"+” “+sqrt); } 8