JAVA MAIN ASSIGNMENT
1. Write a Java program to sort the elements using a for loop.
Answer:
public class SortUsingForLoop {
public static void main(String[] args) {
int[] array = {5, 2, 8, 7, 1};
for (int i = 0; i < [Link] - 1; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
[Link]("Sorted Array in Ascending Order:");
for (int num : array) {
[Link](num + " ");
Aman Navalur Page 1
JAVA MAIN ASSIGNMENT
2. Write a recursive program to find nth Fibonacci number.
Answer:
public class FibonacciRecursive {
public static int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
public static void main(String[] args) {
int n = 5;
[Link]("Fibonacci number at position " + n + " is: " +
fibonacci(n));
Aman Navalur Page 2
JAVA MAIN ASSIGNMENT
3. Write a program to perform Stack operations using proper class and Methods.
Answer:
class Stack {
private int maxSize;
private int[] stackArray;
private int top;
public Stack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
public void push(int value) {
if (top == maxSize - 1) {
[Link]("Stack is full!");
} else {
stackArray[++top] = value;
public int pop() {
Aman Navalur Page 3
JAVA MAIN ASSIGNMENT
if (top == -1) {
[Link]("Stack is empty!");
return -1;
} else {
return stackArray[top--];
public void display() {
if (top == -1) {
[Link]("Stack is empty!");
} else {
[Link]("Stack elements: ");
for (int i = 0; i <= top; i++) {
[Link](stackArray[i] + " ");
[Link]();
public class StackOperations {
public static void main(String[] args) {
Aman Navalur Page 4
JAVA MAIN ASSIGNMENT
Stack stack = new Stack(5);
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]("Popped element: " + [Link]());
[Link]();
4. Java Program to sort the elements of an array in ascending & disascending
order.
Answer:
import [Link];
import [Link];
public class ArraySortAscDesc {
public static void main(String[] args) {
Integer[] array = {5, 2, 8, 7, 1};
Aman Navalur Page 5
JAVA MAIN ASSIGNMENT
[Link](array);
[Link]("Array in Ascending Order:");
for (int num : array) {
[Link](num + " ");
[Link](array, [Link]());
[Link]("\nArray in Descending Order:");
for (int num : array) {
[Link](num + " ");
5. Write a Java Program to Create an Interface.
Answer:
interface Animal {
void makeSound();
class Dog implements Animal {
public void makeSound() {
[Link]("Bark");
Aman Navalur Page 6
JAVA MAIN ASSIGNMENT
public class InterfaceExample {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
6. Write a Java Program to Show Encapsulation in Class.
Answer:
class Employee {
private String name;
public String getName() {
return name;
public void setName(String name) {
[Link] = name;
}
Aman Navalur Page 7
JAVA MAIN ASSIGNMENT
public class EncapsulationExample {
public static void main(String[] args) {
Employee emp = new Employee();
[Link]("John Doe");
[Link]("Employee Name: " + [Link]());
7. Write a Java Program to Show Inheritance in Class.
Answer:
class Animal {
void eat() {
[Link]("Eating...");
class Dog extends Animal {
void bark() {
[Link]("Barking...");
Aman Navalur Page 8
JAVA MAIN ASSIGNMENT
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
[Link]();
8. Write a Java Program to Show Data Hiding in Class .
Answer:
class BankAccount {
private double balance;
public BankAccount(double balance) {
[Link] = balance;
public double getBalance() {
Aman Navalur Page 9
JAVA MAIN ASSIGNMENT
return balance;
public class DataHidingExample {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.00);
[Link]("Account Balance: " + [Link]());
9. Write a Java Program to Show Overloading of Methods in Class.
Answer:
class Calculator {
int add(int a, int b) {
return a + b;
int add(int a, int b, int c) {
return a + b + c;
Aman Navalur Page 10
JAVA MAIN ASSIGNMENT
double add(double a, double b) {
return a + b;
public class OverloadingExample {
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]("Sum of 2 and 3: " + [Link](2, 3));
[Link]("Sum of 2, 3, and 4: " + [Link](2, 3, 4));
[Link]("Sum of 2.5 and 3.5: " + [Link](2.5, 3.5));
10. Write a Java Program to Show Overriding of Methods in Classes.
Answer:
class Animal {
void makeSound() {
[Link]("Animal sound");
Aman Navalur Page 11
JAVA MAIN ASSIGNMENT
class Dog extends Animal {
@Override
void makeSound() {
[Link]("Bark");
public class OverridingExample {
public static void main(String[] args) {
Animal myDog = new Dog();
[Link]();
11. Write a Java Program to Show Use of Super Keyword in Class.
Answer:
class Vehicle {
String type;
Vehicle(String type) {
[Link] = type;
Aman Navalur Page 12
JAVA MAIN ASSIGNMENT
class Car extends Vehicle {
Car(String type) {
super(type);
void displayType() {
[Link]("Car type: " + type);
public class SuperKeywordExample {
public static void main(String[] args) {
Car car = new Car("Sedan");
[Link]();
Aman Navalur Page 13
JAVA MAIN ASSIGNMENT
12. Write a Java Program to Show Use of This Keyword in Class.
Answer:
class Person {
String name;
Person(String name) {
[Link] = name;
void display() {
[Link]("Name: " + [Link]);
public class ThisKeywordExample {
public static void main(String[] args) {
Person person = new Person("Alice");
[Link]();
Aman Navalur Page 14
JAVA MAIN ASSIGNMENT
13. Write a Java Program to Show Usage of Static keyword in Class.
Answer:
class Counter {
static int count = 0;
Counter() {
count++;
static void displayCount() {
[Link]("Number of objects created: " + count);
public class StaticKeywordExample {
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
[Link]();
Aman Navalur Page 15
JAVA MAIN ASSIGNMENT
14. Write a Java Program to Show Usage of Access Modifier.
Answer:
class AccessModifierExample {
private String privateVariable = "Private Variable";
public String publicVariable = "Public Variable";
protected String protectedVariable = "Protected Variable";
String defaultVariable = "Default Variable";
void display() {
[Link](privateVariable);
[Link](publicVariable);
[Link](protectedVariable);
[Link](defaultVariable);
public class AccessModifierDemo {
public static void main(String[] args) {
AccessModifierExample example = new AccessModifierExample();
[Link]();
[Link]([Link]);
}
Aman Navalur Page 16
JAVA MAIN ASSIGNMENT
Assignment 2:
1. Write a Java Program to Handle the Exception Methods.
Answer:
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
[Link](numbers[3]); // This will throw
ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: " + [Link]());
} finally {
[Link]("This block executes regardless of exception.");
Aman Navalur Page 17
JAVA MAIN ASSIGNMENT
2. Write a Java program to Handle the Checked exceptions.
Answer:
import [Link];
import [Link];
import [Link];
public class CheckedExceptionExample {
public static void main(String[] args) {
try {
File file = new File("[Link]");
Scanner scanner = new Scanner(file);
while ([Link]()) {
[Link]([Link]());
[Link]();
} catch (FileNotFoundException e) {
[Link]("Error: File not found. " + [Link]());
Aman Navalur Page 18
JAVA MAIN ASSIGNMENT
3. Write a Java Program to Handle the Unchecked Exceptions.
Answer:
public class UncheckedExceptionExample {
public static void main(String[] args) {
try {
String str = null;
[Link]([Link]());
catch (NullPointerException e) {
[Link]("Error: " + [Link]());
Aman Navalur Page 19
JAVA MAIN ASSIGNMENT
4. Write a Java Program to Show Thread interface and memory consistency
errors.
Answer:
class Counter implements Runnable {
private int count = 0;
public void run() {
for (int i = 0; i < 1000; i++) {
count++;
public int getCount() {
return count;
public class ThreadInterfaceExample {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Thread thread1 = new Thread(counter);
Thread thread2 = new Thread(counter);
Aman Navalur Page 20
JAVA MAIN ASSIGNMENT
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("Final Count: " + [Link]());
Aman Navalur Page 21
JAVA MAIN ASSIGNMENT
5. Write a Java Program to Check the Thread Status.
Answer:
public class ThreadStatusExample {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
try {
[Link](2000);
} catch (InterruptedException e) {
[Link]();
});
[Link]("Thread status before starting: " + [Link]());
[Link]();
[Link]("Thread status after starting: " + [Link]());
[Link]();
[Link]("Thread status after completion: " + [Link]());
Aman Navalur Page 22
JAVA MAIN ASSIGNMENT
5. Write a Java Program to Suspend a thread.
Answer:
class SuspendResumeThread implements Runnable {
private volatile boolean suspended = false;
public void run() {
for (int i = 1; i <= 5; i++) {
while (suspended) {
try {
[Link](100);
} catch (InterruptedException e) {
[Link]();
[Link](i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link]();
Aman Navalur Page 23
JAVA MAIN ASSIGNMENT
public void suspend() {
suspended = true;
public void resume() {
suspended = false;
public class SuspendResumeExample {
public static void main(String[] args) throws InterruptedException {
SuspendResumeThread task = new SuspendResumeThread();
Thread thread = new Thread(task);
[Link]();
[Link](2000);
[Link]();
[Link]("Thread suspended.");
[Link](2000);
[Link]();
[Link]("Thread resumed.");
[Link]();
Aman Navalur Page 24
JAVA MAIN ASSIGNMENT
6. Write a Java Program The values( ) and valueOf( ) Methods in enum.
Answer:
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY;
public class EnumMethodsExample {
public static void main(String[] args) {
Day[] days = [Link]();
[Link]("Days of the week:");
for (Day day : days) {
[Link](day);
Day day = [Link]("MONDAY");
[Link]("Selected day: " + day);
Aman Navalur Page 25
JAVA MAIN ASSIGNMENT
7. Write a Java program for enumeration.
Answer:
enum Color {
RED, GREEN, BLUE;
public class EnumerationExample {
public static void main(String[] args) {
Color favoriteColor = [Link];
[Link]("Favorite color: " + favoriteColor);
[Link]("All colors:");
for (Color color : [Link]()) {
[Link](color);
Aman Navalur Page 26