0% found this document useful (0 votes)
50 views2 pages

10 Mypacakage

The document describes a Java program that creates a package named "mypack" containing a class called "MyPackageClass" with methods to display a message and add two numbers. A separate class "PackageDemo" imports and implements the "mypack" package by creating an instance of "MyPackageClass" and calling its displayMessage() and addNumbers() methods.

Uploaded by

nida61325
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)
50 views2 pages

10 Mypacakage

The document describes a Java program that creates a package named "mypack" containing a class called "MyPackageClass" with methods to display a message and add two numbers. A separate class "PackageDemo" imports and implements the "mypack" package by creating an instance of "MyPackageClass" and calling its displayMessage() and addNumbers() methods.

Uploaded by

nida61325
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/ 2

10.

Develop a JAVA program to create a package named mypack and import &
implement it in a suitable class.

A. Package mypack

// Inside a folder named 'mypack'

package mypack;

public class MyPackageClass {

public void displayMessage() {

System.out.println("Hello from MyPackageClass in mypack package!");

// New utility method

public static int addNumbers(int a, int b) {

return a + b;

B. PackageDemo class using mypack Package

// Main program outside the mypack folder


import mypack.MyPackageClass;

//import mypack.*;

public class PackageDemo {

public static void main(String[] args) {

// Creating an instance of MyPackageClass from the mypack package


MyPackageClass myPackageObject = new MyPackageClass();

// Calling the displayMessage method from MyPackageClass

myPackageObject.displayMessage();

// Using the utility method addNumbers from MyPackageClass

int result = MyPackageClass.addNumbers(5, 3);


System.out.println("Result of adding numbers: " + result);

You might also like