Packages in Java
Packages in Java
Types of packages:
Built-in Packages
These packages consist of a large number of classes
which are a part of Java API. Some of the commonly used
built-in packages are:
1) java.lang: Contains language support classes(e.g
classed which defines primitive data types, math
operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output
operations.
3) java.util: Contains utility classes which implement data
structures like Linked List, Dictionary and support ; for
Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the
components for graphical user interfaces (like button ,
;menus etc).
6) java.net: Contain classes for supporting networking
operations.
1. Package statement
2. Import statement
3. Class declaration
Sample programs
package vehicles;
interface Vehicle
{
public void run();
public void speed();
}
Car.java
package vehicles;
public class Car implements Vehicle
{
public void run()
{
System.out.println("Car is running.");
}
public void speed()
{
System.out.println("Speed of Car: 50 Km/h");
}
public static void main(String args[])
{
Car Car = new Car();
Car.run();
Car.speed();
System.out.println("Hello World!");
}
}
Output:
3. Write a Package MCA which has one class Student. Accept
student detail through parameterized constructor. Write display
() method to display details. Create a main class which will use
package and calculate total marks and percentage.
Notepad----------Student.java
/*In this example we create a package mca which is used to
display the student records. This package is used by
Studentmain class. */
package mca;
public class Student
{
public int r_no;
public String name;
public int a,b,c;
int sum=0;
public Student(int roll, String nm, int m1,int m2,int m3)
{
r_no = roll;
name = nm;
a = m1;
b = m2;
c = m3;
sum = a+b+c;
}
public void display()
{
System.out.println("Roll_no : "+r_no);
System.out.println("Name : "+name);
System.out.println("-----MARKS-------");
System.out.println("Sub 1 : "+a);
System.out.println("Sub 2 : "+b);
System.out.println("Sub 3 : "+c);
System.out.println("Total : "+sum);
System.out.println("percentage: "+sum/3);
System.out.println("------------------");
}
}
Output: