0% found this document useful (0 votes)
484 views4 pages

Practical No. 20

The document provides examples of creating and using packages and classes in Java. It defines a package "myPackage" with a class "MyClass" containing a method "sayHello()". Another class "Hello" in a separate file imports "myPackage" and calls its method. Similarly, it defines packages "myInstitute" with class "Department" and "let_me_calculate" with class "Calculator", and imports them into other classes to call their methods. This demonstrates how to properly structure packages and classes in Java and import/use classes defined in other 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)
484 views4 pages

Practical No. 20

The document provides examples of creating and using packages and classes in Java. It defines a package "myPackage" with a class "MyClass" containing a method "sayHello()". Another class "Hello" in a separate file imports "myPackage" and calls its method. Similarly, it defines packages "myInstitute" with class "Department" and "let_me_calculate" with class "Calculator", and imports them into other classes to call their methods. This demonstrates how to properly structure packages and classes in Java and import/use classes defined in other 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/ 4

Practical No.

20
Program Code: Write a program to implement user defined packages in terms of
creating a new package and importing the same.
1)
package myPackage;
public class MyClass {
public static void sayHello() {
System.out.println("Hello & Welcome to My Class!");
}}
2)
import myPackage.MyClass;
public class Hello {
public static void main (String [] args) {
MyClass.sayHello();
}}

Exercise:
1. The code uses the class defined below. Class Importclass is not defined in
circle folder. Will the code run without giving any errors?
import circle.NewCircle;

class ImportClass{

public static void main (String args []) {

circle.NewCircle nc = new circle.NewCircle();

System.out.println(“Hello Java”);

}}
Yes, the code should run without any errors as long as NewCircle class is defined correctly in
the circle package and is accessible to the main method of the ImportClass class.

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.
1)
package myInstitute;

import java.util.*;

public class Department{

Scanner s = new Scanner(System.in);

private String staffName, dptName;

private int staffid;

public void display(){

System.out.println("Enter your Name:");

staffName = s.nextLine();

System.out.println("Enter your Staff Id:");

staffid = s.nextInt();

s.nextLine(); // consume the leftover newline character

System.out.println("Enter your department:");

dptName = s.nextLine();

public String getStaffName() {

return staffName;

public int getStaffId() {

return staffid;

public String getDptName() {

return dptName;

}}

2)
import myInstitute.*;

class Main{

public static void main(String args[]){

Department obj = new Department();

obj.display();
System.out.println("Name is :"+ obj.getStaffName());

System.out.println("Id is :"+ obj.getStaffId());

System.out.println("Department Name is :"+ obj.getDptName());

}}

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.
1)
package let_me_calculate;
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
}
2)
import let_me_calculate.Calculator;
public class Demo {
public static void main(String[] args) {
int a = 10;
int b = 20;
int result = Calculator.add(a, b);
System.out.println("The sum of " + a + " and " + b + " is " + result);
}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy