0% found this document useful (0 votes)
13 views11 pages

Exercise - 6

Java lab excersize on packages
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Exercise - 6

Java lab excersize on packages
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

EXERCISE : 6

Aim:

Program to demonstrate Packages.

6(a) Program to print “hello world” using packages.

Software requirements:

Windows operating system

Java jdk 17

Notepad

Source code:

package mypack;

public class Hellopack{

public void display(){

System.out.println(“welcome to hello world”);

//in another file

package mypack;

import mypack.*;

public class Testpack{

public static void main(String[] args){

Hellopack obj = new Hellopack();

obj.display();
}

Output :

D:\046Java>javac -d . Testpack.java

D:\046Java>java mypack.Testpack

welcome to hello world

6(b) Program to find the factorial of a number using packages.

Source code :

Software requirements:

Windows operating system

Java jdk 17

Notepad

Factorial Package

package MyPack1;

public class factorial{

public void facto(){

int fact=1,i,n=4;

for(i=1;i<=n;i++){

fact=fact*i;

System.out.println("Factorial of 4 is "+fact);

}
}

package MyPack1;

import MyPack1.*;

public class Test1{

public static void main(String[] args){

factorial obj = new factorial();

obj.facto();

Output:

D:\046Java>javac -d . factorial.java

D:\046Java>javac -d . Test1.java

D:\046Java>java MyPack1.Test1

Factorial of 4 is 24

6(c) Program to create a package which has classes and methods to read
Student Admission details.

Source code:

// class for student personal details (in a package FIRST);


package first;

public class Studentinfo

int age;

char sex;

String name,add,fn,mn;

public Studentinfo(int e,char f,String a,String b,String c,String d)

age=e;

sex=f;

name=a;

add=d;

fn=b;

mn=c;

public void display()

System.out.println("STUDENT PERSONAL DETAILS");

System.out.println("Name of the student is: "+name);

System.out.println("Age="+age);

System.out.println("Student is "+sex);

System.out.println("Address of the student is: "+add);


System.out.println("Father's name: "+fn);

System.out.println("Mother'sname is: "+mn);

// class for student acedamic details(in package FIRST)

package first;

public class Acedamicinfo

int avg,att,rollno;

public Acedamicinfo(int f,int g,int h)

avg=f;

att=g;

rollno=h;

public void display2()

System.out.println("STUDENT EDUCATIONAL DETAILS");

System.out.println("Roll no of the student "+rollno);

System.out.println("Average of Marks of student= "+avg);

System.out.println("Attendance of student ="+att+"%");

}
// class for faculty details(in package second)

package second;

public class Teacherinfo

String name,des,pos;

public Teacherinfo(String n,String d,String p)

name=n;

des=d;

pos=p;

public void display()

System.out.println("STAFF DETAILS");

System.out.println("Name of the Member "+name);

System.out.println("Designation= "+des);

System.out.println("His Posistion::"+pos);

// MAIN CLASS

import first.*;

import second.*;

class PackageDemo
{

public static void main(String args[])

first.Studentinfo a=new first.Studentinfo(18,'M',"PETER","Mr.


JOHN","Mrs.MARY","VIZAG");

a.display();

first.Acedamicinfo de= new first.Acedamicinfo(499,98,1248);

de.display2();

second.Teacherinfo b=new
second.Teacherinfo("DAVID","Faculty","Professor");

b.display();

Output:

D:\>javac Acedamicinfo.java

D:\>javac Studentinfo.java

D:\>javac Teacherinfo.java

D:\>javac Package.java

D:\>java Package

STUDENT PERSONAL DETAILS

Name of the student is: Priya

Age=18

Student is M
Address of the student is: VIZAG

Father's name: Mr. sagar

Mother'sname is: Mrs. sowji

STUDENT EDUCATIONAL DETAILS

Roll no of the student 1248

Average of Marks of student= 499

Attendance of student =98%

STAFF DETAILS

Name of the Member santosh

Designation= Faculty

His Posistion::Professor

6(d)Write a Java program to perform employee payroll processing using


packages.

Source code:

//SAVE AS Emp.java

package employee;

public class Emp{

String name,empid, category;

int bpay;

double hra,da,npay,pf,grosspay,incometax,allowance;
public Emp(String n, String id, String c, int b)

name = n;

empid = id;

category = c;

bpay = b;

public void call()

da = bpay*0.05;

hra = bpay*0.09;

pf = bpay*0.11;

allowance = bpay*0.10;

grosspay = bpay+da+hra+allowance-pf;

incometax = 0.75*grosspay;

npay = grosspay- incometax;

public void display()

System.out.println("/n/n Employee Details");

System.out.println("/n/n Name:"+name);
System.out.println("/n/n Empid:"+empid);

System.out.println("/n/n Category:"+category);

System.out.println("/n/n bpay:"+bpay);

System.out.println("/n/n da:"+da);

System.out.println("/n/n hra:"+hra);

System.out.println("/n/n pf:"+pf);

System.out.println("/n/n all:"+allowance);

System.out.println("/n/n gs:"+grosspay);

System.out.println("/n/n Incometax:"+incometax);

System.out.println("/n/n npay:"+npay);

//SAVE as Emppay.java

import java.io.*;

import employee.Emp;

class Emppay

public static void main(String args[])

Emp e = new Emp("ANU","23","Female",12000);

e.call();
e.display();

/*OUTPUT:

D:\>javac -d . Emp.java

D:\>javac -d . Emppay.java

D:\>java Emppay

Employee Details

Name: ANU

Empid: 23

Category: Female

bpay: 12000

da: 600.0

hra: 1080.0

pf: 1320.0

allowance: 1200.0

grosspay: 13560.0

Incometax: 10170.0

npay: 3390 */

You might also like