java library n.
Skip this Video
Loading SlideShow in 5 Seconds..
Java Library PowerPoint Presentation
Download Presentation
Java Library

Loading in 2 Seconds...

play fullscreen
1 / 26

Java Library - PowerPoint PPT Presentation

  • Uploaded on

Java Library. Lecture 9 by Dr. Norazah Yusof. 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:

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Java Library' - kolton

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
java library

Java Library

Lecture 9


Dr. Norazah Yusof

java library1
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.langcontains classes such as, Object, String, and System.
    • java.awtprovides classes such as, Button, TextField, and Graphics.
java library system and printstream
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.
the import statement
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.

Allows a specific class to be known by its abbreviated


import package.class

Allows all the classes in the specified package to be known by their short names.

import package.*

the scanner class
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
the scanner class cont
The Scanner Class (Cont)
  • The Scanner class is defined in java.util, so we import java.util.Scanner;
  • Scanner objects work with
  • To create a Scanner object,

Scanner input1 = new Scanner (

  • Scanner class methods are listed in page 78 in the text.
java library java lang math the math class
Java Library: java.lang.Math (The Math Class)
  • The java.lang.Math class provides common mathematical functions.

The Math class is a static class and can not be subclassed or instantiated.

publicfinalclass Math {// final class cannot be subclassed

private Math() {} // private constructor cannot be invoked


publicstaticnativedouble sqrt (double a)

throws ArithmeticException;


All Math class methods are static class methods. They are invoked as follows:


the math class
The Math Class
  • Class constants:
    • PI
    • E
  • (A class constantis 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).
trigonometric methods
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)
trigonometric methods1
Trigonometric Methods
  • sin(double a)
  • cos(double a)
  • tan(double a)
  • acos(double a)
  • asin(double a)
  • atan(double a)


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 0.866

Math.cos(Math.PI / 2) returns 0



exponent methods
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)
exponent methods1
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.


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 22.91765

Math.sqrt(4) returns 2.0

Math.sqrt(10.5) returns 3.24

rounding methods
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)
rounding methods1
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).

rounding methods examples
Rounding Methods Examples
  • Math.ceil(2.1)
  • Math.ceil(2.0)
  • Math.ceil(-2.0)
  • Math.ceil(-2.1)
  • Math.floor(2.1)
  • Math.floor(2.0)
  • Math.floor(-2.0)
  • Math.floor(-2.1)
  • Math.rint(2.1)
  • Math.rint(2.0)
  • Math.rint(-2.0)
  • Math.rint(-2.1)
  • Math.rint(2.5)
  • Math.rint(-2.5)
  • Math.round(2.6f)
  • Math.round(2.0)
  • Math.round(-2.0f)
  • Math.round(-2.6)
rounding methods examples1
Rounding Methods Examples
  • Math.ceil(2.1) returns 3.0
  • Math.ceil(2.0) returns 2.0
  • Math.ceil(-2.0) returns –2.0
  • Math.ceil(-2.1) returns -2.0
  • Math.floor(2.1) returns 2.0
  • Math.floor(2.0) returns 2.0
  • Math.floor(-2.0) returns –2.0
  • Math.floor(-2.1) returns -3.0
  • Math.rint(2.1) returns 2.0
  • Math.rint(2.0) returns 2.0
  • Math.rint(-2.0) returns –2.0
  • Math.rint(-2.1) returns -2.0
  • Math.rint(2.5) returns 2.0
  • Math.rint(-2.5) returns -2.0
  • Math.round(2.6f) returns 3
  • Math.round(2.0) returns 2
  • Math.round(-2.0f) returns -2
  • Math.round(-2.6) returns -3
min max and abs
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)
min max and abs1
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.


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

the random method
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).


In general,

view java lang math documentation
View java.lang.Math Documentation

You can view the complete documentation for the Math class online from

the random class
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.

the random class example
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: 734 660 210 581 128 202 549 564 459 961

From random2: 734 660 210 581 128 202 549 564 459 961

using classes from the java library
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.

the date class
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.

the date class example
The Date Class Example

For example, the following code

java.util.Date date = new java.util.Date();


displays a string likeSun Mar 09 13:50:19 EST 2003.