Java Lab File
Java Lab File
THEORY :-
STACK : A stack is a linear data structure that follows the LIFO (Last–In, First–Out) principle.
That means the objects can be inserted or removed only at one end of it, also called a top.
push inserts an item at the top of the stack (i.e., above its current top element).
pop removes the object at the top of the stack and returns that object from the function.
The stack size will be decremented by one.
QUEUE :A queue is a linear data structure that follows the FIFO (First–In, First–Out) principle.
That means the object inserted first will be the first one out, followed by the object inserted next.
i. STACK IMPLEMENTATION
public class S1 {
private intarr[];
privateint top;
privateint capacity;
return -1;
}
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
System.out.println("The front element is " + q.peek());
q.dequeue();
System.out.println("The front element is " + q.peek());
System.out.println("The queue size is " + q.size());
q.dequeue();
q.dequeue();
if (q.isEmpty()) {
System.out.println("The queue is empty");
}
else {
System.out.println("The queue is not empty");
}
}
}
OUTPUT :-
Dhriti 02213302720 Page 6
EXPERIMENT-2
THEORY :-
Compile-time Polymorphism
Compile-time polymorphism is also known as static polymorphism or early binding. Compile-
time polymorphism is a polymorphism that is resolved during the compilation process.
Overloading of methods is called through the reference variable of a class. Compile-time
polymorphism is achieved by method overloading and operator overloading.
1. Method overloading
We can have one or more methods with the same name that are solely distinguishable by
argument numbers, type, or order.
Method Overloading occurs when a class has many methods with the same name but different
parameters. Two or more methods may have the same name if they have other numbers of
parameters, different data types, or different numbers of parameters and different data types.
Example:
void sum() { ... }
void sum(int num1 ) { ... }
void sum(float num1) { ... }
void sum(int num1 , float num2 ) { ... }
(a). Method overloading by changing the number of parameters
In this type, Method overloading is done by overloading methods in the function call with a
varied number of parameters
Example:
show( char a )
show( char a ,char b )
(b). Method overloading by changing Datatype of parameter
In this type, Method overloading is done by overloading methods in the function call with
different types of parameters
Example:
show( float a float b)
show( int a, int b )
(c). By changing the sequence of parameters
In this type, overloading is dependent on the sequence of the parameters
Example:
show( int a, float b )
show( float a, int b )
Here in this example, The parameters int and float are used in the first declaration. The
parameters are int and float in the second declaration, but their order in the parameter list is
different.
CODE :-
{
void demo (int a) // First Method
{
System.out.println ("a: " + a);
}
class MethodOverloading
{
public static void main (String args[]) // main function
{
Overload O1 = new Overload(); double result;
O1 .demo(10);
O1 .demo(10, 20);
result = O1 .demo(5.5);
OUTPUT :-
THEORY :-
Java Applet
Applet is a special type of program that is embedded in the webpage to generate the dynamic
content. It runs inside the browser and works at client side.
Advantage of Applet
There are many advantages of applet. They are as follows:
o It works at client side so less response time.
o Secured
o It can be executed by browsers running under many plateforms, including Linux,
Windows, Mac Os etc.
Drawback of Applet
o Plugin is required at client browser to execute applet.
CODE :-
import java.applet.Applet;
import java.awt.*;
OUTPUT :-
THEORY :-
SUPER CLASS : The parent class from which many subclasses can be created. All the
subclasses have all the attributes and properties that have parent class.
INHERITANCE is the mechanism of object-oriented language by which any class can (child
class) inherit other class all the properties and behaviour of the parent class.
There are two methods to call the instance variables and methods of the superclass
(parent class) in the child class.
1) First Method: super keyword is one of the reserved words in java. Super refers to an
object of the parent class.
Uses:
We can invoke the overridden method of the parent class with the help of the super
keyword.
super() is used for executing the constructor of the parent class and should be used
in the first line in the derived class constructor.
2) Second Method: Without using the keyword super keyword after inheritance all the
methods and instance variables of the parent class is inherited by the child class. So we
can direct them in the child class.
}
public static void main(String args[])
ob.name();
class G{
G()
EXPERIMENT-5
CODE :-
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
public class Uppercase {
public static void main(String[] args) {
try {
File file = new File("e.txt");
if (!file.exists()) {
// File does not exist, print error message and return
System.err.println("Error: file " + file.getName() + " does not exist.");
return;
}
FileReader reader = new FileReader("e.txt");
String result = "";
int data;
int data2;
System.out.println("Before :");
while ((data = reader.read()) != -1) {
System.out.println((char)data);
data2 = Character.toUpperCase(data);
result += (char)data2;
}
reader.close();
System.out.println("After :");
System.out.println(result);
FileWriter writer = new FileWriter("e.txt");
writer.write(result);
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT :-
THEORY :-
EXCEPTION- In Java, an exception is an event that disrupts the normal flow of the program. It
is an object which is thrown at runtime.
Java provides five keywords that are used to handle the exception. The following table describes
each.
KEYWORD DESCRIPTION
try The "try" keyword is used to specify a block where we should place an exception code.
It means we can't use try block alone. The try block must be followed by either catch or
finally.
catch The "catch" block is used to handle the exception. It must be preceded by try block
which means we can't use catch block alone. It can be followed by finally block later.
finally The "finally" block is used to execute the necessary code of the program. It is executed
whether an exception is handled or not.
throws The "throws" keyword is used to declare exceptions. It specifies that there may occur an
exception in the method. It doesn't throw an exception. It is always used with method
signature.
CUSTOMIZED EXCEPTION - Java provides us the facility to create our own exceptions
which are basically derived classes of Exception. Creating our own Exception is known as a
custom exception or user-defined exception. Basically, Java custom exceptions are used to
customize the exception according to user needs.
CODE :-
OUTPUT :-
THEORY :-
Multithreading is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of CPU. Each part of such program is called a thread. So,
threads are light-weight processes within a process.
Threads can be created by using two mechanisms :
1. Implementing the Runnable Interface
2. Extending the Thread class
We create a new class which implements java.lang.Runnable interface and override run()
method. Then we instantiate a Thread object and call start() method on this object.
We create a class that extends the java.lang.Thread class. This class overrides the run()
method available in the Thread class. A thread begins its life inside run() method. We create an
object of our new class and call start() method to start the execution of a thread. Start() invokes
the run() method on the Thread object.
1. If we extend the Thread class, our class cannot extend any other class because Java doesn’t
support multiple inheritance. But, if we implement the Runnable interface, our class can
still extend other base classes.
2. We can achieve basic functionality of a thread by extending Thread class because it
provides some inbuilt methods like yield(), interrupt() etc. that are not available in
Runnable interface.
3. Using runnable will give you an object that can be shared amongst multiple threads.
CODE :-
b) THREAD CLASS
OUTPUT :-
b) THREAD CLASS