[Go to site: main page, start]

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

Java Lab File

The document contains a series of Java programs demonstrating various programming concepts such as printing output, conditional statements, arrays, strings, vectors, exception handling, inheritance, polymorphism, encapsulation, sorting algorithms, and the use of packages and interfaces. Each program is accompanied by a brief explanation of the theory behind the concept it illustrates. The document serves as a comprehensive guide for learning Java programming through practical examples.

Uploaded by

adi885511
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 views28 pages

Java Lab File

The document contains a series of Java programs demonstrating various programming concepts such as printing output, conditional statements, arrays, strings, vectors, exception handling, inheritance, polymorphism, encapsulation, sorting algorithms, and the use of packages and interfaces. Each program is accompanied by a brief explanation of the theory behind the concept it illustrates. The document serves as a comprehensive guide for learning Java programming through practical examples.

Uploaded by

adi885511
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

Program-1

Write a java program to print hello java.


class hello

public static void main(String[] args)

[Link](“Hello Java”);

}
Program – 2
Write a java program to demonstrate the use of if statement.
import [Link].*;

public class condition{

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter your age");

int age=[Link]();

if(age>=18) {

[Link]("adult");

else {

[Link]("child");

} } }
Program -3

class VariablesExample {

public static void main(String[] args) {

int number = 10;

float price = 25.5f;

char grade = 'A';

boolean status = true;

[Link]("Number: " + number);

[Link]("Price: " + price);

[Link]("Grade: " + grade);

[Link]("Status: " + status);

}
Program- 4

class ConstantsExample
{
public static void main(String[] args)
{
final double PI = 3.14159;
[Link]("Value of PI: " + PI);
}
}
Java programs with theory: Arrays, Strings and Vectors.
One Dimensional Array

Program 5
public class OneDArrayExample {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
[Link]("Elements of 1-D Array:");
for (int i = 0; i < [Link]; i++)
{
[Link](arr[i]);
}
}}
Two Dimensional Array
Program 6:
public class TwoDArrayExample {
public static void main(String[] args) {
int[][] arr = { {1, 2, 3}, {4, 5, 6} };
[Link]("Elements of 2-D Array:");
for (int i = 0; i < [Link]; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
[Link](arr[i][j] + " ");
}
[Link]();
}}
String in java
Program- 7
public class StringExample {
public static void main(String[] args) {
String text = "Legal NLP";
[Link]("Original String: " + text);
[Link]("Length: " + [Link]());
[Link]("Uppercase: " + [Link]());
}}
Vector in Java
Program 8
import [Link];
public class VectorExample {
public static void main(String[] args) {
Vector v = new Vector<>();
[Link]("Court");
[Link]("Judge");
[Link]("Law");
[Link]("Elements of Vector:");
for (String item : v){
[Link](item);
}}}

Output
Program- 9
class WrapperClass{
public static void main(String[]args){
int num=25; double price=99.99;
char letters=’A’;
Integers objNum= num;
Double objPrice = price;
Character objLetter=Letter;
[Link](“Integer object: “+ objNum);
[Link](“Double object: “+ objPrice);
[Link](“Character object: “+ objLetter);
int new Num=objNum;
double newPrice=objPrice;
[Link](“Unboxed integer: “+ newNum);
[Link](“Unboxed Double: “+ newPrice);
}}
Output
Program - 10
Write a java code to implement inheritance, abstraction, polymorphism and
encapsulation.
Abstraction

Theory: Abstraction means hiding implementation details and showing only functionality
using abstract classes or interfaces.

Program:

abstract class Animal {


abstract void sound();
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
public class AbstractionDemo {
public static void main(String[] args) {
Animal a = new Dog();
[Link]();
}
}
Program - 11
Polymorphism

Theory: Polymorphism means one method having multiple forms. It is achieved using
method overloading or overriding.

Program:
class MathOperation {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}}
public class PolymorphismDemo {
public static void main(String[] args) {
MathOperation obj = new MathOperation();
[Link]([Link](2, 3));
[Link]([Link](2, 3, 4));
}
}
Program - 12
Encapsulation

Theory: Encapsulation means wrapping data and methods together and restricting access
using private variables and getter/setter methods.

Program:
class Student {
private String name;

public void setName(String name) {


[Link] = name;
}
public String getName() {
return name;
}
}
public class EncapsulationDemo {
public static void main(String[] args) {
Student s = new Student();
[Link]("Ali Murtza");
[Link]([Link]());
}
}
Program – 13
write a java program of constructors, method overloading and method
overriding.
Constructor

Theory: Constructor is a special method used to initialize objects. It has same name as
class.

Program:
class Demo {
Demo() {
[Link]("Default Constructor");
}
}
public class ConstructorDemo {
public static void main(String[] args) {
Demo d = new Demo();
}
}
Program – 14
Method Overloading
Theory: Method overloading means same method name with different parameters.

Program:
class Overload {
void show(int a) {
[Link](a);
}
void show(int a, int b) {
[Link](a + " " + b);
}
}
public class OverloadingDemo {
public static void main(String[] args) {
Overload obj = new Overload();
[Link](20);
[Link](20,30);
}
}
Program - 15
Method Overriding
Theory: Method overriding means redefining parent class method in child class.

