Professional Documents
Culture Documents
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.
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)
int length()
char chatAt(int index) String concat(String str) String toLowerCase() String toUpperCase()
int length() It is used to represent a variable strings. int capacity() It is useful when the string changes in value or in length.
Object clone()
Boolean equals(Object obj)
Wrapper classes
Wrapper classes are applied for the following data types:
Boolean
Character
Integer Long Float Double
Wraps the
getBoolean()
boolean
fundamental data type.
Wraps the
char
fundamental data type. It provides some useful methods for manipulating characters.
It wraps the Similar to those of the Integer fundamental class. integer types long. It implements methods similar to those of the Integer class.
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.
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.
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.
7. Compile the file. This file creates .class file in the subdirectory.
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.
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.