using methods in the java library. previously discussed method = a collection of statements that...

19
Using methods in the Java library

Upload: erick-thomas

Post on 14-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Using methods in the Java library

Page 2: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Previously discussed

• Method = a collection of statements that performs a complex (useful) task

A method is identified by a method name

• Class = a container for methods

Methods that serves a similar purpose are stored in the same class

A class is identified by a class name

Page 3: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Previously discussed (cont.)

• Schematically:

Page 4: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Organization of the Java library

• The Standard Java library consists of a number of packages

Each package consists of a number of classes (that provide similar functionality)

• The standard Java library is named java

• A package named xxx inside the standard Java library is named java.xxx

Page 5: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Organization of the Java library (cont.)

• Some commonly used packages:

• java.lang: Provides classes that are fundamental to the design of the Java programming language. Official website: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/package-summary.html

• java.lang: Contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array). Official website: http://download.oracle.com/javase/1.4.2/docs/api/java/util/package-summary.html

Page 6: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Organization of the Java library (cont.)

• Schematical representation of Java's Standard library:

Page 7: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Organization of the Java library (cont.)

• A class named yyy inside the package java.xxx is named java.xxx.yyy

Example:

• The class Math inside the package java.lang is known as java.lang.Math

• The class Double inside the package java.lang is known as java.lang.Double

• The class Stack inside the package java.util is known as java.util.Stack

• The class Scanner inside the package java.util is known as java.util.Scanner

Page 8: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Organization of the Java library (cont.)

• Note:

• It is a Java convention that the name of a Java class begins with a upper case letter

• It is also a Java convention that the name of a method begins with a lower case letter

Page 9: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Using a method that is available in Java's standard library: importing a class

• Rule of usage:

The import clauses must occur before any class definitions

• Syntax to import a class from the Java library:

• If a Java program wants to use a method in the Java library, the Java program must first import the containing class

import className ;

Page 10: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Using a method that is available in Java's standard library: importing a class (cont.)

• Examples:

import java.lang.Math;

import java.lang.Double;

import java.util.Stack;

import java.util.Scanner;

// After the import clauses, you can write the class definition

// This program can now use all methods defined inside the classes Math, Double, Stack

// and Scanner public

Page 11: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Using a method that is available in Java's standard library: importing a class (cont.)

class MyProgram

{

public static void main(String[] args)

{

double a;

a = Math.sqrt(2.0); // Save computed value in variable

System.out.println(a); // You can print the saved value later

}

}

Page 12: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Importing all classes in a package

• Some complex Java program may use many different methods contained in many different classes in the same package

It would be a pain to write a long list of import clauses

Example:

import java.lang.Math;

import java.lang.Double;

import java.lang.Integer; ...

Page 13: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Importing all classes in a package (cont.)

• There is a short hand to import all classes contained in a package:

import java.lang.* ; // import all class in java.lang package

import java.util.* ; // import all class in java.util package

Page 14: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Frequently used methods: java.lang

• According to the Rule of usage:

We must import java.lang.Math if we want to use the method Math.sqrt()

• If a Java program wants to use a method in the Java library, the Java program must first import the containing class

Page 15: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Frequently used methods: java.lang (cont.)

We should have written:

import java.lang.Math; // We MUST import this class to use Math.sqrt

public class Abc

{

double a, b, c, x1, x2; // Define 5 variable

a = 1.0;

b = 0.0;

c = -4.0;

Page 16: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Frequently used methods: java.lang (cont.)

x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);

x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);

System.out.print("a = ");

System.out.println(a);

System.out.print("b = ");

System.out.println(b);

System.out.print("c = ");

Page 17: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Frequently used methods: java.lang (cont.)

System.out.println(c);

System.out.print("x1 = ");

System.out.println(x1);

System.out.print("x2 = ");

System.out.println(x2); }

Page 18: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Frequently used methods: java.lang (cont.)M

• But.... because:

All classes in the java.lang package are automatically included in every Java program (the Java compiler is programmed to do this)

That is why we did not need to import java.lang.Math in our program.

• The package java.lang contains classes that are fundamental to the design of the Java programming language.

Page 19: Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified

Summary: using methods in the Java library

• Rule of usage:

• If a Java program wants to use a method in the Java library, the Java program must first import the containing class

• All classes in the java.lang package have already been imported into a Java program (You can use methods in these classes without the import clause)