[go: up one dir, main page]

0% found this document useful (0 votes)
4 views3 pages

JPR Manual Answers Experiment 10

The document outlines three programming experiments involving the creation and usage of user-defined packages in Java. Each experiment includes source code for a package and a main class that imports the package to demonstrate functionality, such as displaying messages and performing calculations. The examples illustrate how to define classes within packages and how to call their methods from other classes.

Uploaded by

Riya Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

JPR Manual Answers Experiment 10

The document outlines three programming experiments involving the creation and usage of user-defined packages in Java. Each experiment includes source code for a package and a main class that imports the package to demonstrate functionality, such as displaying messages and performing calculations. The examples illustrate how to define classes within packages and how to call their methods from other classes.

Uploaded by

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

Experiment No.

10

Program Statement 1: WAP to implement user defined packages in terms of creating a


new package and importing the same.

Source Code:
For Packm(class in the package file)

package MyPack;

public class Packm


{
public void display()
{
System.out.println(“Hello! It’s Riya…”);
}
}

For Mainpk(class that consists of the main method)

import MyPack.Packm;

class Mainpk
{
public static void main(String[] args)
{
Packm p=new Packm();
p.display();
}
}

Output:
Program Statement 2: Define a package named myInstitute include class named as
department with one method to display the staff of that department. Develop a program to
import this package in a java application and call the method defined in the package.

Source Code:
For department(class in the package file)
package myInstitute;

public class Department


{
public void show(int empid,String name)
{
System.out.println("Staff employee details:-\nEmployee
ID:"+empid+"\nEmployee Name:"+name);
}
}

For Mainpk(class that consists of the main method)

import myInstitute.Department;

public class StaffD


{
public static void main(String args[])
{
int empid=30;
String name="Riya Patil";
Department d=new Department();
d.show(30,"Riya Patil");
}
}

Output:
Program Statement 3:Develop a program which consists of the package named
let_me_calculate with a class named Calculator and a method named add to add two integer
numbers. Import let_me_calculate package in another program(class named Demo) to add
two numbers.

Source Code:
For Calculator(class in the package file)
package let_me_calculate;

public class Calculator


{
public int add(int a,int b)
{
return a+b;
}
}

For Demo(class that consists of the main method)

import let_me_calculate.Calculator;

public class Demo


{
public static void main(String[] args)
{
Calculator c=new Calculator();
int sum=c.add(20,10);
System.out.println("Sum:"+sum);
}
}

Output:

You might also like