Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Programming in Java

Packages in Java

 A java package is a group of similar types of classes, interfaces and sub-packages.


 Packages in java can be categorized in two form, built-in package and user-defined
package.

A java Package can be considered as a single unit which comprises various


classes , interfaces – it also may contain another package inside called as sub
package in the beginning. If You want to use a particular package we use a
keyword import – Syntax is –
import <package name.*>; where .* means all the available components
inside that package we can use in our current program.
Programming in Java

Packages in Java

Package Names & Their Descriptions


This Package is included in every program by default by the compiler
and contains the various Wrapper Classes and other useful
java.lang functionalities and the primitive data types.
This Package is used to facilitate Input/Output activities and
java.io particularly to handle various kinds of Streams
java.awt This Package is useful to create GUI Components in our Program
java.awt.event Here it is a sub pacakge and is useful for processing events
Swing components are advanced form of AWT Components and is
javax.swing used to apply GUI Components in our Java Program
It is Particularly used when a Java Application has to be connected
java.sql with a Database Server
It contains various tools to incorporate ArrayList, LinkedList , Stacks &
java.util Queues
Programming in Java

Packages in Java
Steps For Creating an User Defined Package

1) Create A Java File where the First Line Should be -


package <packageName>;
2) Create a Class with public specifier within this java File.
3) Compile it successfully and Your Package will be created automatically.
4) To Use this newly built package create another Java source File and write import
packagename at the top.
5) Further you can use an object of the class from package and try to call any of its
methods.
Programming in Java

Access Specifiers

The access modifiers in Java specifies the accessibility or scope of a field, method,
constructor, or class. We can change the access level of fields, constructors, methods, and
class by applying the access modifier on it.
There are four types of Java access modifiers:
private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
public: The access level of a public modifier is everywhere. It can be accessed from within
the class, outside the class, within the package and outside the package.
Programming in Java

Access Specifiers

Access Modifier within class within package outside package outside package
by subclass only

private Y N N N
default Y Y N N
protected Y Y Y N
public Y Y Y Y

We can use other modifiers as well like static , abstract or final. But these are
considered as modifiers.
Programming in Java
Exception Handling in Java

Exceptions are a mechanism used to describe what to do when something unexpected


happens. For example:
When a method is invoked with unacceptable arguments
When a network connection fails
When the user asks to open a non-existent file
Assertions are a way to test certain assumptions about the logic of a program. For
example:
To test a variable for a positive value at a particular point of time.
Programming in Java
Exception Handling in Java

Exceptions are a mechanism used to describe what to do when something


unexpected happens.
You can implement exception handling in your program by using the following
keywords:
try – It acts as a safegurard it immediately passes the control to the
corresponding catch block if some exception is raised otherwise the code will
run normally.
catch – It acts as an Exception Handler- this block will only be executed only
when some Exception is Raised, otherwise Not.
throws/throw- Both are used to generate Exceptions sometimes forcefully.
finally – This block contains the code which are to be executed both in case of
normal execution as well as exceptional situations. It means at any cost the
codes written within the finally block will be executed.
Java Script
Exception Handling in Java
The Java programming language provides two broad categories of exceptions:
Checked
Unchecked
Checked exceptions are those that the programmer is expected to handle in the
program, and that arise from external conditions that can readily occur in a working
program.
Unchecked exceptions might arise from conditions that represent bugs, or situations
that are considered generally too difficult for a program to handle reasonably.
Exceptions that arise from a category of situations that probably represent bugs are
called runtime exceptions.
Java Script
Exception Handling in Java

Exceptions that arise as a result of environmental issues that are rare enough or hard
enough to recover from are called errors.
The Exception class is the base class that represents checked and unchecked
exceptions. It is already defined in java.lang package.
The Error class is the base class used for the unchecked, serious error conditions from
which your program is not expected to attempt recovery.
The RuntimeException class is the base class that is used for the unchecked exceptions
that might arise as a result of program bugs.
Java Script
Exception Handling in Java :An Example
import java.util.Scanner;
// Example of Exception Handling
public class Example1
{
private int firstno,secondno, result;
public void GetData()
{
try
{
Scanner sc=new Scanner(System.in); System.out.println("Enter Two Numbers");
firstno=sc.nextInt(); secondno=sc.nextInt();
result=firstno/(secondno-3); System.out.println("Total = "+result);
}
catch(Exception x)
{
System.out.println("Exception Occurred During Execution");
}
finally { System.out.println("End of Program"); }
}
public static void main(String[] a)
{
Example1 e1=new Example1(); e1.GetData();
}
}
Programming in Java
Exception Handling in Java
The try-catch block:
The try block governs the statements that are enclosed within it and defines the
scope of the exception-handlers associated with it.
A try block must have at least one catch block that follows it immediately.
The catch statement takes the object of the exception class that refers to the
exception caught, as a parameter.
Once the exception is caught, the statements within the catch block are executed.
The scope of the catch block is restricted to the statements in the preceding try
block only.
If an exception is not handled in the current try-catch block, it is thrown to the
caller of the method.
If the exception gets back to the main method and is not handled there, the
program is terminated abnormally
Java Script
Exception Handling in Java

The common exceptions are:


NullPointerException – occurs when an object is not initialized and we are trying
to use it in our program.
FileNotFoundException – Suppose you are trying to access a File but the File is
not present there.
NumberFormatException – When you trying to convert a value in Integer or
Decimal value but you are using an alphanumeric value.
ArithmeticException – When you are trying to divide a number by zero.

These are special Exception classes and they are all derived from Exception class
Programming in Java
Exception Handling in Java

User-defined exceptions are created by extending the Exception class.


The extended class contains constructors, data members and methods.
The throw and throws keywords are used while implementing user-defined
exceptions.

Trainer Will Give Necessary Explanation and Demo For the Above Topic.
Programming in Java
Assignment:

1. Java program to Display Fibonacci Series upto a certain number- the no. has to be
entered by the user. [ Apply try/catch/finally]
2. Java program to Find Factorial for an entered No. [ Apply try/catch/finally]
3. Java program to find largest of three numbers using ternary operator.
4. Java program to check leap year – year you have to enter using keyboard [ Apply
try/catch/finally]
5. Java Program to Reverse an input number [ Apply try/catch/finally]
6. Java Program to Calculate area of circle – Apply Method Overloading so that in one
method you have to enter the radius & value of Pi as a parameter and in the overloaded
method – only you have to enter the radius.
7. Java Program to Calculate area of triangle – apply User Defined Exception so that no –ve
value or 0 is entered as a base or height.
8. Java Program to Find sum of array elements – catch ArrayIndexOutOfBounds Exception
in this Program.
Programming With Java

End of Day 3

You might also like