0% found this document useful (0 votes)
6 views40 pages

Java Practical

The document contains Java programming assignments from a student named Vala Dhaval Bharatbhai, detailing various tasks such as arithmetic operations, sorting arrays, designing bank account classes, handling command line arguments, and managing employee information. Each assignment includes code snippets and expected outputs, demonstrating the student's understanding of Java concepts like classes, threads, and exception handling. The document serves as a comprehensive collection of practical programming exercises for a BCA course.

Uploaded by

dhavalvala2006
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)
6 views40 pages

Java Practical

The document contains Java programming assignments from a student named Vala Dhaval Bharatbhai, detailing various tasks such as arithmetic operations, sorting arrays, designing bank account classes, handling command line arguments, and managing employee information. Each assignment includes code snippets and expected outputs, demonstrating the student's understanding of Java concepts like classes, threads, and exception handling. The document serves as a comprehensive collection of practical programming exercises for a BCA course.

Uploaded by

dhavalvala2006
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/ 40

SYBCA SEM-4 Name : Vala Dhaval Bharatbhai

Roll No : 2363

1.Write a Java program to print the sum (addition), multiply, subtract, divide
and remainder of two numbers arithmetic

Ans:-
import java.util.Scanner;

class ArithmeticOperation{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Number: ");

double num1=sc.nextDouble();
System.out.println("Enter Second Number: ");
double num2=sc.nextDouble();

double sum=num1+num2;
double diffrence=num1-num2;
double product=num1*num2;
double quotient=num1/num2;
double reminder=num1%num2;

System.out.println("Sum: "+sum);
System.out.println("Diffrence: "+diffrence);
System.out.println("Product: "+product);

if(num2 !=0){
System.out.println("Quotient: "+quotient);
System.out.println("Reminder: "+reminder);
}else{

SASCMA BCA COLLEGE 1


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

System.out.println("Error! Division by zero is not allowed.");


}
sc.close();
}
}

Output:-

SASCMA BCA COLLEGE 2


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

2.Write a Java program to sort array elements.


