You are on page 1of 37

Programming Language (JAVA)

Unit 6.9 In-built Packages

Presentation 2

Objectives

At the end of this presentation, you will be able to Use the in-built packages in Java programs Create user-defined packages

java.lang package
The java.lang package is one of the most important packages in Java.
The java.lang package is automatically imported. It contains classes and interfaces that are fundamental to virtually all java programming. There are many classes in java.lang package.

Classes in java.lang Package


Class Name : Math Description Methods

It contains many static double sqrt(double a) mathematical static double pow(double a, double functions b) static int min(int a, int b)
static int max(int a, int b) static double exp(double a)

Classes in java.lang Package


Class Name : String Description Methods

It represents constants strings

int length()
char chatAt(int index) String concat(String str) String toLowerCase() String toUpperCase()

Classes in java.lang Package


Class Name : StringBuffer Description Methods

int length() It is used to represent a variable strings. int capacity() It is useful when the string changes in value or in length.

Classes in java.lang Package


Class Name : Object Description Methods

Inherits the variables and methods of object

Object clone()
Boolean equals(Object obj)

Wrapper classes
Wrapper classes are applied for the following data types:

Boolean

Character
Integer Long Float Double

Classes in java.lang Package


Class Name : Boolean Description Methods

Wraps the

getBoolean()

boolean
fundamental data type.

Classes in java.lang Package


Class Name : Character Description Methods

Wraps the

static boolean isLowerCase(char ch)


static boolean isUpperCase(char ch) static boolean isDigit(char ch)

char
fundamental data type. It provides some useful methods for manipulating characters.

static boolean isSpace(char ch)


static char toLowerCase(char ch) static char toUpperCase(char ch)

Classes in java.lang Package


Class Name : Integer Description Methods

The integer class wraps the fundamental integer types int.

static int parseInt(String s, int radix)


static int parseInt(String s) static Integer getInteger(String name)

Classes in java.lang Package


Class Name : Long Description Methods

It wraps the Similar to those of the Integer fundamental class. integer types long. It implements methods similar to those of the Integer class.

Classes in java.lang Package


Class Name : Float Description Methods

The Float class wraps the fundamental floating-point types float.

static int floatToIntBits(float value)


static float intBitsToFloat(int bits)

Classes in java.lang Package


Class Name : Double Description Methods

The Double class wraps the fundamental floating-point types double.

boolean isNaN()
static boolean isNaN(float v) boolean isInfinite()

Hands-On!
The program p8_demo.java checks the minimum value of the two numbers and returns the minimum among them.

Activity 6.9.2
Step 1: Open p8_demo.java from Student data file.
Step 2: Edit the Code Line 8 as a=300 Step 3: Save the program. Step 4: Compile the program.

Step 5: Run and observe the output.

Activity 6.9.3
Step 1: Open p9_demo.java from Student data file.
Step 2: Edit the Code Line 10 as

System.out.println(Math.max (a,b));
Step 3: Save the program. Step 4: Compile the program. Step 5: Run and observe the output.

Activity 6.9.4
Step 1: Open p11_demo.java from Student data file.
Step 2: Type int ln in Code Line 7; Step 3: Type ln=s1.length(); in Code Line 10. Step 4: Edit the Code Line 11 as System.out.

println(The length of the string is + ln);.

Step 5: Save the program. Step 6: Compile the program. Step 7: Run and observe the output.

Hands-On!
The program p5_demo.java checks the case of the character and returns its Boolean value

Activity 6.9.5
Step 1: Open p6_demo.java from Student data file.
Step 2: Edit the code line 8 as System.out.

println(Character. isLowerCase(ch));
Step 3: Save the program. Step 4: Compile the program. Step 5: Run and observe the output.

Activity 6.9.6
Step 1: Open p7_demo.java from Student data file.
Step 2: Edit the code line 8 as System.out.

println(Character .isDigit(ch));
Step 3: Save the program. Step 4: Compile the program. Step 5: Run and observe the output.

Lab Exercise
1. Write a program to check whether the character is in lowercase or uppercase.

User-Defined Packages
Users can create classes and group them into packages as per their requirement.
In the in-built package, only the classes available in it can be used. In the case of user-defined packages, classes can be created and grouped into packages accordingly.

Package Structure
Student
personal

Employee
personal

salary

marks

Naming Convention
Selecting the package name depends upon how the classes are used.
By naming convention, a package name begins with lowercase letter and a class name begins with uppercase letter. For example java.lang.Math, lang is the package name and Math is the class name. The naming convention helps to distinguish the package name and the class name easily.

Steps in creating packages


1. Declare the package in the first line of the source file. Package name must be in lowercase letter.
2. Declare the class as public, so that it can be used in other packages. 3. Define the methods of the class.

4. Create a subdirectory under the current directory.

Steps in creating packages


5. Name the subdirectory same as the package name.
6. Store the source file in the subdirectory.

7. Compile the file. This file creates .class file in the subdirectory.

Package Declaration : Example


package student;
public class Personal { .. .. }

Package Declaration : Syntax


package packagename;
public class classname { }

Importing User-Defined Packages


import student.*; class Student_details { .. .. ..
}

Hands-On!
Program Class1.java shows the declaration of the package and definition of the class in the package.
Step 1: Create a subdirectory pack1 in the current directory. Step 2: Save the program as Class1.java in the pack1 subdirectory. For example if d:\ is the current directory, then create d:\pack1and save Class1.java in it. Step 3: Compile the program and save it the pack1 subdirectory.

Hands-On!
Program Packtest.java imports the class Class1 from the package pack1.
Step 1: Save the program as packtest.java in the current directory. Step 2: If d: is the directory in which the packtest.java is present and the user-defined package is in d:\pack1, then set the classpath as set CLASSPATH=%CLASSPATH%;d:\pack1; in

the current directory. Type the above command in the command prompt and execute it to set the path.
Step 3: Compile and run the program.

Activity 6.9.7
Step 1: Open Class1.java from Student data file.
Step 2: Type package.pack1 in the Code line 1. Step 3: Save and compile the program.

Activity 6.9.8
Step 1: Open Packtest1.java from the student data file.
Step 2: Type import pack1.Class1; in Code line 1.

Step 3: Type c1.mul(); in Code line 7.


Step 4: Save the program.

Step 5: Compile the program.


Step 6: Run and observe the output.

Summary
In this presentation, you learnt the following:
The java.lang package is one of the most important packages. It provides number of classes and interfaces. Some of the important classes in the java.lang package are Boolean, Byte, Character, Class, Double, Float, Integer, Long, Math, Object, Short, String and StringBuffer.

Lab Exercise
1. Write a program to create a package. The package should contain a class to calculate the product of two float numbers.
2. Write a program to import the class from the package that you created.

Assignment

1.

Perform the following to create and import a userdefined package.


a) Write a program to create a package packcube. The package should contain a class sample that has a method(cube( )) definition to calculate the cube of a number b) Write another program to import the class sample from the packcube package and get an input value from the command line and pass it to the cube method by creating an object for the sample class.

You might also like