0% found this document useful (0 votes)
2 views23 pages

Java Programs MCA

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)
2 views23 pages

Java Programs MCA

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/ 23

1) Write a java program to create class and object

public class Main

int x = 5;

public static void main(String[] args)

Main myObj = new Main();

System.out.println(myObj.x);

}}

OUTPUT 5

2) Write a java program find garbage collection and finalize method

public class GarbageCollectionDemo {


public static void main(String[] args)

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

new Demo();

System.gc();

class Demo

Demo()

{
System.out.println("Object Created!");

@Override

protected void finalize() throws Throwable

Try

System.out.println("Object is being garbage collected!");

Finally

super.finalize();

}}

OUTPUT

Object Created!

Object Created!

Object Created!

Object Created!

Object Created!

Object is being garbage collected!

Object is being garbage collected!

Object is being garbage collected!

Object is being garbage collected!


3) Write a java progrm implementation of queue class?

import java.util.NoSuchElementException;

public class Queue<T> {

private Node<T> front;

private Node<T> rear;

private int size;

private static class Node<T> {

private T data;

private Node<T> next;

public Node(T data) {

this.data = data;

public void enqueue(T data) {

Node<T> newNode = new Node<>(data);

if (rear != null) {

rear.next = newNode;

rear = newNode;

if (front == null) {

front = newNode;

size++;

}
public T dequeue() {

if (isEmpty()) {

throw new NoSuchElementException("Queue is empty");

T data = front.data;

front = front.next;

if (front == null) {

rear = null;

size--;

return data;

} // Return the front element without removing it

public T peek() {

if (isEmpty()) {

throw new NoSuchElementException("Queue is empty");

return front.data;

}// Check if the queue is empty

public boolean isEmpty() {

return front == null;

}// Return the number of elements in the queue

public int size() {

return size;

}
// Clear all elements from the queue

public void clear() {

front = rear = null;

size = 0;

public static void main(String[] args) {

Queue<Integer> queue = new Queue<>();

queue.enqueue(1);

queue.enqueue(2);

queue.enqueue(3);

System.out.println("Front element: " + queue.peek());

System.out.println("Queue size: " + queue.size());

System.out.println("Dequeue: " + queue.dequeue());

System.out.println("Queue size after dequeue: " + queue.size());

queue.clear();

System.out.println("Queue size after clear: " + queue.size());

OUTPUT

Front element: 1

Queue size: 3

Dequeue: 1

Queue size after dequeue: 2

Queue size after clear: 0


4) Write java program to find Quick sort

public class QuickSort {

public static void quickSort(int[] arr, int low, int high) {

if (low < high) {

int pivotIndex = partition(arr, low, high);

quickSort(arr, low, pivotIndex - 1);

quickSort(arr, pivotIndex + 1, high);

private static int partition(int[] arr, int low, int high) {

int pivot = arr[high]; // Choose the last element as the pivot

int i = low - 1; // Index for smaller elements

for (int j = low; j < high; j++) {

if (arr[j] < pivot) {

i++;

// Swap arr[i] and arr[j]

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;
return i + 1; // Return the pivot index

public static void main(String[] args) {

int[] arr = { 10, 7, 8, 9, 1, 5 };

System.out.println("Original array:");

printArray(arr);

quickSort(arr, 0, arr.length - 1);

System.out.println("Sorted array:");

printArray(arr);

private static void printArray(int[] arr) {

for (int num : arr) {

System.out.print(num + " ");

System.out.println();

OUTPUT

Original array:

10 7 8 9 1 5

Sorted array:

1 5 7 8 9 10
5) Write a java program to extending vehicle class

class Vehicle {

String brand;

public Vehicle(String brand) {

this.brand = brand;

public void displayDetails() {

System.out.println("Brand: " + brand);

class Car extends Vehicle {

int doors;

public Car(String brand, int doors) {

super(brand);

this.doors = doors;

@Override

public void displayDetails() {

super.displayDetails();

System.out.println("Doors: " + doors);

class Bike extends Vehicle {

boolean hasCarrier;
public Bike(String brand, boolean hasCarrier) {

super(brand);

this.hasCarrier = hasCarrier;

@Override

public void displayDetails() {

super.displayDetails();

System.out.println("Has Carrier: " + (hasCarrier ? "Yes" : "No"));

public class VehicleTest {

public static void main(String[] args) {

new Car("Toyota", 4).displayDetails();

new Bike("Yamaha", true).displayDetails();

OUTPUT

Brand: Toyota

Doors: 4

Brand: Yamaha

Has Carrier: Yes

6 Write a java program create a queue interface?

import java.util.LinkedList;

import java.util.Queue;
public class QueueExample {

public static void main(String[] args) {

Queue<String> queue = new LinkedList<>();

queue.add("apple");

queue.add("banana");

queue.add("cherry");

System.out.println("Queue: " + queue);

String front = queue.remove();

System.out.println("Removed element: " + front);

System.out.println("Queue after removal: " + queue);

queue.add("date");

String peeked = queue.peek();

System.out.println("Peeked element: " + peeked);

System.out.println("Queue after peek: " + queue);

Output
Queue: [apple, banana, cherry]
Removed element: apple
Queue after removal: [banana, cherry]
Peeked element: banana
Queue after peek: [banana, cherry, date]
7 Write a java program to create a thread?

import java.io.*;

import java.util.*;
class ThreadImpl extends Thread

@Override

public void run()

String str = "Thread Class Implementation Thread"

+ " is Running Successfully";

System.out.println(str);

class RunnableThread implements Runnable

@Override

public void run()

String str = "Runnable Interface Implementation Thread"

+ " is Running Successfully";

System.out.println(str);

public class Geeks

public static void main(String[] args)

{ ThreadImpl t1 = new ThreadImpl();

t1.start();
RunnableThread g2 = new RunnableThread();

Thread t2 = new Thread(g2);

t2.start();

Output
Runnable Interface Implementation Thread is Running Successfully
Thread Class Implementation Thread is Running Successfully

8 // Java Program to Implement Traffic signal

// Using Java Swing Components

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

public class Traffic_Signal

extends JFrame implements ItemListener {

JRadioButton jr1;

JRadioButton jr2;

JRadioButton jr3;

JTextField j1 = new JTextField(10);

ButtonGroup b = new ButtonGroup();


String msg = " ";

// Initially setting the co-ordinates to 0,0,0

int x = 0, y = 0, z = 0;

public Traffic_Signal(String msg)

super(msg);

setLayout(new FlowLayout());

// Assigning name to the button declared above

// with help of JRadioButton class

jr1 = new JRadioButton("Red");

jr2 = new JRadioButton("Yellow");

jr3 = new JRadioButton("Green");

jr1.addItemListener(this);

jr2.addItemListener(this);

jr3.addItemListener(this);

add(jr1);

add(jr2);

add(jr3);

b.add(jr1);
b.add(jr2);

b.add(jr3);

add(j1);

// Method 1

// To add a window

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e)

// It haults here itself

System.exit(0);

});

// Method 2

// To change colors of traffic signal

public void itemStateChanged(ItemEvent ie)

// If it is red

if (ie.getSource() == jr1) {

if (ie.getStateChange() == 1) {
// Then display message- Stop

msg = "Stop!";

x = 1;

// Repainting the box with original one

// Practically black

repaint();

else {

msg = "";

// If state is Orange or technically jr2

if (ie.getSource() == jr2) {

if (ie.getStateChange() == 1) {

// Then display message-

// Get ready in waiting state

msg = "Get Ready to go!";

y = 1;
// Again repainting the button

repaint();

else {

msg = "";

// If state is Green

if (ie.getSource() == jr3) {

if (ie.getStateChange() == 1) {

// Then display message- Go

msg = "Go!!";

z = 1;

repaint();

else {

msg = "";

j1.setText(msg);

}
// Method 3

// handling the paint graphics and

// dimensions of the buttons via

// setting co-ordinates

public void paint(Graphics g)

g.drawRect(100, 105, 110, 270);

g.drawOval(120, 150, 60, 60);

g.drawOval(120, 230, 60, 60);

g.drawOval(120, 300, 60, 60);

// Case: Red

if (x == 1) {

g.setColor(Color.RED);

g.fillOval(120, 150, 60, 60);

g.setColor(Color.WHITE);

g.fillOval(120, 230, 60, 60);

g.setColor(Color.WHITE);

g.fillOval(120, 300, 60, 60);

x = 0;

}
// Case: Orange

if (y == 1) {

g.setColor(Color.WHITE);

g.fillOval(120, 150, 60, 60);

g.setColor(Color.YELLOW);

g.fillOval(120, 230, 60, 60);

g.setColor(Color.WHITE);

g.fillOval(120, 300, 60, 60);

y = 0;

// Case: Green

if (z == 1) {

g.setColor(Color.WHITE);

g.fillOval(120, 150, 60, 60);

g.setColor(Color.WHITE);

g.fillOval(120, 230, 60, 60);

g.setColor(Color.GREEN);

g.fillOval(120, 300, 60, 60);

z = 0;

}
// Method 4

// Main driver method

public static void main(String args[])

// Creating object of Jframe class inside main()

// method

JFrame jf = new Traffic_Signal("Traffic Light");

// Setting size and making traffic signal

// operational using setVisible() method

jf.setSize(500, 500);

jf.setVisible(true);

Output:
9 Write a java program Comparing path of two files in Java

import java.io.File;

public class GFG {

public static void main(String[] args)

File file1 = new File("/home/mayur/GFG.java");

File file2 = new File("/home/mayur/file.txt");

File file3 = new File("/home/mayur/GFG.java");

// Path comparison

if (file1.compareTo(file2) == 0) {

System.out.println(

"paths of file1 and file2 are same");

else {

System.out.println(

"Paths of file1 and file2 are not same");

// Path comparison

if (file1.compareTo(file3) == 0) {
System.out.println(

"paths of file1 and file3 are same");

else {

System.out.println(

"Paths of file1 and file3 are not same");

Output:

1. 10 Write a java program to create generic queue?

import java.util.LinkedList;
2. import java.util.Queue;
3. public class IntegerQueueExample {
4. public static void main(String[] args) {
5. // Create a queue to store integers
6. Queue<Integer> queue = new LinkedList<Integer>();
7. // Add elements to the queue
8. queue.add(10);
9. queue.add(20);
10. queue.add(30);
11. // Display the elements in the queue
12. System.out.println("Elements in the queue: " + queue);
13. // Remove and display the front element of the queue
14. System.out.println("Front element of the queue: " + queue.remove());
15. // Display the elements in the queue after removal
16. System.out.println("Elements in the queue after removal: " + queue);
17. }
18. }
Output:

Elements in the queue: [10, 20, 30]


Front element of the queue: 10
Elements in the queue after removal: [20, 30]

11 Write a Java Program to Make An Applet

import java.awt.*;

import java.awt.applet.*;

public class AppletDemo extends Applet

public void init()

setBackground(Color.black);

setForeground(Color.yellow);

public void paint(Graphics g)

g.drawString("Welcome", 100, 100);

// Save file as AppletDemo.java in local machine


<html>

<applet code = AppletDemo

width = 400

height = 500>

</applet>

</html>

<!-- Save as Applet.html -->

Compilation:
c:\> javac AppletDemo.java
Execution:
c:\> appletviewer AppletDemo.java

OUTPUT
Double click on Applet.html
This won't work on browser as we don't have the proper plugins.

You might also like