Posts

Showing posts with the label Packages

Program to import 'UseMeToo' package and use display( ) method

In this program we use the package  useFul.useFullToo  to import the class  UseMeToo  in another class  SubPackageUse . Here we have used the  display( )  method of UseMe class. PROGRAM import useFul.useFullToo.UseMeToo; class SubPackageUse { public static void main(String args[]) { UseMeToo umt = new UseMeToo(); umt.display(); } } OUTPUT C:\>javac SubPackageUse.java C:\>java SubPackageUse This is from Inside Package.

Program to create a package named 'useFullToo' inside the above package 'useFul' with one class named 'useMeToo' having a method to display the message "This is from Inside Package"

Packages can contain sub packages (one package in another package). In simple words you can create a hierarchy of packages using dot (.) operator as given below: package p1.p2.p3; In above example the package p1 is main package, p2 is sub-package of p1 and p3 is sub-package of p2. In this program, we have created a package  useFullToo  inside the existing package named  useFul  and put the  UseMeToo  class in that package. Compile this program using java compiler then you will get a .class file in given package. PROGRAM package useFul.useFullToo; public class UseMeToo { public void display() { System.out.println("This is from Inside Package."); } } OUTPUT C:\>javac -d . UseMeToo.java

Program to define class 'Student' to calculate the percentage. Import the above package 'useFul' and make use of the method percentage( )

In this program we use the package  useFul  to import the class  UseMe  in another class  Student . Here we have used the  percentage( )  method of UseMe class to calculate the percentage of student. PROGRAM import useFul.UseMe; class Student { public static void main(String b[]) { UseMe u3 = new UseMe(); u3.percentage(600,450); } } OUTPUT C:\>javac Student.java C:\>java Student Total marks = 600 Obtained marks = 450 Percentage = 75.0

Program to create a class 'Manager' to calculate the salary. Import the above package 'useFul' and make use of salary( ) method

In this program we use the package  useFul  to import the class  UseMe  in another class  Manager . Here we have used the  salary( )  method of UseMe class to calculate the gross salary of manager. PROGRAM import useFul.UseMe; class Manager { public static void main(String b[]) { UseMe u2 = new UseMe(); u2.salary(15000,2500,1500); } } OUTPUT C:\>javac Manager.java C:\>java Manager Basic salary = 15000.0 DA = 2500.0 HRA = 1500.0 Gross Salary = 19000.0

Program to create a class 'PackageUse' to import the above package 'useFul' and use the method area( )

In previous program, we have crated a package named  useFul  and added class  UseMe  in that package. Now in this program we use that package to import the class "UseMe" in class "PackageUse". Here we have used the  area( )  method of UseMe class. PROGRAM import useFul.UseMe; class PackageUse { public static void main(String b[]) { UseMe um = new UseMe(); um.area(12,7); } } OUTPUT C:\>javac PackageUse.java C:\>java PackageUse Length of Rectangle = 12.0 Breadth of Rectangle = 7.0 Area of Rectangle = 84.0

Program to define a package named 'useFul' with a class named 'UseMe'

In this program, we have created a package  useFul  and put the  UseMe  class in that package. Compile this program using java compiler then you will get a .class file in given package. In this program, we have created a package  useFul  and put the  UseMe  class in that package. Compile this program using java compiler then you will get a .class file in given package. PROGRAM package useFul; public class UseMe { public void area(double length, double breadth) { double area = length * breadth; System.out.println("Length of Rectangle = "+length); System.out.println("Breadth of Rectangle = "+breadth); System.out.println("Area of Rectangle = "+area); } public void salary(double basic_sal, double da, double hra) { double gross_sal = basic_sal + da + hra; System.out.println("Basic salary = "+basic_sal); System.out.println("DA = "+da); System.out.println("HRA = "+hra); System.out.println("Gross Salary...

Program to import package 'myPackage' and use the 'DisplayMsg' class in package

In previous program, we have crated a package named "myPackage" and added class "DisplayMsg" in that package. Now in this program we use that package to import the class "DisplayMsg" in class "PackageDemo". PROGRAM import myPackage.DisplayMsg; class PackageDemo { public static void main(String[] args) { DisplayMsg dm = new DisplayMsg(); dm.show(); } } OUTPUT C:\>javac PackageDemo.java C:\>java PackageDemo This is from package.

Program to create package named "myPackage" and put "DisplayMsg" class in it

In this program, we have created a package  myPackage  and put the  DisplayMsg  class in that package. Compile this program using java compiler then you will get a .class file in given package. PROGRAM package myPackage; public class DisplayMsg { public void show() { System.out.println("This is from package."); } } HOW TO COMPILE ? To compile the above package program use the following command. javac -d . DisplayMsg.java After successful compilation the  myPackage  directory will be created and the .class file is placed in that directory. The output of this program is the .class file.

Program to import the class A from package p1

In previous program, we have crated a package named "p1" and added class "A" in that package. Now in this program we use that package "p1" to import the class "A" in class "PackageExample". import  keyword is used to import built-in and user-defined packages into your java source file. There are 3 different ways to refer to class that is present in different package Using fully qualified name class MyDate extends java.util.Date{ // statement; } import the only class you want to use. import java.util.Date; class MyDate extends Date{ // statement; } import all the classes from the particular package import java.util.*; class MyDate extends Date{ // statement; } Remember that, if there are both package and import statement in your source file then the import statement is after package statement. In this program, we have imported the class  A  from package  p1  usnig the import statement as below: import p1.A; ...

Program to create user-defined package named "p1" and put class "A" in it

A  package  can be defined as a group of similar types of classes, interface, enumeration and sub-package. Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc. Using package it becomes easier to locate the related classes. Package in java can be categorized in two form, Built-in package :  Existing Java package for example  java.lang, java.util, java.awt,  etc User-defined package :  Java package created by user to categorized classes and interfaces. . Creating a package in java is quite easy. Simply include a package command followed by name of the package as the first statement in java source file. package mypackage; public class MyClass { ...statements; } The above statement create a package called  mypackage . Java uses file system directory to store package. For example the .class for any classes you define to be part of  mypackage  package must b...