Ans:-
import java.util.Arrays;
class SortArray{
public static void main(String args[]){
int[] array={5,2,8,1,9};
System.out.println("Original array: "+Arrays.toString(array));
Arrays.sort(array);
System.out.println("Sorted array: "+Arrays.toString(array));
}
public static void main(int[] array){
int n=array.length;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(array[j]>array[j+1]){
//array[j] and array[j+1];
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}

SASCMA BCA COLLEGE 3


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

Output:-

SASCMA BCA COLLEGE 4


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

3. Write a program which design Bank Account class as Saving and Current
Account and manage information accordingly.
Ans:-
abstract class BankAccount {
String accountNumber;
String accountHolderName;
double balance;
public BankAccount(String accountNumber, String accountHolderName, double balance) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountHolderName() {
return accountHolderName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
public abstract void withdraw(double amount);

SASCMA BCA COLLEGE 5


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

public abstract void displayAccountInfo();


}
// SavingAccount.java
class SavingAccount extends BankAccount {
private double interestRate;
public SavingAccount(String accountNumber, String accountHolderName, double balance,
double interestRate) {
super(accountNumber, accountHolderName, balance);
this.interestRate = interestRate;
}
public void withdraw(double amount) {
if (amount > getBalance()) {
System.out.println("Insufficient balance.");
} else {
super.balance -= amount;
System.out.println("Withdrawn: " + amount);
}
}
public void displayAccountInfo() {
System.out.println("Account Number: " + getAccountNumber());
System.out.println("Account Holder Name: " + getAccountHolderName());
System.out.println("Balance: " + getBalance());
System.out.println("Interest Rate: " + interestRate + "%");
}
}
// CurrentAccount.java

SASCMA BCA COLLEGE 6


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

class CurrentAccount extends BankAccount {


private double overdraftLimit;
public CurrentAccount(String accountNumber, String accountHolderName, double balance,
double overdraftLimit) {
super(accountNumber, accountHolderName, balance);
this.overdraftLimit = overdraftLimit;
}
public void withdraw(double amount) {
if (amount > getBalance() + overdraftLimit) {
System.out.println("Transaction exceeds overdraft limit.");
} else {
super.balance -= amount;
System.out.println("Withdrawn: " + amount);
}
}
public void displayAccountInfo() {
System.out.println("Account Number: " + getAccountNumber());
System.out.println("Account Holder Name: " + getAccountHolderName());
System.out.println("Balance: " + getBalance());
System.out.println("Overdraft Limit: " + overdraftLimit);
}
}
// MainMethod.java
class SavingData {
public static void main(String[] args) {

SASCMA BCA COLLEGE 7


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

SavingAccount savingAccount = new SavingAccount("SAV123", "John Doe", 1000.0, 4.0);


CurrentAccount currentAccount = new CurrentAccount("CUR456", "Jane Doe", 500.0,
2000.0);
savingAccount.displayAccountInfo();
savingAccount.deposit(500.0);

savingAccount.withdraw(200.0);
System.out.println();
currentAccount.displayAccountInfo();
currentAccount.deposit(1000.0);
currentAccount.withdraw(800.0);
}

Output:-

SASCMA BCA COLLEGE 8


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

4. Write a program that accepts two numbers from command line and add two
numbers if the arguments are numbers else display total number of vowels of
two strings.
Ans:-
class CommandLineArguments {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Please provide exactly two arguments.");
return;
}
try {
double num1 = Double.parseDouble(args[0]);
double num2 = Double.parseDouble(args[1]);
double sum = num1 + num2;
System.out.println("The sum of the two numbers is: " + sum);
} catch (NumberFormatException e) {
int vowelCount = countVowels(args[0]) + countVowels(args[1]);
System.out.println("The total number of vowels in the two strings is: " + vowelCount);
}
}
public static int countVowels(String str) {
int count = 0;

str = str.toLowerCase(); // Convert string to lowercase to handle both cases


for (int i = 0; i < str.length(); i++) {

char ch = str.charAt(i);

SASCMA BCA COLLEGE 9


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {


count++;
}
}
return count;
}
}

Output:-

SASCMA BCA COLLEGE 10


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

5. Write a program that stores details of 5 employees and display this


information after every one second.
Ans:-
class Employee{
String name;
int age;
String department;
Employee(String name,int age,String department)
{
this.name=name;
this.age=age;
this.department=department;
}
public String toString(){
return "Name:" +name+",Age:"+age+",Department:"+department;
}
}
class programEM{
public static void main(String[] args) throws InterruptedException {
Employee[] employees=new Employee[5];
employees[0]=new Employee("Dhaval",30,"HR");
employees[1]=new Employee("Yash",45,"Marketing");
employees[2]=new Employee("Parth",28,"Sales");
employees[3]=new Employee("Divy",34,"IT");
employees[4]=new Employee("Dhaval",44,"HR");
for(int i=0;i<employees.length;i++){

SASCMA BCA COLLEGE 11


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

System.out.println(employees[i]);
Thread.sleep(1000);
}
}
}

Output :-

SASCMA BCA COLLEGE 12


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

6. Write a program to accept 5 command line argument and then raise the custom
exception if any argument is not from the list
(“BCA”,”MCA”,”BBA”,”MBA”,”OTHER”).
Ans:-
class InvalidArgumentException extends Exception {
public InvalidArgumentException(String message) {
super(message);
}
}
public class CommandLineArguments {
public static void main(String[] args) {
if (args.length != 5) {
System.out.println("Please provide exactly 5 arguments.");
return;
}
String[] validArguments = {"BCA", "MCA", "BBA", "MBA", "OTHER"};

for (int i = 0; i < args.length; i++) {


try {
if (!isValidArgument(args[i], validArguments)) {
throw new InvalidArgumentException("Invalid argument: " + args[i]);
}

System.out.println("Valid argument: " + args[i]);


} catch (InvalidArgumentException e) {
System.out.println(e.getMessage());
}
}

SASCMA BCA COLLEGE 13


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

}
public static boolean isValidArgument(String arg, String[] validArguments) {
for (String valid : validArguments) {
if (valid.equals(arg)) {
return true;
}
}
return false;
}
}

Output:-

SASCMA BCA COLLEGE 14


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

7. Write a java code which accepts name of 10 students. Sort the names of
students in ascending order. Display the names of students using thread class at
interval of one second.
Ans:-
import java.util.Arrays;
import java.util.Scanner;
class StudentThread extends Thread {
String[] studentNames;
public StudentThread(String[] studentNames) {
this.studentNames = studentNames;
}
public void run() {
for (String studentName : studentNames) {
System.out.println(studentName);
try {
Thread.sleep(1000); // sleep for 1 second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
class StudentNameSorter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the names of 10 students:");

String[] studentNames = new String[10];


for (int i = 0; i < 10; i++) {
studentNames[i] = scanner.nextLine();
}
Arrays.sort(studentNames);
System.out.println("Sorted student names:");

StudentThread thread = new StudentThread(studentNames);


thread.start();
}
}

SASCMA BCA COLLEGE 15


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

Output:-

SASCMA BCA COLLEGE 16


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

8.Write java application which accept a string and display the string in reverse
order by interchanging its odd positioned characters with even positioned
characters.
Ans:-
import java.util.Scanner;
public class ReverseAndSwapCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
char[] charArray = input.toCharArray();
for (int i = 0; i < charArray.length - 1; i += 2) {
char temp = charArray[i];
charArray[i] = charArray[i + 1];
charArray[i + 1] = temp;
}
String reversedString = new StringBuilder(new String(charArray)).reverse().toString();
System.out.println("The string after interchanging odd and even positions and reversing: ");
System.out.println(reversedString);
scanner.close();
}
}

Output:-

SASCMA BCA COLLEGE 17


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

9. Write an application that executes three threads from one thread class. One
thread displays “JAVA” every 1 second. another display “PAPER” every 2 seconds
and last one display “COLLEGE” every 3 seconds. Create the thread by using
Runnable Interface.
Ans:-
class JavaRunnable implements Runnable {
public void run() {
try {
while (true) {
System.out.println("JAVA");
Thread.sleep(1000); // Sleep for 1 second
}
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
class PaperRunnable implements Runnable {
public void run() {
try {
while (true) {
System.out.println("PAPER");
Thread.sleep(2000); // Sleep for 2 seconds
}
} catch (InterruptedException e) {
System.out.println(e.getMessage());

SASCMA BCA COLLEGE 18


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

}
}
}
class CollegeRunnable implements Runnable {
public void run() {
try {
while (true) {
System.out.println("COLLEGE");
Thread.sleep(3000);
}
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
class ThreadExample {
public static void main(String[] args) {
Runnable javaRunnable = new JavaRunnable();
Runnable paperRunnable = new PaperRunnable();
Runnable collegeRunnable = new CollegeRunnable();

Thread javaThread = new Thread(javaRunnable);


Thread paperThread = new Thread(paperRunnable);
Thread collegeThread = new Thread(collegeRunnable);

javaThread.start();
paperThread.start();

SASCMA BCA COLLEGE 19


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

collegeThread.start();
}
}

Output:-

SASCMA BCA COLLEGE 20


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

10. Write a program to input two strings search similar characters from both
string and replace it with ‘*’.
Ans:-
import java.util.Scanner;
class SimReplace{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter second string: ");
String str2 = scanner.nextLine();
System.out.println("Original strings:");
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);

String modifiedStr1 = replaceSimilarChars(str1, str2);


String modifiedStr2 = replaceSimilarChars(str2, str1);
System.out.println("\nModified strings:");
System.out.println("String 1: " + modifiedStr1);
System.out.println("String 2: " + modifiedStr2);
}
public static String replaceSimilarChars(String str1, String str2) {
StringBuilder modifiedStr = new StringBuilder(str1);

for (int i = 0; i < str1.length(); i++) {


char c = str1.charAt(i);
if (str2.indexOf(c) != -1) {

SASCMA BCA COLLEGE 21


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

modifiedStr.setCharAt(i, '*');
}
}
return modifiedStr.toString();
}
}

Output:-

SASCMA BCA COLLEGE 22


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

11. Write a java program that accepts string data. Extract either all vowels or all
Non-vowels from given data according to option selection.
Ans:-
import java.util.Scanner;
public class ExtractVowelsNonVowels {
public static String extractVowels(String input) {
StringBuilder vowels = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (isVowel(ch)) {
vowels.append(ch);
}
}
return vowels.toString();
}
public static String extractNonVowels(String input) {
StringBuilder nonVowels = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);

if (!isVowel(ch)) {
nonVowels.append(ch);
}
}
return nonVowels.toString();
}

SASCMA BCA COLLEGE 23


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

public static boolean isVowel(char ch) {


ch = Character.toLowerCase(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
System.out.println("Select an option:");
System.out.println("1. Extract Vowels");
System.out.println("2. Extract Non-Vowels");
System.out.print("Enter your choice (1 or 2): ");

int choice = scanner.nextInt();


String result = "";
if (choice == 1) {
result = extractVowels(input);
System.out.println("Vowels in the string: " + result);
} else if (choice == 2) {

result = extractNonVowels(input);
System.out.println("Non-Vowels in the string: " + result);
} else {
System.out.println("Invalid choice!");
}
scanner.close();
}

SASCMA BCA COLLEGE 24


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

Output:-

SASCMA BCA COLLEGE 25


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

12. Write a java program that accepts string data from user and then provide
options for changing case into any of the following: (UPPERCASE, lowercase, and
Sentence case)
Ans:-
import java.util.Scanner;
class CaseChanger {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
System.out.println("Select an option:");
System.out.println("1. Convert to UPPERCASE");
System.out.println("2. Convert to lowercase");
System.out.println("3. Convert to Sentence case");
int option = scanner.nextInt();
scanner.close();
switch (option) {
case 1:
System.out.println("Converted string: " + inputString.toUpperCase());
break;
case 2:
System.out.println("Converted string: " + inputString.toLowerCase());
break;
case 3:
System.out.println("Converted string: " + toSentenceCase(inputString));
break;

SASCMA BCA COLLEGE 26


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

default:
System.out.println("Invalid option");
}
}
public static String toSentenceCase(String inputString) {
String[] words = inputString.split("\\s+");
StringBuilder sentenceCase = new StringBuilder();
for (String word : words) {
sentenceCase.append(Character.toUpperCase(word.charAt(0)));
sentenceCase.append(word.substring(1).toLowerCase());
sentenceCase.append(" ");
}
return sentenceCase.toString().trim();
}
}

Output:-

SASCMA BCA COLLEGE 27


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

13. Write a program to create singly Link List and perform following operations:
1. Insert a node at desired Location.
2. Delete a node from given Location.
3. Update desired Location.
4. Search
5. Display
Ans:-
import java.util.Scanner;
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
newNode.next = head;
head = newNode;

SASCMA BCA COLLEGE 28


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

}
}
public void insertAtDesiredLocation(int data, int location) {
Node newNode = new Node(data);
if (location == 0) {
insertAtBeginning(data);
} else {
Node temp = head;
for (int i = 0; i < location - 1; i++) {
if (temp.next == null) {
System.out.println("Location out of bounds");
return;
}
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
}
}
public void deleteAtDesiredLocation(int location) {
if (location == 0) {
head = head.next;
} else {
Node temp = head;
for (int i = 0; i < location - 1; i++) {
if (temp.next == null) {

SASCMA BCA COLLEGE 29


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

System.out.println("Location out of bounds");


return;
}
temp = temp.next;
}
if (temp.next == null) {
System.out.println("Location out of bounds");
} else {
temp.next = temp.next.next;
}
}
}
public void updateAtDesiredLocation(int data, int location) {
Node temp = head;
for (int i = 0; i < location; i++) {
if (temp == null) {
System.out.println("Location out of bounds");
return;
}
temp = temp.next;
}
if (temp != null) {
temp.data = data;
}
}
public boolean search(int data) {

SASCMA BCA COLLEGE 30


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

Node temp = head;


while (temp != null) {
if (temp.data == data) {
return true;
}
temp = temp.next;
}
return false;
}
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
}
class LinkListOP {
public static void main(String[] args) {
LinkedList list = new LinkedList();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Insert at beginning");
System.out.println("2. Insert at desired location");
System.out.println("3. Delete at desired location");

SASCMA BCA COLLEGE 31


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

System.out.println("4. Update at desired location");


System.out.println("5. Search");
System.out.println("6. Display");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter data to insert: ");
int data = scanner.nextInt();
list.insertAtBeginning(data);
break;
case 2:
System.out.print("Enter data to insert: ");
data = scanner.nextInt();
System.out.print("Enter location to insert: ");
int location = scanner.nextInt();
list.insertAtDesiredLocation(data, location);
break;
case 3:
System.out.print("Enter location to delete: ");
location = scanner.nextInt();
list.deleteAtDesiredLocation(location);
break;
case 4:
System.out.print("Enter data to update: ");

SASCMA BCA COLLEGE 32


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

data = scanner.nextInt();
System.out.print("Enter location to update: ");
location = scanner.nextInt();
list.updateAtDesiredLocation(data, location);
break;
case 5:
System.out.print("Enter data to search: ");
data = scanner.nextInt();
if (list.search(data)) {
System.out.println("Data found");
} else {
System.out.println("Data not found");
}
break;
case 6:
System.out.println("Linked list:");
list.display();
break;
case 7:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}

SASCMA BCA COLLEGE 33


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

Output:-

SASCMA BCA COLLEGE 34


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

14. Write a program to create singly circular Link List and perform following
operations:
1. Insert a node at beginning.
2. Insert a node at End.
3. Delete First node.
4. Delete Last node.
5. Display.
Ans:-
import java.util.Scanner;
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class CircularLinkedList {
Node head;
Node tail;
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;

SASCMA BCA COLLEGE 35


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

newNode.next = head;
} else {
newNode.next = head;
head = newNode;
tail.next = head;
}
}
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
} else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
}
public void deleteFirstNode() {
if (head == null) {
System.out.println("List is empty");
} else if (head == tail) {
head = null;
tail = null;
} else {

SASCMA BCA COLLEGE 36


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

head = head.next;
tail.next = head;
}
}
public void deleteLastNode() {
if (head == null) {
System.out.println("List is empty");
} else if (head == tail) {
head = null;
tail = null;
} else {
Node temp = head;
while (temp.next != tail) {
temp = temp.next;
}
temp.next = head;
tail = temp;
}
}
public void display() {
if (head == null) {
System.out.println("List is empty");
} else {
Node temp = head;
do {
System.out.print(temp.data + " ");

SASCMA BCA COLLEGE 37


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

temp = temp.next;
} while (temp != head);
System.out.println();
}
}
}
class CircleQueue {
public static void main(String[] args) {
CircularLinkedList list = new CircularLinkedList();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Insert at beginning");
System.out.println("2. Insert at end");
System.out.println("3. Delete first node");
System.out.println("4. Delete last node");
System.out.println("5. Display");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter data to insert: ");
int data = scanner.nextInt();
list.insertAtBeginning(data);
break;
case 2:

SASCMA BCA COLLEGE 38


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

System.out.print("Enter data to insert: ");


data = scanner.nextInt();
list.insertAtEnd(data);
break;
case 3:
list.deleteFirstNode();
break;
case 4:
list.deleteLastNode();
break;
case 5:
System.out.println("Circular linked list:");
list.display();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}

Output:-

SASCMA BCA COLLEGE 39


SYBCA SEM-4 Name : Vala Dhaval Bharatbhai
Roll No : 2363

SASCMA BCA COLLEGE 40

You might also like