Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views14 pages

EXP JAVA Packages

The document provides an overview of Java packages, explaining their purpose as collections of related classes and interfaces. It details the types of packages, including user-defined and Java API packages, and lists core packages such as java.lang, java.io, java.util, and java.net. Additionally, it includes examples of creating user-defined packages and demonstrates how to compile and run Java programs using these packages.

Uploaded by

peerlearning4
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)
3 views14 pages

EXP JAVA Packages

The document provides an overview of Java packages, explaining their purpose as collections of related classes and interfaces. It details the types of packages, including user-defined and Java API packages, and lists core packages such as java.lang, java.io, java.util, and java.net. Additionally, it includes examples of creating user-defined packages and demonstrates how to compile and run Java programs using these packages.

Uploaded by

peerlearning4
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/ 14

TY

JAVA Lab
Exp: 5
Aim : Study of JAVA packges
Introduction
✔ A package is basically collections of different classes.
✔ A package is a way to group variety of classes and/or interfaces together.
✔ The grouping is done based on related functionality.
✔ The classes contained in a package can be used in any program that import that classes.
✔ A package is nothing but a physical folder structure (directory) that contains a group of
related classes, interfaces, and sub-packages according to their functionality.

✔ There are mainly two types of packages available in Java :


1) User-defined package
2) Java API Package / Predefined Package / Built in Package
Introduction
✔ Java API Package / Predefined Package / Built in Package
Introduction
✔ Java Core Packages:

1. Java.lang: The ‘lang’ stands for language. The Java language package consists of Java
classes and interfaces that form the core of the Java language and the JVM. It is a fundamental
package that is useful for writing and executing all Java programs. Examples are classes,
objects, string, thread, predefined data types, etc. It is imported automatically into the Java
programs.
2. Java.io: The ‘io’ stands for input and output. It provides a set of I/O streams that are used to
read and write data to files. A stream represents a flow of data from one place to another place.
3. Java util: The ‘util’ stands for utility. It contains a collection of useful utility classes and
related interfaces that implement data structures like LinkedList, Dictionary, HashTable, stack,
vector, calender, data utility, etc.
4. Java.net: The ‘net’ stands for network. It contains networking classes and interfaces for
networking operations. The programming related to the client-server can be done by using this
package.
Introduction
✔ Window Toolkit and Applet:

✔1. Java.awt: The ‘awt’ stands for abstract window toolkit.


✔The Abstract window toolkit package contains GUI (Graphical User Interface) elements,
such as buttons, lists, menus, and text areas. Programmers can develop programs with colorful
screens, paintings, and images, etc using this package.

✔2. Java.applet: It is used for creating applets. Applets are programs that are executed from
the server into the client machine on a network.
Introduction
Follow the following steps to see the list of predefined packages in Java.

1. Go to programs files and open them.


2. Now go to Java folder and open it. You will see two folders such as JDK and JRE.
3. Go to JDK folder, extract the src folder. After extracting it, go to Java folder.
Here, you will see 14 predefined packages folders such as applet, awt, beans, io, lang, math,
net, nio, rmi, security, sql, text, time, and util.
4. Now you open lang package and scroll down.
You can see classes like String, StringBuffer, StringBuilder, Thread, etc.
Introduction
✔ A simple example program where we will create a user-defined package.
✔ Inside a Package a Class and method (function) within it.
✔ First take name of package and then create sub directory with the same name inside bin
directory of Java.
✔ C:/program files/java/jdk 1.4.0/bin/new folder with name (example)
✔ To write program go to notepad right click run as administrator
✔ package example;
✔ public class myclass
✔{
✔Public void myfunction()
✔{
✔System.out.println(“ I created user-defined package example”);
✔}
✔}
✔To save program go to save as notepad C:/program files/java/jdk 1.4.0/bin/new folder with
name (example) and give file name myclass.java and select all files and save.
✔ To run program cmd through go to command prompt right click run as administrator
✔ c:\ WINDOWS\system32>cd\
✔C:\> cd program files
✔C:\Program files >cd java
✔C:\Program files\ java > dir
✔C:\Program files\ java >cd jdk.14.0.1
✔C:\Program files\ java\jdk.14.0.1 >cd bin
✔C:\Program files\ java\jdk.14.0.1\bin >cd example
✔C:\Program files\ java\jdk.14.0.1\bin\example >dir
✔C:\Program files\ java\jdk.14.0.1\bin\example >cd..
✔C:\Program files\ java\jdk.14.0.1\bin>javac example\myclass.java
✔C:\Program files\ java\jdk.14.0.1\bin> cd example
✔C:\Program files\ java\jdk.14.0.1\bin\example >dir
✔To use same package example in any other program
✔Wite in new notepad file
✔import example.*;
✔class demonstration
✔{
✔public static void main(String args[])
✔{
✔myclass aa=new myclass();
✔aa.myfunction();
✔}
✔}
✔To save pgm go to
✔C:\Program files\java\ jdk.14.0.1\bin\file name demonstration\all files\save
✔To compile
✔C:\Program files\ java\jdk.14.0.1\bin\example >cd..
✔C:\Program files\ java\jdk.14.0.1\bin>javac demonstration.java
✔C:\Program files\ java\jdk.14.0.1\bin> java demonstration
✔I created user-defined package example
✔ Write a program to find balance of an account using Package which demonstrates package.
✔C:/program files/java/jdk 1.4.0/bin/new folder with name (bank)
✔ To write program go to notepad right click run as administrator: File 1: bank/Account.java
✔ package bank;
✔ public class Account
✔{
✔ private String accountNumber;
✔ private double balance;
✔public Account(String accountNumber, double balance) {
✔ this.accountNumber = accountNumber;
✔ this.balance = balance;
✔ }

✔ // Method to get balance


✔ public double getBalance() {
✔ return balance;
✔ }
✔ // Method to deposit money
✔ public void deposit(double amount) {
✔ if (amount > 0) {
✔ balance += amount;
✔ }
✔ }

✔ // Method to withdraw money


✔ public void withdraw(double amount) {
✔ if (amount > 0 && amount <= balance) {
✔ balance -= amount;
✔ }
✔ }
✔}
✔ File 2: app/AccountTest.java

✔package app;
✔import bank.Account; // Importing from bank package
✔public class AccountTest {
✔ public static void main(String[] args) {
✔Account myAccount = new Account("ACC12345", 5000.00); // Create an account object

✔ // Deposit and withdraw


✔ myAccount.deposit(1500);
✔ myAccount.withdraw(2000);

✔ // Display balance
✔ System.out.println("Account Balance: ₹" + myAccount.getBalance());
✔ }
✔}
✔ To Compile and Run

✔javac bank/Account.java app/AccountTest.java


✔java app.AccountTest

You might also like