Program:
class Parent {
void display() {
[Link]("Parent Method");
}
}
class Child extends Parent {
void display() {
[Link]("Child Method");
}
}
public class OverridingDemo {
public static void main(String[] args) {
Child c = new Child();
[Link]();
}
}
Program – 16
Write a Java program to demonstrate multilevel inheritance.
class Animal {

void eat() {

[Link]("eating..."); }}

class Dog extends Animal {

void bark() {

[Link]("barking..."); } }

class BabyDog extends Dog {

void weep() {

[Link]("weeping..."); } }

class shweta {

public static void main(String args[]) {

BabyDog d = new BabyDog();

[Link]();

[Link]();

[Link](); } }
Experiment: write a separate java code to implement each of the following: exception
handling with try, catch, throw, throws, finally multiple each statement with the
following exceptions: Arithmetic exception, array out of bounds exception and array
store exception.

Theory

Exception handling in Java is a mechanism to handle runtime errors, allowing the program
to continue execution. Java provides five keywords for exception handling: try, catch, throw,
throws, and finally.

try: Used to define a block of code that may throw an exception.

catch: Used to handle the exception.

throw: Used to explicitly throw an exception.

throws: Used to declare exceptions.

finally: Executes regardless of exception occurrence.

Programs - 17

1. Try-Catch with ArithmeticException

class TryCatchExample {
public static void main(String[] args) {
try {
int a = 10/0;
} catch (ArithmeticException e) {
[Link]("Arithmetic Exception Occurred");
}
}
}

output
Program -18
class ThrowExample {
static void checkAge(int age) {
if (age < 18)
throw new ArithmeticException("Not eligible");
else
[Link]("Eligible");
}
public static void main(String[] args) {
checkAge(15);
}
}

Output
Program -19
class ThrowsExample {
static void check() throws ArithmeticException {
int a = 10/0;
}
public static void main(String[] args) {
try {
check();
} catch (Exception e) {
[Link]("Handled");
}
}
}

Output
Program – 20
class FinallyExample {

public static void main(String[] args) {

try {

int a = 5 / 0;

} catch (Exception e) {

[Link]("Exception caught");

} finally {

[Link]("Finally block executed");

Output
Program -21
class MultipleException {

public static void main(String[] args) {

try {

int arr[] = new int[2];

arr[5] = 10;

} catch (ArrayIndexOutOfBoundsException e) {

[Link]("Array Index Out Of Bounds");

} catch (ArithmeticException e) {

[Link]("Arithmetic Error");

Output
Experiment : Write a java code to implement package and how to import
them and interface.
Theory

A package in Java is a namespace that organizes classes and interfaces. Interfaces define
methods that a class must implement.

Program- 22
1. Package Example

package mypack;

public class Hello {


public void display() {
[Link]("Hello Package");
}
}

2. Import Package

import [Link];

class Test {
public static void main(String[] args) {
Hello obj = new Hello();
[Link]();
}
}

Output
Experiment: Write a separate java code to implement each of the
following: bubble sort, selection sort, insertion sort, merge sort.
Theory
Sorting algorithms are used to arrange data in a specific order. Common sorting
techniques include Bubble Sort, Selection Sort, Insertion Sort, and Merge Sort.

Program- 23
1. Bubble Sort
class BubbleSort {
public static void main(String[] args) {
int arr[] = {5,3,8,4};
for(int i=0;i<[Link];i++) {
for(int j=0;j<[Link]-i-1;j++) {
if(arr[j]>arr[j+1]) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp; } } }
for(int i:arr) [Link](i+" "); }}

Output
Program -24
class SelectionSort {

public static void main(String[] args) {

int arr[] = {5, 3, 8, 4};

for (int i = 0; i < [Link]; i++) {

int min = i;

for (int j = i + 1; j < [Link]; j++) {

if (arr[j] < arr[min])

min = j;

int temp = arr[min];

arr[min] = arr[i];

arr[i] = temp;

for (int i : arr)

[Link](i + " ");

Output
Program – 25

class InsertionSort {

public static void main(String[] args) {

int arr[] = {5, 3, 8, 4};

for (int i = 1; i < [Link]; i++) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--; }

arr[j + 1] = key;

for (int i : arr)

[Link](i + " "); }}

Output
Program – 26
class MergeSort {

void merge(int arr[], int l, int m, int r) {

int n1 = m - l + 1;

int n2 = r - m;

int L[] = new int[n1];

int R[] = new int[n2];

for (int i = 0; i < n1; i++)

L[i] = arr[l + i];

for (int j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

int i = 0, j = 0, k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j])

arr[k++] = L[i++];

else

arr[k++] = R[j++]; }

while (i < n1)

arr[k++] = L[i++];

while (j < n2)

arr[k++] = R[j++]; }

void sort(int arr[], int l, int r) {

if (l < r) {

int m = (l + r) / 2;

sort(arr, l, m);

sort(arr, m + 1, r);
merge(arr, l, m, r); } }

public static void main(String args[]) {

int arr[] = {5, 3, 8, 4};

MergeSort ob = new MergeSort();

[Link](arr, 0, [Link] - 1);

for (int i : arr)

[Link](i + " ");

Output

You might also like