0% found this document useful (0 votes)
23 views22 pages

Understanding Java Packages Explained

bfd
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)
23 views22 pages

Understanding Java Packages Explained

bfd
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/ 22

COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE

COURSE NAME: Object Oriented Programming

COURSE CODE: CoSc2051

INDIVIDUAL ASSIGNMENT
NAME: NARDOS SHAMEBO
ID. NO: 3588

TOGETHER WE CAN!

1
Q.1 WHAT IS JAVA PACKAGES?
 A package in Java is used to group related classes.
Think of it as a folder in a file directory. We use
packages to avoid name conflicts, and to write a
better maintainable code. Packages are divided into
two categories: Built-in Packages (packages from the
Java API)

 A package is simply a container that groups related types


(Java classes, interfaces, enumerations, and annotations).
For example, in core Java, the ResultSet interface
belongs to the java. sql package. The package contains all
the related types that are needed for the SQL query and
database connection.
 A package in Java is used to group related classes. Think of it as a
folder in a file directory. We use packages to avoid name
conflicts, and to write a better maintainable code. Packages are
divided into two categories:

o Built-in Packages (packages from the Java API)


o User-defined Packages (create your own package

Syntax:
import package.name.Class; // Import a single class

import package.name.*; // Import the whole package

2
EXAMPLES:

import java.util.Scanner;

class MyClass {

public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);

System.out.println("Enter username");

String userName = myObj.nextLine();

System.out.println("Username is: " + userName);

n Java, a package is a way to organize related classes and


interfaces into a single unit. It provides a namespace for the
classes and interfaces, which helps to avoid naming conflicts
with other classes and interfaces. Packages also provide a way
to control access to the classes and interfaces, by using access
modifiers such as public, private, and protected.

Here is an example of a Java package:

package com.example.myapp;

public class MyClass {


// class code here
}
3
In this example, the package name is com.example.myapp, and
the class name is MyClass. The package name is typically
written in reverse domain name notation, to ensure that it is
unique and does not conflict with other package names.

Here is another example of a Java package, which contains


multiple classes:

package com.example.myapp;

public class MyClass1 {


// class code here
}

public class MyClass2 {


// class code here
}

public interface MyInterface {


// interface code here
}

4
Q2. HOW TO CREATE JAVA PACKAGES?

Creating a package is a simple task as follows

 Choose the name of the package


 Include the package command as the first line of code in your
Java Source File.
 The Source file contains the classes, interfaces, etc you want
to include in the package
 Compile to create the Java packages

Step 1) Consider the following package program in Java:

package p1;

class c1(){
public void m1(){
System.out.println("m1 of c1");
}
public static void main(string args[]){
c1 obj = new c1();
obj.m1();
}
}
To create a Java package, follow these steps:

1. Create a new folder with the name of your package. For


example, if you want to create a package named
"com.example", create a folder named "com" and then create a
subfolder named "example" inside it.

2. Create your Java classes inside the subfolder you just


created. Make sure to include the package declaration at the
beginning of each class file, like this:

5
```
package com.example;

public class MyClass {


// class code goes here
}
```

3. Compile your Java classes using the javac command. If you're


using an IDE like Eclipse or IntelliJ IDEA, this step may be done
automatically for you.

4. Once your classes are compiled, you can use them in other
Java programs by importing them with the import statement,
like this:

mport com.example.MyClass;

