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.

Slides:



Advertisements
Similar presentations
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
Advertisements

Chapter 5 Methods to our madness. Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 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 5 Methods.
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
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Chapter 4 Mathematical Functions, Characters, and Strings
INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 08 Title: Methods, Part 2 Reference: MalikFarrell, chap 1, Liang, Ch 5.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Programming Fundamentals I (COSC-1336), Lecture 5 (prepared after Chapter 5 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 5.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Method Abstraction You can.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Methods 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Introducing Methods.
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.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Using Java Class Library
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma.
The Math Class Methods Utilizing the Important Math Operations of Java!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
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.
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.
Chapter 4: Methods Method Structure Method Structure Declaring Methods Declaring Methods Calling Methods Calling Methods Passing Parameters Passing Parameters.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 5 Methods.
Chapter 3 Methods.
Chapter 5 Methods.
Chapter 6 Methods.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 5 Methods.
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Chapter 6 Methods.
Chapter 5 Methods.
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Using java libraries CGS3416 spring 2019.
Chapter 4 Methods Introducing Methods Declaring Methods
Chapter 4 Mathematical Functions, Characters, and Strings
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

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 in a class library). A package is a collection of interrelated classes in the Java class library. Example of package: java.lang contains classes such as, Object, String, and System. java.awt provides classes such as, Button, TextField, and Graphics.

3 Java Library: System and PrintStream The java.lang.System class contains PrintStream objects that perform Input/Output (I/O). The java.lang.PrintStream class contains the print() and println() methods that perform output.

4 The import Statement Makes java classes available to programs under their abbreviated names – make the program a bit shorter and more readable. 2 possible forms: For example: the import statement import java.lang.System refers to the System class in java.lang package. import package.class import package.* Allows a specific class to be known by its abbreviated name. Allows all the classes in the specified package to be known by their short names.

5 The Scanner Class Java was designed primarily to receive input from a graphical user interface (GUI). Getting information from the keyboard in a console application is not convenient. We can use the Scanner class to simplify standard input

6 The Scanner Class (Cont) The Scanner class is defined in java.util, so we import java.util.Scanner; Scanner objects work with System.in To create a Scanner object, Scanner input1 = new Scanner (System.in) Scanner class methods are listed in page 78 in the text.

7 The Scanner Class (Cont) next() Reading a string. A string is delimited by spaces nextByte() Reading an integer of the byte type. nextShort() Reading an integer of the short type. nextInt() Reading an int type. nextLong() Reading an integer of the long type. nextFloat() Reading an integer of the float type. nextDouble() Reading an integer of the doublet type.

