java practical assignment(krishna)
java practical assignment(krishna)
ROLL NO :- 2231
DIVISION :- C
CLASS :- S.Y.B.C.A
1. Write a Java program to print the sum (addition), multiply, subtract, divide
and remainder of two numbers arithmetic
import java.util.Scanner;
num1=scanner.nextDouble();
double sum=num1+num2;
double difference=num1-num2;
double product=num1*num2;
double quotient=num1/num2;
double remainder=num1%num2;
System.out.println("Product:" + product );
scanner.close();
import java.util.Arrays;
int[] numbers={34,12,5,87,56,9};
Arrays.sort(numbers);
3. Write a program which design Bank Account class as Saving and Current
Account and manage information accordingly.
SASCMA BCA COLLAGE,SURAT
Page | 6
Roll no :- 2231
double balance;
this.balance = balance;
deposit(double amount) { if
amount;
} else {
-= amount;
} else {
{ return balance;
private double
interestRate;
interestRate;
interest;
// Account information
accountInfo() {
overdraftLimit;
{ balance -= amount;
} else {
// Account information
accountInfo() {
savingAccount.applyInterest(); savingAccount.accountInfo();
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.
if (args.length != 2) {
return;
try {
} catch (NumberFormatException e) {
int count = 0;
(vowels.indexOf(str.charAt(i)) != -1)
{ count++;
return count;
import java.util.Timer;
import java.util.TimerTask;
class Employee
{ int id;
String name;
String department;
String department) {
name; this.department =
department;
System.out.println("ID: " + id + ", Name: " + name + ", Department: " + department);
new
timer.scheduleAtFixedRate(new TimerTask()
int currentEmployee = 0;
@Override
employees[currentEmployee].display();
currentEmployee++;
} else {
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”).
passed if (args.length != 5) {
return;
try
(!isValidArgument(arg, validArguments))
} catch (InvalidArgumentException e)
if (validArg.equals(arg))
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.
java.util.Arrays;
this.names = names;
@Override
try
System.out.println(name);
} catch (InterruptedException e)
e.printStackTrace();
studentNames[i] = scanner.nextLine();
Arrays.sort(studentNames);
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.
import java.util.Scanner;
// Loop through the string, swapping the odd and even indexed characters
+ 1] = temp;
return reversedString;
scanner.close();
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.
@Override
while (true)
try {
System.out.println("JAVA");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
};
@Override
while (true)
try {
System.out.println("PAPER");
} catch (InterruptedException e)
Thread.currentThread().interrupt();
};
@Override
while (true)
try
System.out.println("COLLEGE");
} catch (InterruptedException e)
Thread.currentThread().interrupt();
};
javaThread.start();
paperThread.start();
collegeThread.start();
10. Write a program to input two strings search similar characters from both
string and replace it with ‘*’.
import java.util.Scanner;
str2.toCharArray();
// Convert the arrays back to strings and return the modified strings
System.out.println("Modified strings:");
System.out.println(result);
scanner.close();
11. Write a java program that accepts string data. Extract either all vowels or
all Non-vowels from given data according to option selection.
import java.util.Scanner;
char ch = data.charAt(i);
if (Character.isAlphabetic(ch))
return result.toString();
System.out.println("Select an
option:"); System.out.println("1.
Extract vowels");
option
= scanner.nextInt();
SASCMA BCA COLLAGE,SURAT
Page | 33
Roll no :- 2231
if (option == 1)
} else if (option == 2)
nonVowels);
} else {
scanner.close();
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)
import java.util.Scanner;
return input.toUpperCase();
return input.toLowerCase();
// Trim the input and convert the first letter to uppercase, others to
= true;
result.append(Character.toUpperCase(ch));
capitalizeNext = false;
} else {
result.append(Character.toLowerCase(ch));
if (ch == '.') {
capitalizeNext = true;
return result.toString();
System.out.println("1. UPPERCASE");
System.out.println("2. lowercase");
option = scanner.nextInt();
switch (option)
{ case 1:
result = toUpperCase(inputString);
break;
case 2:
result = toLowerCase(inputString);
break;
case 3:
result = toSentenceCase(inputString);
break;
default:
break;
if (!result.isEmpty())
scanner.close();
13. Write a program to create singly Link List and perform following operations:
SASCMA BCA COLLAGE,SURAT
Page | 39
Roll no :- 2231
4. Search
5. Display
import java.util.Scanner;
class SinglyLinkedList
Node next;
this.data = data;
this.next = null;
public SinglyLinkedList()
{ head =
null;
return;
current = current.next;
if (current == null) {
return;
newNode.next = current.next;
current.next = newNode;
if (head == null) {
System.out.println("List is empty.");
return;
current = current.next;
return;
current.next = current.next.next;
current = current.next;
if (current == null) {
return;
current.data = data;
(current != null)
if (current.data == value)
System.out.println("Node with value " + value + " found at position " + position);
return;
current = current.next;
position++;
if (head == null)
System.out.println("The list is
empty."); return;
current = current.next;
System.out.println("null");
while (true) {
System.out.println("\nSelect an operation:");
System.out.println("6. Exit");
choice = scanner.nextInt();
switch (choice)
{ case 1:
list.insertAtLocation(dataInsert, positionInsert);
break;
case 2:
list.deleteAtLocation(positionDelete);
break;
case 3:
positionUpdate = scanner.nextInt();
list.updateAtLocation(dataUpdate, positionUpdate);
break;
case 4:
list.search(valueSearch); break;
case 6:
System.out.println("Exiting program...");
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.
class SinglyCircularLinkedList
// Node class
int data;
Node next;
this.next = null;
insertAtBeginning(int
data)
Circular link
} else {
temp = temp.next;
new node
Circular link
} else {
temp = temp.next;
temp.next = newNode;
newNode.next = head;
if (head == null) {
System.out.println("List is empty.");
return;
head = null;
} else {
{ temp = temp.next;
head = head.next;
temp.next = head;
if (head == null) {
System.out.println("List is empty.");
return;
head = null;
} else {
{ temp = temp.next;
{ temp2 = temp2.next;
if (head == null) {
System.out.println("List is empty.");
return;
do {
temp = temp.next;
System.out.println();
// Inserting nodes
list.insertAtBeginning(10);
list.insertAtEnd(20);
list.insertAtBeginning(5);
list.insertAtEnd(30);
// Display list
"); list.display();
// Deleting nodes
list.deleteFirstNode();
list.deleteLastNode();
");
list.display();