SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
1 .Write a Java program to print the sum (addition), multiply,
subtract, divide and remainder of two numbers arithmetic
Ans ):
import [Link];
class fun{
int sum(int x,int y){
int s;
s=x+y;
[Link](x+" + "+y+" = "+s);
return s;
}
int multiply(int x,int y){
int mul;
mul=x*y;
[Link](x+" * "+y+" = "+mul);
return mul;
}
int subtract(int x,int y){
int sub;
sub=x-y;
[Link](x+" - "+ y +" = "+sub);
return sub;
}
int devide(int x,int y){
int div;
div=x/y;
[Link](x+" / "+y+" = "+div);
return div;
}
int remainder(int x,int y){
int rem;
rem=x%y;
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
[Link](x+" % "+y+" = "+rem);
return rem;
}
}
class program1{
public static void main(String args[])
{
int a,b;
Scanner sc=new Scanner([Link]);
[Link]("Enter The Value of a :");
a=[Link]();
[Link]("/nEnter The Value of b :");
b=[Link]();
fun func=new fun();
int i;
i=[Link](a,b);
i=[Link](a,b);
i=[Link](a,b);
i=[Link](a,b);
i=[Link](a,b);
}//end of main method
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
2 Write a Java program to sort array elements.
Ans ) :
import [Link];
class pro2{
public static void main(String args[])
{
int arr[]={5,7,3,2,6,5};
[Link]("Orignal Array");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
[Link]();
[Link](arr);
[Link]("Sorted array:");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
[Link]();
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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) {
[Link] = accountNumber;
[Link] = accountHolderName;
[Link] = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountHolderName() {
return accountHolderName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
[Link]("Deposited: " + amount);
}
public abstract void withdraw(double amount);
@Override
public String toString() {
return "Account Number: " + accountNumber +
"\nAccount Holder Name: " + accountHolderName +
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
"\nBalance: " + balance;
}
}
// SavingAccount
class SavingAccount extends BankAccount {
private double interestRate;
public SavingAccount(String accountNumber, String accountHolderName, double
balance, double interestRate) {
super(accountNumber, accountHolderName, balance);
[Link] = interestRate;
}
@Override
public void withdraw(double amount) {
if (amount > getBalance()) {
[Link]("Insufficient balance");
} else {
balance -= amount;
[Link]("Withdrawn: " + amount);
}
}
public void addInterest() {
double interest = getBalance() * interestRate / 100;
deposit(interest);
}
@Override
public String toString() {
return [Link]() + "\nInterest Rate: " + interestRate + "%";
}
}
// CurrentAccount
class CurrentAccount extends BankAccount {
private double overdraftLimit;
public CurrentAccount(String accountNumber, String accountHolderName, double
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
balance, double overdraftLimit) {
super(accountNumber, accountHolderName, balance);
[Link] = overdraftLimit;
}
@Override
public void withdraw(double amount) {
if (amount > getBalance() + overdraftLimit) {
[Link]("Transaction exceeds overdraft limit");
} else {
balance -= amount;
[Link]("Withdrawn: " + amount);
}
}
@Override
public String toString() {
return [Link]() + "\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);
[Link]("Saving Account:");
[Link]([Link]());
[Link](500);
[Link](200);
[Link]();
[Link]("\nCurrent Account:");
[Link]([Link]());
[Link](1000);
[Link](1500);
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
[Link] 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 ([Link] != 2) {
[Link]("Usage: java Main <arg1> <arg2>");
[Link](1);
}
String arg1 = args[0];
String arg2 = args[1];
if (isNumber(arg1) && isNumber(arg2)) {
double num1 = [Link](arg1);
double num2 = [Link](arg2);
[Link]("Sum: " + (num1 + num2));
} else {
int vowelsCount = countVowels(arg1) + countVowels(arg2);
[Link]("Total vowels: " + vowelsCount);
}
public static boolean isNumber(String s) {
try {
[Link](s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static int countVowels(String s) {
int count = 0;
for (char c : [Link]().toCharArray()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
return count;
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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) {
[Link] = name;
[Link] = age;
[Link] = 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 < [Link]; i++) {
[Link](employees[i]);
[Link](1);
}
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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 [Link];
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 ([Link] != 1) {
[Link]("Usage: java Main <course1> <course2> <course3>
<course4> <course5>");
[Link](1);
}
for (String arg : args) {
if (.contains([Link]())) {
throw new InvalidCourseException("Invalid course: " + arg);
}
}
[Link]("your course is valid:");
for (String arg : args) {
[Link](arg);
}
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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 [Link];
import [Link];
class StudentThread extends Thread {
String[] studentNames;
public StudentThread(String[] studentNames) {
[Link] = studentNames;
}
public void run() {
for (String name : studentNames) {
[Link](name);
try {
[Link](1000); // sleep for 1 second
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
}
}
public class program7{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String[] studentNames = new String[10];
[Link]("Enter the names of 10 students:");
for (int i = 0; i < 10; i++) {
studentNames[i] = [Link]();
}
[Link](studentNames);
[Link]("Sorted student names:");
StudentThread thread = new StudentThread(studentNames);
[Link]();
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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 [Link];
public class program8 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string:");
String input = [Link]();
[Link]();
char[] chars = [Link]();
int length = [Link];
// 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();
[Link]("Result:");
[Link](reversed);
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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) {
[Link] = message;
[Link] = delay;
}
public void run() {
while (true) {
[Link](message);
try {
[Link](delay * 1000);
} catch (InterruptedException e) {
[Link]().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);
[Link]();
[Link]();
[Link]();
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
10 ) Write a program to input two strings search similar
characters from both string and replace it with ‘*’.
Ans ) :
import [Link];
public class program10 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter first string:");
String str1 = [Link]();
[Link]("Enter second string:");
String str2 = [Link]();
[Link]();
String result1 = replaceSimilarChars(str1, str2);
String result2 = replaceSimilarChars(str2, str1);
[Link]("Modified first string:");
[Link](result1);
[Link]("Modified second string:");
[Link](result2);
}
public static String replaceSimilarChars(String str1, String str2) {
StringBuilder sb = new StringBuilder();
for (char c : [Link]()) {
if ([Link](c) != -1) {
[Link]('*');
} else {
[Link](c);
}
}
return [Link]();
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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 [Link];
public class program11 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string:");
String input = [Link]();
[Link]("Select an option:");
[Link]("1. Extract vowels");
[Link]("2. Extract non-vowels");
int option = [Link]();
[Link]();
switch (option) {
case 1:
extractVowels(input);
break;
case 2:
extractNonVowels(input);
break;
default:
[Link]("Invalid option");
}
}
public static void extractVowels(String input) {
[Link]("Vowels:");
for (char c : [Link]().toCharArray()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
[Link](c + " ");
}
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
public static void extractNonVowels(String input) {
[Link]("Non-vowels:");
for (char c : [Link]().toCharArray()) {
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' && c != ' ') {
[Link](c + " ");
}
}
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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 [Link];
public class program12 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string:");
String input = [Link]();
while (true) {
[Link]("Select an option:");
[Link]("1. Convert to UPPERCASE");
[Link]("2. Convert to lowercase");
[Link]("3. Convert to Sentence case");
[Link]("4. Exit");
int option = [Link]();
[Link](); // Consume newline left-over
switch (option) {
case 1:
[Link]("UPPERCASE: " + [Link]());
break;
case 2:
[Link]("lowercase: " + [Link]());
break;
case 3:
[Link]("Sentence case: " + toSentenceCase(input));
break;
case 4:
[Link](0);
break;
default:
[Link]("Invalid option");
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
}
public static String toSentenceCase(String input) {
if ([Link]()) {
return input;
}
return [Link](0, 1).toUpperCase() + [Link](1).toLowerCase();
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
13 Write a program to create singly Link List and perform
following operations:
[Link] a node at desired Location.
[Link] a node from given Location.
[Link] desired Location.
[Link]
[Link]
Ans ):
import [Link];
class Node {
int data;
Node next;
public Node(int data) {
[Link] = data;
[Link] = null;
}
}
class LinkedList {
Node head;
public void insertAtLocation(int data, int location) {
Node newNode = new Node(data);
if (location < 0) {
[Link]("Invalid location");
return;
}
if (location == 0) {
[Link] = head;
head = newNode;
} else {
Node temp = head;
for (int i = 0; i < location - 1; i++) {
if ([Link] == null) {
[Link]("Location out of range");
return;
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
temp = [Link];
}
[Link] = [Link];
[Link] = newNode;
}
}
public void deleteFromLocation(int location) {
if (location < 0) {
[Link]("Invalid location");
return;
}
if (location == 0) {
if (head == null) {
[Link]("List is empty");
} else {
head = [Link];
}
} else {
Node temp = head;
for (int i = 0; i < location - 1; i++) {
if ([Link] == null) {
[Link]("Location out of range");
return;
}
temp = [Link];
}
if ([Link] == null) {
[Link]("Location out of range");
return;
}
[Link] = [Link];
}
}
public void updateAtLocation(int data, int location) {
if (location < 0) {
[Link]("Invalid location");
return;
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
Node temp = head;
for (int i = 0; i < location; i++) {
if (temp == null) {
[Link]("Location out of range");
return;
}
temp = [Link];
}
[Link] = data;
}
public boolean search(int data) {
Node temp = head;
while (temp != null) {
if ([Link] == data) {
return true;
}
temp = [Link];
}
return false;
}
public void display() {
Node temp = head;
while (temp != null) {
[Link]([Link] + " ");
temp = [Link];
}
[Link]();
}
}
public class program13{
public static void main(String[] args) {
LinkedList list = new LinkedList();
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("Linked List Operations:");
[Link]("1. Insert at location");
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
[Link]("2. Delete from location");
[Link]("3. Update at location");
[Link]("4. Search");
[Link]("5. Display");
[Link]("6. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter data: ");
int data = [Link]();
[Link]("Enter location: ");
int location = [Link]();
[Link](data, location);
break;
case 2:
[Link]("Enter location: ");
location = [Link]();
[Link](location);
break;
case 3:
[Link]("Enter data: ");
data = [Link]();
[Link]("Enter location: ");
location = [Link]();
[Link](data, location);
break;
case 4:
[Link]("Enter data to search: ");
data = [Link]();
if ([Link](data)) {
[Link]("Data found");
} else {
[Link]("Data not found");
}
break;
case 5:
[Link]();
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
break;
case 6:
[Link](0);
break;
default:
[Link]("Invalid choice");
}
}
}
}
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
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 [Link];
class Node {
int data;
Node next;
public Node(int data) {
[Link] = data;
[Link] = null;
}
}
class CircularLinkedList {
Node head;
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
[Link] = head;
} else {
Node temp = head;
while ([Link] != head) {
temp = [Link];
}
[Link] = newNode;
[Link] = head;
head = newNode;
}
}
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
[Link] = head;
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
} else {
Node temp = head;
while ([Link] != head) {
temp = [Link];
}
[Link] = newNode;
[Link] = head;
}
}
public void deleteFirstNode() {
if (head == null) {
[Link]("List is empty");
} else if ([Link] == head) {
head = null;
} else {
Node temp = head;
while ([Link] != head) {
temp = [Link];
}
[Link] = [Link];
head = [Link];
}
}
public void deleteLastNode() {
if (head == null) {
[Link]("List is empty");
} else if ([Link] == head) {
head = null;
} else {
Node temp = head;
while ([Link] != head) {
temp = [Link];
}
[Link] = head;
}
}
public void display() {
Node temp = head;
if (head == null) {
[Link]("List is empty");
} else {
do {
[Link]([Link] + " ");
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
temp = [Link];
} while (temp != head);
[Link]();
}
}
}
public class program14{
public static void main(String[] args) {
CircularLinkedList list = new CircularLinkedList();
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("Circular Linked List Operations:");
[Link]("1. Insert at beginning");
[Link]("2. Insert at end");
[Link]("3. Delete first node");
[Link]("4. Delete last node");
[Link]("5. Display");
[Link]("6. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter data: ");
int data = [Link]();
[Link](data);
break;
case 2:
[Link]("Enter data: ");
data = [Link]();
[Link](data);
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
case 5:
[Link]();
break;
SASCMA COLLEGE
SYBCA Divison : D Parmar Divy Ashok bhai
Rollno :2332
case 6:
[Link](0);
break;
default:
[Link]("Invalid choice");
}
}
}
}
SASCMA COLLEGE