SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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 fun{
int sum(int x,int y){
int s;
s=x+y;
System.out.println(x+" + "+y+" = "+s);
return s;
}
int multiply(int x,int y){
int mul;
mul=x*y;
System.out.println(x+" * "+y+" = "+mul);
return mul;
}
int subtract(int x,int y){
int sub;
sub=x-y;
System.out.println(x+" - "+ y +" = "+sub);
return sub;
}
int devide(int x,int y){
int div;
div=x/y;
System.out.println(x+" / "+y+" = "+div);
return div;
}
int remainder(int x,int y){
int rem;
rem=x%y;
SASCMA COLLEGE Page No : 1
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
System.out.println(x+" % "+y+" = "+rem);
return rem;
}
}
class program1{
public static void main(String args[])
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.print("Enter The Value of a :");
a=sc.nextInt();
System.out.print("/nEnter The Value of b :");
b=sc.nextInt();
fun func=new fun();
int i;
i=func.sum(a,b);
i=func.subtract(a,b);
i=func.devide(a,b);
i=func.remainder(a,b);
i=func.multiply(a,b);
}//end of main method
SASCMA COLLEGE Page No : 2
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
2 Write a Java program to sort array elements.
Ans ) :
import java.util.Arrays;
class pro2{
public static void main(String args[])
{
int arr[]={5,7,3,2,6,5};
System.out.println("Orignal Array");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
Arrays.sort(arr);
System.out.println("Sorted array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
SASCMA COLLEGE Page No : 3
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
3 Write a program which design Bank Account class as
Saving and Current Account and manage information
accordingly.
Ans ):
abstract class BankAccount {
protected String accountNumber;
protected String accountHolderName;
protected 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);
@Override
public String toString() {
return "Account Number: " + accountNumber +
"\nAccount Holder Name: " + accountHolderName +
SASCMA COLLEGE Page No : 4
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
"\nBalance: " + balance;
}
}
// SavingAccount
class SavingAccount extends BankAccount {
private double interestRate;
public SavingAccount(String accountNumber, String accountHolderName, double
balance, double interestRate) {
super(accountNumber, accountHolderName, balance);
this.interestRate = interestRate;
}
@Override
public void withdraw(double amount) {
if (amount > getBalance()) {
System.out.println("Insufficient balance");
} else {
balance -= amount;
System.out.println("Withdrawn: " + amount);
}
}
public void addInterest() {
double interest = getBalance() * interestRate / 100;
deposit(interest);
}
@Override
public String toString() {
return super.toString() + "\nInterest Rate: " + interestRate + "%";
}
}
// CurrentAccount
class CurrentAccount extends BankAccount {
private double overdraftLimit;
public CurrentAccount(String accountNumber, String accountHolderName, double
SASCMA COLLEGE Page No : 5
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
balance, double overdraftLimit) {
super(accountNumber, accountHolderName, balance);
this.overdraftLimit = overdraftLimit;
}
@Override
public void withdraw(double amount) {
if (amount > getBalance() + overdraftLimit) {
System.out.println("Transaction exceeds overdraft limit");
} else {
balance -= amount;
System.out.println("Withdrawn: " + amount);
}
}
@Override
public String toString() {
return super.toString() + "\nOverdraft Limit: " + overdraftLimit;
}
} // BankAccountManager
public class BankAccountManager {
public static void main(String[] args) {
SavingAccount savingAccount = new SavingAccount("SAV123", "Parth Patel ",
1000, 5);
CurrentAccount currentAccount = new CurrentAccount("CUR456", "Rohan Patel",
500, 2000);
System.out.println("Saving Account:");
System.out.println(savingAccount.toString());
savingAccount.deposit(500);
savingAccount.withdraw(200);
savingAccount.addInterest();
System.out.println("\nCurrent Account:");
System.out.println(currentAccount.toString());
currentAccount.deposit(1000);
currentAccount.withdraw(1500);
}
}
SASCMA COLLEGE Page No : 6
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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 ):
public class program4{
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java Main <arg1> <arg2>");
System.exit(1);
}
String arg1 = args[0];
String arg2 = args[1];
if (isNumber(arg1) && isNumber(arg2)) {
double num1 = Double.parseDouble(arg1);
double num2 = Double.parseDouble(arg2);
System.out.println("Sum: " + (num1 + num2));
} else {
int vowelsCount = countVowels(arg1) + countVowels(arg2);
System.out.println("Total vowels: " + vowelsCount);
}
public static boolean isNumber(String s) {
try {
Double.parseDouble(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static int countVowels(String s) {
int count = 0;
for (char c : s.toLowerCase().toCharArray()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
return count;
}
}
SASCMA COLLEGE Page No : 7
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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;
public Employee(String name, int age, String department) {
this.name = name;
this.age = age;
this.department = department;
}
@Override
public String toString() {
return "Name: " + name + ", Age: " + age + ", Department: " + department;
}
}
public class program5{
public static void main(String[] args) throws InterruptedException {
Employee[] employees = new Employee[5];
employees[0] = new Employee("Parth", 30, "HR");
employees[1] = new Employee("Smith", 25, "Marketing");
employees[2] = new Employee("Rohan", 40, "Sales");
employees[3] = new Employee("Kishan", 28, "IT");
employees[4] = new Employee("mohan", 35, "Finance");
for (int i = 0; i < employees.length; i++) {
System.out.println(employees[i]);
TimeUnit.SECONDS.sleep(1);
}
}
}
SASCMA COLLEGE Page No : 8
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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 ):
import java.util.Arrays;
class InvalidCourseException extends Exception {
public InvalidCourseException(String message) {
super(message);
}
}
public class program6{
public static void main(String[] args) throws InvalidCourseException {
String[] validCourses = {"BCA", "MCA", "BBA", "MBA", "OTHER"};
if (args.length != 1) {
System.out.println("Usage: java Main <course1> <course2> <course3>
<course4> <course5>");
System.exit(1);
}
for (String arg : args) {
if (!Arrays.asList(validCourses).contains(arg.toUpperCase())) {
throw new InvalidCourseException("Invalid course: " + arg);
}
}
System.out.println("your course is valid:");
for (String arg : args) {
System.out.println(arg);
}
}
}
SASCMA COLLEGE Page No : 9
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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 name : studentNames) {
System.out.println(name);
try {
Thread.sleep(1000); // sleep for 1 second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
public class program7{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] studentNames = new String[10];
System.out.println("Enter the names of 10 students:");
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 COLLEGE Page No : 10
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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 program8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string:");
String input = scanner.nextLine();
scanner.close();
char[] chars = input.toCharArray();
int length = chars.length;
// Interchange odd positioned characters with even positioned characters
for (int i = 0; i < length - 1; i += 2) {
char temp = chars[i];
chars[i] = chars[i + 1];
chars[i + 1] = temp;
}
// Reverse the string
String reversed = new StringBuilder(new String(chars)).reverse().toString();
System.out.println("Result:");
System.out.println(reversed);
}
}
SASCMA COLLEGE Page No : 11
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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 MyThread implements Runnable {
private String message;
private int delay;
public MyThread(String message, int delay) {
this.message = message;
this.delay = delay;
}
public void run() {
while (true) {
System.out.println(message);
try {
Thread.sleep(delay * 1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
public class program9 {
public static void main(String[] args) {
MyThread thread1 = new MyThread("JAVA", 1);
MyThread thread2 = new MyThread("PAPER", 2);
MyThread thread3 = new MyThread("COLLEGE", 3);
Thread t1 = new Thread(thread1);
Thread t2 = new Thread(thread2);
Thread t3 = new Thread(thread3);
t1.start();
t2.start();
t3.start();
}
}
SASCMA COLLEGE Page No : 12
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
10 ) Write a program to input two strings search similar
characters from both string and replace it with ‘*’.
Ans ) :
import java.util.Scanner;
public class program10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first string:");
String str1 = scanner.nextLine();
System.out.println("Enter second string:");
String str2 = scanner.nextLine();
scanner.close();
String result1 = replaceSimilarChars(str1, str2);
String result2 = replaceSimilarChars(str2, str1);
System.out.println("Modified first string:");
System.out.println(result1);
System.out.println("Modified second string:");
System.out.println(result2);
}
public static String replaceSimilarChars(String str1, String str2) {
StringBuilder sb = new StringBuilder();
for (char c : str1.toCharArray()) {
if (str2.indexOf(c) != -1) {
sb.append('*');
} else {
sb.append(c);
}
}
return sb.toString();
}
}
SASCMA COLLEGE Page No : 13
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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 program11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("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");
int option = scanner.nextInt();
scanner.close();
switch (option) {
case 1:
extractVowels(input);
break;
case 2:
extractNonVowels(input);
break;
default:
System.out.println("Invalid option");
}
}
public static void extractVowels(String input) {
System.out.println("Vowels:");
for (char c : input.toLowerCase().toCharArray()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
System.out.print(c + " ");
}
}
}
SASCMA COLLEGE Page No : 14
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
public static void extractNonVowels(String input) {
System.out.println("Non-vowels:");
for (char c : input.toLowerCase().toCharArray()) {
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' && c != ' ') {
System.out.print(c + " ");
}
}
}
}
SASCMA COLLEGE Page No : 15
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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;
public class program12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string:");
String input = scanner.nextLine();
while (true) {
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");
System.out.println("4. Exit");
int option = scanner.nextInt();
scanner.nextLine(); // Consume newline left-over
switch (option) {
case 1:
System.out.println("UPPERCASE: " + input.toUpperCase());
break;
case 2:
System.out.println("lowercase: " + input.toLowerCase());
break;
case 3:
System.out.println("Sentence case: " + toSentenceCase(input));
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid option");
}
}
SASCMA COLLEGE Page No : 16
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
}
public static String toSentenceCase(String input) {
if (input.isEmpty()) {
return input;
}
return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
}
}
SASCMA COLLEGE Page No : 17
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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 insertAtLocation(int data, int location) {
Node newNode = new Node(data);
if (location < 0) {
System.out.println("Invalid location");
return;
}
if (location == 0) {
newNode.next = head;
head = newNode;
} else {
Node temp = head;
for (int i = 0; i < location - 1; i++) {
if (temp.next == null) {
System.out.println("Location out of range");
return;
}
SASCMA COLLEGE Page No : 18
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
}
}
public void deleteFromLocation(int location) {
if (location < 0) {
System.out.println("Invalid location");
return;
}
if (location == 0) {
if (head == null) {
System.out.println("List is empty");
} else {
head = head.next;
}
} else {
Node temp = head;
for (int i = 0; i < location - 1; i++) {
if (temp.next == null) {
System.out.println("Location out of range");
return;
}
temp = temp.next;
}
if (temp.next == null) {
System.out.println("Location out of range");
return;
}
temp.next = temp.next.next;
}
}
public void updateAtLocation(int data, int location) {
if (location < 0) {
System.out.println("Invalid location");
return;
}
SASCMA COLLEGE Page No : 19
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
Node temp = head;
for (int i = 0; i < location; i++) {
if (temp == null) {
System.out.println("Location out of range");
return;
}
temp = temp.next;
}
temp.data = data;
}
public boolean search(int data) {
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();
}
}
public class program13{
public static void main(String[] args) {
LinkedList list = new LinkedList();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Linked List Operations:");
System.out.println("1. Insert at location");
SASCMA COLLEGE Page No : 20
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
System.out.println("2. Delete from location");
System.out.println("3. Update at location");
System.out.println("4. Search");
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: ");
int data = scanner.nextInt();
System.out.print("Enter location: ");
int location = scanner.nextInt();
list.insertAtLocation(data, location);
break;
case 2:
System.out.print("Enter location: ");
location = scanner.nextInt();
list.deleteFromLocation(location);
break;
case 3:
System.out.print("Enter data: ");
data = scanner.nextInt();
System.out.print("Enter location: ");
location = scanner.nextInt();
list.updateAtLocation(data, location);
break;
case 4:
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 5:
list.display();
SASCMA COLLEGE Page No : 21
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
SASCMA COLLEGE Page No : 22
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
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;
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
head.next = head;
} else {
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
head = newNode;
}
}
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
head.next = head;
SASCMA COLLEGE Page No : 23
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
} else {
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
}
}
public void deleteFirstNode() {
if (head == null) {
System.out.println("List is empty");
} else if (head.next == head) {
head = null;
} else {
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = head.next;
head = head.next;
}
}
public void deleteLastNode() {
if (head == null) {
System.out.println("List is empty");
} else if (head.next == head) {
head = null;
} else {
Node temp = head;
while (temp.next.next != head) {
temp = temp.next;
}
temp.next = head;
}
}
public void display() {
Node temp = head;
if (head == null) {
System.out.println("List is empty");
} else {
do {
System.out.print(temp.data + " ");
SASCMA COLLEGE Page No : 24
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
temp = temp.next;
} while (temp != head);
System.out.println();
}
}
}
public class program14{
public static void main(String[] args) {
CircularLinkedList list = new CircularLinkedList();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Circular Linked List Operations:");
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: ");
int data = scanner.nextInt();
list.insertAtBeginning(data);
break;
case 2:
System.out.print("Enter data: ");
data = scanner.nextInt();
list.insertAtEnd(data);
break;
case 3:
list.deleteFirstNode();
break;
case 4:
list.deleteLastNode();
break;
case 5:
list.display();
break;
SASCMA COLLEGE Page No : 25
SYBCA Divison : D Vaghasiya Parth RajuBhai
Rollno :2361
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
SASCMA COLLEGE Page No : 26