public class MyProgram {


public static void main(String[] args) {
MyClass myObject = new MyClass();
// use myObject here
}
}
```

That's it! You've created and used a Java package.

6
Q3. HOW JAVA STORE PACKAGE?
In Java, a package is a way to organize related classes and
interfaces. A package is a collection of related classes and interfaces
that provide a common namespace for the types it contains.

To create a package in Java, you need to follow these steps:

1. Create a new directory with the name of your package. For


example, if you want to create a package named
"com.example.mypackage", you would create a directory named
"com" in your project directory, and then create a subdirectory
named "example" inside the "com" directory, and then create a
subdirectory named "mypackage" inside the "example" directory.

2. Create your Java classes inside the package directory. For example, if
you want to create a class named "MyClass" in the "com.example.mypackage"
package, you would create a file named "MyClass.java" inside the "mypackage"
directory.

3. Add the package declaration at the beginning of each Java file. The
package declaration should match the directory structure of your package. For
example, the package declaration for the "MyClass" class would be:

7
package com.example.mypackage;

public class MyClass {

// class code here

4. Compile your Java classes using the "javac" command.


You need to specify the directory containing your package
using the "-d" option. For example, if your package directory is
located at "/path/to/project/src/com/example/mypackage", you
would compile your classes like this:

javac -d /path/to/project/src
/path/to/project/src/com/example/mypackage/MyClass.java

5. Use your package in other Java classes by importing it. To import a


package, you need to use the "import" statement at the beginning of your Java
file. For example, if you want to use the "MyClass" class in another Java file,
you would import it like this:

import com.example.mypackage.MyClass;

public class AnotherClass {


MyClass myObject = new MyClass();

// rest of the class code here}

8
Q4. WRITE IN DETAIL TYPES OF PACKAGE?

In Java, there are two types of


packages: built-in packages and
user-defined packages.
The package keyword is used in
Java to create Java packages.
Many in-built packages are
available in Java, including util,
lang, awt, javax, swing, net, io, sql,
etc.
We can import all members of a
package using packagename.

9
There are two types of package:

A. Built-in Packages
These packages consist of a large number of
classes which are a part of Java API.Some of
the commonly used built-in packages are:
1) java.lang: Contains language support
classes(e.g classed which defines
primitive data types, math operations).
This package is automatically imported.
2) java.io: Contains classed for
supporting input / output operations.
3) java.util: Contains utility classes
which implement data structures like
10
Linked List, Dictionary and support ; for Date /
Time operations.
4) java.applet: Contains classes for creating
Applets.
5) java.awt: Contain classes for implementing the
components for graphical user interfaces (like
button , ;menus etc).
6) java.net: Contain classes for supporting
networking operations.

Let’s see detail about all of above:

1. java.lang: This package contains fundamental classes


and interfaces that are used by the Java runtime system.
It includes classes such as Object, String, Math, and
System, which are used in almost every Java program.

Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. java.util:
This package contains classes and interfaces for working
with collections, dates, times, and other utility functions. It includes

11
classes such as ArrayList, HashMap, Date, and Calendar, which are
used in many Java programs.

Example:
import java.util.ArrayList;

public class MyArrayList {

public static void main(String[] args) {

ArrayList<String> list = new ArrayList<String>();

list.add("apple");

list.add("banana");

list.add("orange");

System.out.println(list);

3.java.io: This package contains classes and interfaces for


working with input and output streams, files, and directories. It
includes classes such as FileInputStream, FileOutputStream,
FileReader, and FileWriter, which are used for reading and
writing data to and from files.

Example:

import java.io.*;

public class MyFileReader {

public static void main(String[] args) {

12
try {

FileReader reader = new FileReader("myfile.txt");

int character;

while ((character = reader.read()) != -1) {

System.out.print((char) character);

reader.close();

} catch (IOException e) {

e.printStackTrace();

4. java.net: This package contains classes and interfaces for working with network
connections, URLs, and sockets. It includes classes such as URL,
HttpURLConnection, and Socket, which are used for connecting to web servers
and other network resources.

Example:
import java.net.*;

public class MyURLConnection {

public static void main(String[] args) {

try {

URL url = new URL("https://www.google.com");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

int responseCode = connection.getResponseCode();

13
System.out.println("Response code: " + responseCode);

} catch (IOException e) {

e.printStackTrace();

B. User Defined Package:

In Java, a user-defined package is a collection of


related classes and interfaces that are grouped
together for organizational purposes. A package is a
way to organize Java classes into namespaces, which
helps to avoid naming conflicts and makes it easier to
manage large projects. User-defined packages can be
created by developers to organize their own classes
and interfaces.

To create a user-defined package in Java, follow


these steps:

1. Choose a name for your package. The name should


be unique and should not conflict with any existing Java
packages.

14
2. Create a directory with the same name as your
package. This directory should be located in the root
directory of your project.
3. Create your Java classes and interfaces in this
directory. Each class or interface should be saved in a
separate file with the same name as the class or
interface.
4. Add a package statement at the beginning of each
file to specify the name of the package. For example, if
your package is named "com.example.myapp", the
package statement should be:

package com.example.myapp;
5. Compile your Java classes using the javac
command. For example, if your class is named
"MyClass.java", you can compile it using the following
command:

javac MyClass.java
6. Once your classes are compiled, you can use them
in other Java programs by importing the package. For
example, if you want to use the MyClass class in
another program, you can import it using the following
statement:

15
NOW, WE HAVE TO SEE SOME EXAPLE:

import com.example.myapp.MyClass;

Here's an example of a user-defined package in Java:

package com.example.myapp;

public class MyClass {

public void sayHello() {

System.out.println("Hello, world!");

In this example, we have created a package named "com.example.myapp" and a


class named "MyClass" that belongs to this package. The sayHello() method
simply prints "Hello, world!" to the console.

To use this class in another program, we can create a new


Java file and import the package:

// File: MyProgram.java

import com.example.myapp.MyClass;

public class MyProgram {

public static void main(String[] args) {

MyClass obj = new MyClass();

obj.sayHello();

In this example, we have imported the MyClass class from the


"com.example.myapp" package and created an instance of it in the main() method. When
we run this program, it will print "Hello, world!" to the console.

16
Q5. DEFINE IMPORT A CLASS?

 To import java package into a class, we need to use java import keyword
which is used to access package and its classes into the java program. Use
import to access built-in and user-defined packages into your java source file
so that your class can refer to a class that is in another package by directly
using its name.
In Java, importing a class means making it available for use in
another class or program. When you import a class, you are telling
the Java compiler where to find the class and how to use it.

To import a class in Java, you use the import statement followed by


the fully qualified name of the class. The fully qualified name
includes the package name and the class name, separated by a dot.
For example, to import the ArrayList class from the java.util package,
you would use the following import statement:

import java.util.ArrayList;

Once you have imported a class, you can use it in your program
without having to specify the package name every time. For
example, if you want to create an ArrayList object, you can simply
write:

ArrayList<String> list = new ArrayList<String>();

Instead of:

java.util.ArrayList<String> list = new java.util.ArrayList<String>();


17
Importing classes can save you time and make your code more
readable. However, it's important to be careful when importing
classes to avoid naming conflicts. If two classes have the same name,
you can use the fully qualified name to specify which class you want
to use. For example, if you have two classes named MyClass in
different packages, you can import them like this:

import com.example.MyClass;

import com.anotherexample.MyClass;

In this example, we have imported two classes with the same name
from different packages. By using the fully qualified name, we can
avoid naming conflicts and use both classes in our program.

In conclusion, importing a class in Java is a way to make it available


for use in another class or program. By using the import statement,
you can save time and make your code more readable. However, it's
important to be careful when importing classes to avoid naming
conflicts.

18
Let's say we have two Python modules, main.py and my_module.py,
and my_module.py contains a class called MyClass. We want to use
the MyClass class in our main.py module.

First, we need to import the MyClass class from my_module.py using


the import statement at the top of our main.py module:

from my_module import MyClass

Next, we can create an instance of the MyClass class and call its
methods in our main.py module:

from my_module import MyClass

obj = MyClass()

obj.say_hello()

In this example, we have imported the MyClass class from


my_module.py using the import statement. We then create an
instance of the MyClass class and call its say_hello() method.

19
Q6. DEFINE JAVA USER INPUT(SCANNER)?

In Java, Scanner is a class that is used to read input from various


sources, such as the console, files, or network connections. It is part
of the java.util package and provides methods to read different types
of data, such as integers, floating-point numbers, and strings.

To use the Scanner class, you first need to create an instance of it.
Here's an example of how to create a Scanner object to read input
from the console:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your name: ");

String name = scanner.nextLine();

System.out.println("Hello, " + name + "!");

20
In this example, we import the Scanner class from the java.util
package. We then create a Scanner object called scanner that reads
input from the console (System.in). We prompt the user to enter
their name using System.out.print(), and then read their input using
the nextLine() method of the Scanner class. Finally, we print a
greeting message using the user's name.

The Scanner class provides many other methods for reading different
types of data, such as nextInt() for reading integers, nextDouble() for
reading floating-point numbers, and nextBoolean() for reading
boolean values.

21
REFERENCES
1. Chatbot
2. Google
3. W3shool
4. Javatpoint
5. Intro. to OOP’s module of ADMAS UNIVERISTY

THE END!!!!!!!

22

You might also like