8 Java Library: java.lang.Math (The Math Class) public final class Math {// final class cannot be subclassed private Math() {} // private constructor cannot be invoked... public static native double sqrt (double a) throws ArithmeticException; } The java.lang.Math class provides common mathematical functions. All Math class methods are static class methods. They are invoked as follows: Math.sqrt(55.3) The Math class is a static class and can not be subclassed or instantiated.

9 The Math Class Class constants: PI E (A class constant is a final static variable). Class methods: Trigonometric Methods Exponent Methods Rounding Methods min, max, abs, and random Methods (All Math class methods are static class methods).

10 Trigonometric Methods The math class contain the following trigonometric methods: public static double sin(double radians) public static double cos(double radians) public static double tan(double radians) public static double acos(double radians) public static double asin(double radians) public static double atan(double radians) public static double toRadians(double degree) public static double toDegrees(double radians)

11 Trigonometric Methods sin(double a) cos(double a) tan(double a) acos(double a) asin(double a) atan(double a) Radians toRadians(90) Examples: Math.sin(0) returns 0.0 Math.sin(Math.PI / 6) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.cos(0) returns 1.0 Math.cos(Math.PI / 6) returns Math.cos(Math.PI / 2) returns 0

12 Exponent Methods There are five methods related to exponents in the Math class: public static double exp(double x) public static double log(double x) public static double log10(double x) public static double pow(double a, double b) public static double sqrt(double x)

13 Exponent Methods exp(double a) Returns e raised to the power of a. log(double a) Returns the natural logarithm of a. log10(double a) Returns the 10-based logarithm of a. pow(double a, double b) Returns a raised to the power of b. sqrt(double a) Returns the square root of a. Examples: Math.exp(1) returns 2.71 Math.log(2.71) returns 1.0 Math.pow(2, 3) returns 8.0 Math.pow(3, 2) returns 9.0 Math.pow(3.5, 2.5) returns Math.sqrt(4) returns 2.0 Math.sqrt(10.5) returns 3.24

14 Rounding Methods The Math class contains five rounding methods: public static double ceil(double x) public static double floor(double x) public static double rint(double x) public static int round(float x) public static long round(double x)

15 Rounding Methods double ceil(double x) x rounded up to its nearest integer. This integer is returned as a double value. double floor(double x) x is rounded down to its nearest integer. This integer is returned as a double value. double rint(double x) x is rounded to its nearest integer. If x is equally close to two integers, the even one is returned as a double. int round(float x) Return (int)Math.floor(x+0.5). long round(double x) Return (long)Math.floor(x+0.5).

16 Rounding Methods Examples 1. Math.ceil(2.1) 2. Math.ceil(2.0) 3. Math.ceil(-2.0) 4. Math.ceil(-2.1) 5. Math.floor(2.1) 6. Math.floor(2.0) 7. Math.floor(-2.0) 8. Math.floor(-2.1) 9. Math.rint(2.1) 10. Math.rint(2.0) 11. Math.rint(-2.0) 12. Math.rint(-2.1) 13. Math.rint(2.5) 14. Math.rint(-2.5) 15. Math.round(2.6f) 16. Math.round(2.0) 17. Math.round(-2.0f) 18. Math.round(-2.6)

17 Rounding Methods Examples 1. Math.ceil(2.1) returns Math.ceil(2.0) returns Math.ceil(-2.0) returns – Math.ceil(-2.1) returns Math.floor(2.1) returns Math.floor(2.0) returns Math.floor(-2.0) returns – Math.floor(-2.1) returns Math.rint(2.1) returns Math.rint(2.0) returns Math.rint(-2.0) returns – Math.rint(-2.1) returns Math.rint(2.5) returns Math.rint(-2.5) returns Math.round(2.6f) returns Math.round(2.0) returns Math.round(-2.0f) returns Math.round(-2.6) returns -3

18 min, max, and abs The min and max methods are overloaded to return the minimum and maximum numbers between two numbers (i.e int, long, float, or double) The abs methods are overloaded to return the absolute value of the number (int, long, float, and double)

19 min, max, and abs max(a, b) and min(a, b) Returns the maximum or minimum of two parameters. abs(a) Returns the absolute value of the parameter. Examples: Math.max(2, 3) returns 3 Math.max(2.5, 3) returns 3.0 Math.min(2.5, 3.6) returns 2.5 Math.abs(-2) returns 2 Math.abs(-2.1) returns 2.1

20 The random Method Generates a random double value greater than or equal to 0.0 and less than 1.0 (0 <= Math.random() < 1.0). Examples: In general,

21 View java.lang.Math Documentation You can view the complete documentation for the Math class online from

22 The Random Class You have used Math.random() to obtain a random double value between 0.0 and 1.0 (excluding 1.0). A more useful random number generator is provided in the java.util.Random class.

23 The Random Class Example If two Random objects have the same seed, they will generate identical sequences of numbers. For example, the following code creates two Random objects with the same seed 3. Random random1 = new Random(3); System.out.print("From random1: "); for (int i = 0; i < 10; i++) System.out.print(random1.nextInt(1000) + " "); Random random2 = new Random(3); System.out.print("\nFrom random2: "); for (int i = 0; i < 10; i++) System.out.print(random2.nextInt(1000) + " "); From random1: From random2:

24 Using Classes from the Java Library Example 7.1 declared the Circle1 class and created objects from the class. Often you will use the classes in the Java library to develop programs. You learned to obtain the current time using System.currentTimeMillis() in Example 2.5, “ Displaying Current Time. ” You used the division and remainder operators to extract current second, minute, and hour.

25 The Date Class Java provides a system-independent encapsulation of date and time in the java.util.Date class. You can use the Date class to create an instance for the current date and time and use its toString method to return the date and time as a string.

26 The Date Class Example For example, the following code java.util.Date date = new java.util.Date(); System.out.println(date.toString()); displays a string like Sun Mar 09 13:50:19 EST 2003.