0% found this document useful (0 votes)
4 views34 pages

Java Ashish Final

This document is a practical assignment for the Java Programming Language course, detailing various programming tasks to be completed by the student, Ashish Suthar. The tasks include writing Java programs for arithmetic operations, sorting arrays, managing bank account information, handling command line arguments, and implementing multithreading, among others. Each task is accompanied by a brief description and the expected output format.
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)
4 views34 pages

Java Ashish Final

This document is a practical assignment for the Java Programming Language course, detailing various programming tasks to be completed by the student, Ashish Suthar. The tasks include writing Java programs for arithmetic operations, sorting arrays, managing bank account information, handling command line arguments, and implementing multithreading, among others. Each task is accompanied by a brief description and the expected output format.
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

SYBCA-SEM4 2210-Ashish Suthar K

Subject Code: 403


Subject course: “ JAVA PROGRAMMING LANGUAGE”
Name : SUTHAR ASHISH KESHARAM
Class : C Div.:C
Roll no: 2210
Sign of Faculty: _______________

SASCMA BCA COLLEGE, Surat. 1


SYBCA-SEM4 2210-Ashish Suthar K

S.Y.B.C.A. SMESTER 4

PRACTICAL ASSIGNMENT
SUBJECT: 403: JAVA PROGRAMMING LANGUAGE

SR DATE PROGRAMS
NO PAGN
FACULTY
NO SIGN

10/02/26 Write a Java program to print the sum (addition), multiply, subtract, divide and 4
1 remainder of two numbers arithmetic
10/02/26
2 Write a Java program to sort array elements. 7

10/02/26 Write a program which design Bank Account class as Saving and Current Account 8
3 and manage information accordingly.

4 10/02/26 Write a program that accepts two numbers from command line and add two 9
numbers if the arguments are numbers else display total number of vowels of two
strings.

10/02/26 Write a program that stores details of 5 employees and display this information after 11
5 every one second.

6 10/02/26 Write a program to accept 5 command line argument and then raise the custom 13
exception if any argument is not from the list.("BCA","MCA","BBA","MBA"
"OTHER").
7 10/02/26 Write a java code which accepts name of 10 students. Sort the names of students 16
in ascending order. Display the names of students using thread class at interval
of one second.

8 10/02/26 Write java application which accept a string and display the string in reverse order 18
by interchanging its odd positioned characters with even positioned characters.

SASCMA BCA COLLEGE, Surat. 2


SYBCA-SEM4 2210-Ashish Suthar K

9 10/02/26 Write an application that executes three threads from one thread class. One thread 20
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.
10/02/26 Write a program to input two strings search similar characters from both string 22
10 and replace it with ‘*’.

10/02/26 Write a java program that accepts string data. Extract either all vowels or all 26
11 Nonvowels from given data according to option selection.

12 10/02/26 Write a java program that accepts string data from user and then provide options 27
for changing case into any of the following: (UPPERCASE, lowercase, and
Sentence case)

13 10/02/26 Implement a Singly Linked List in java with the following operations: 30
• Insert at the end
• Display all elements

10/02/26 33
Write a Java program that takes two numbers as input and performs division.
14 Handle exception for division by zero and invalid input.

10/02/26 Write a java program that takes five numbers from the command line and prints 35
15 them ascending order.

16 10/02/26 Create a Student class with attributes: name, roll no and marks. implement a 37
method display() to show student details. Create two student objects and display
their details.

07/02/26 Write a java program to Create a thread that prints "Hello from Thread" five times 39
17 using the thread class.

SASCMA BCA COLLEGE, Surat. 3


SYBCA-SEM4 2210-Ashish Suthar K

[Link] a Java program to print the sum (addition), multiply, subtract, divide and
remainder of two numbers arithmetic
Ans-import [Link];
class cal
{

public static void main(String args[])


{
Scanner sc=new Scanner([Link]);

[Link]("enter 1 number:");

int a=[Link]();

[Link]("enter 2 number:");

int b=[Link]();

[Link]("sum of two number:"+(a+b));


[Link]("subtracion of two number:"+(a-b));
[Link]("multiply of two number:"+(a*b));
[Link]("divsion of two number:"+(a/b));
[Link]("remainder of two number:"+(a%b));
}
}

SASCMA BCA COLLEGE, Surat. 4


SYBCA-SEM4 2210-Ashish Suthar K

SASCMA BCA COLLEGE, Surat. 5


SYBCA-SEM4 2210-Ashish Suthar K

[Link] a Java program to sort array elements.


Ans-import [Link];

class cal { public static void


main(String args[]) {
int a[] = {5, 2, 9, 1, 6};

[Link](a);

for (int i : a)
[Link](i + " ");
}
}

[Link] a program which design Bank Account class as Saving and Current
Account and manage information accordingly.
Ans-class Bank {
int acc; double bal;

Bank(int a,double b){ acc=a; bal=b; } void


show(){ [Link](acc+" "+bal); }
}

class Saving extends Bank {


Saving(int a,double b){ super(a,b); bal+=bal*0.04; }
}

SASCMA BCA COLLEGE, Surat. 6


SYBCA-SEM4 2210-Ashish Suthar K

class Current extends Bank {


Current(int a,double b){ super(a,b); bal-=1000; }
}

class cal { public static void


main(String[] args) { new
Saving(101,5000).show(); new
Current(201,8000).show();
}
}

[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-import [Link];

class ScannerDemo {
public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first value: ");


String input1 = [Link]();

[Link]("Enter second value: ");

SASCMA BCA COLLEGE, Surat. 7


SYBCA-SEM4 2210-Ashish Suthar K

String input2 = [Link]();

try {
// Try converting inputs to numbers
int a = [Link](input1); int
b = [Link](input2);

int sum = a + b;
[Link]("Sum = " + sum);

} catch (NumberFormatException e) {
// If inputs are not numbers, count vowels
int count = 0;
String combined = input1 + input2;

for (int i = 0; i < [Link](); i++) { char


ch = [Link]([Link](i));

if (ch == 'a' || ch == 'e' || ch == 'i' ||


ch == 'o' || ch == 'u') { count++;
}
}

[Link]("Total vowels = " + count);


}

[Link]();
}
}

SASCMA BCA COLLEGE, Surat. 8


SYBCA-SEM4 2210-Ashish Suthar K

[Link] a program that stores details of 5 employees and display this


information after every one second.
Ans-class EmployeeDetails { public
static void main(String[] args) {

// Storing employee details


String[] empId = {"E101", "E102", "E103", "E104", "E105"};
String[] empName = {"Amit", "Riya", "Karan", "Neha", "Rahul"};
String[] empDept = {"IT", "HR", "Sales", "Finance", "Support"};

[Link]("Employee Details:\n");

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


[Link]("Employee ID : " + empId[i]);
[Link]("Employee Name : " + empName[i]);
[Link]("Department : " + empDept[i]);
[Link]("------------------------");
try
{
// Pause for 1 second (1000 milliseconds)

SASCMA BCA COLLEGE, Surat. 9


SYBCA-SEM4 2210-Ashish Suthar K

[Link](1000);
} catch (InterruptedException e) {
[Link]("Error in delay");
}
}
}
}

SASCMA BCA COLLEGE, Surat. 10


SYBCA-SEM4 2210-Ashish Suthar K

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];

// Custom Exception class InvalidCourseException


extends Exception { public
InvalidCourseException(String message) {
super(message);
}
}

class CourseCheckScanner {
public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

// Valid course list


String[] validCourses = {"BCA", "MCA", "BBA", "MBA", "OTHER"};

try {
[Link]("Enter 5 course names:");

for (int i = 1; i <= 5; i++) {


[Link]("Course " + i + ": ");
String course = [Link]();

boolean isValid = false;

SASCMA BCA COLLEGE, Surat. 11


SYBCA-SEM4 2210-Ashish Suthar K

for (String v : validCourses) {


if ([Link](v)) {
isValid = true;
break;
}
}

if (!isValid) {
throw new InvalidCourseException(
"Invalid course entered: " + course
);
}
}

[Link]("All entered courses are valid.");

} catch (InvalidCourseException e) {
[Link]([Link]());
}

[Link]();
}
}

SASCMA BCA COLLEGE, Surat. 12


SYBCA-SEM4 2210-Ashish Suthar K

SASCMA BCA COLLEGE, Surat. 13


SYBCA-SEM4 2210-Ashish Suthar K

.[Link] 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 {
public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


String[] students = new String[10];

// Accept 10 student names


[Link]("Enter names of 10 students:");
for (int i = 0; i < 10; i++) {
[Link]("Student " + (i + 1) + ": ");
students[i] = [Link]();
}

// Sort names in ascending order


[Link](students);

[Link]("\nSorted Student Names:\n");

// Display using thread delay


for (int i = 0; i < 10; i++) {
[Link](students[i]);

try {
[Link](1000); // 1 second delay
} catch (InterruptedException e) {

SASCMA BCA COLLEGE, Surat. 14


SYBCA-SEM4 2210-Ashish Suthar K

[Link]("Thread interrupted");
}
}

[Link]();
}
}

[Link] 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];

class OddEvenReverse {
public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

SASCMA BCA COLLEGE, Surat. 15


SYBCA-SEM4 2210-Ashish Suthar K

[Link]("Enter a string: ");


String str = [Link]();

char[] ch = [Link]();

// Interchange odd and even positioned characters


for (int i = 0; i < [Link] - 1; i += 2) { char temp
= ch[i]; ch[i] = ch[i + 1]; ch[i + 1] = temp;
}

// Reverse the string


[Link]("Output: ");
for (int i = [Link] - 1; i >= 0; i--) {
[Link](ch[i]);
}

[Link]();
}
}

SASCMA BCA COLLEGE, Surat. 16


SYBCA-SEM4 2210-Ashish Suthar K

[Link] 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 PrintMessage implements Runnable {

String message;
int delay;

// Constructor
PrintMessage(String message, int delay) {
[Link] = message;
[Link] = delay;
}

// Thread task
public void run() {
try {
while (true) {
[Link](message);
[Link](delay);
}
} catch (InterruptedException e) {
[Link]("Thread interrupted");
}
}
}

public class RunnableDemo {


public static void main(String[] args) {

SASCMA BCA COLLEGE, Surat. 17


SYBCA-SEM4 2210-Ashish Suthar K

// Creating Runnable objects


Runnable r1 = new PrintMessage("JAVA", 1000);
Runnable r2 = new PrintMessage("PAPER", 2000);
Runnable r3 = new PrintMessage("COLLEGE", 3000);

// Creating threads
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
Thread t3 = new Thread(r3);

// Starting threads
[Link]();
[Link](); [Link]();
}
}

[Link] a program to input two strings search similar characters from both
string and replace it with ‘*’
Ans-import [Link];

SASCMA BCA COLLEGE, Surat. 18


SYBCA-SEM4 2210-Ashish Suthar K

class ReplaceCommonChars {
public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first string: ");


String s1 = [Link]();

[Link]("Enter second string: ");


String s2 = [Link]();

char[] a = [Link]();
char[] b = [Link]();

for (int i = 0; i < [Link]; i++) {


for (int j = 0; j < [Link]; j++) {
if (a[i] == b[j] && a[i] != '*') {
a[i] = '*';
b[j] = '*';
}
}
}

[Link]("Modified first string : " + new String(a));


[Link]("Modified second string: " + new String(b));

[Link]();
}

SASCMA BCA COLLEGE, Surat. 19


SYBCA-SEM4 2210-Ashish Suthar K

SASCMA BCA COLLEGE, Surat. 20


SYBCA-SEM4 2210-Ashish Suthar K

[Link] 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];

class VowelExtractor { public static


void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");


String str = [Link]();

[Link]("1. Extract Vowels");


[Link]("2. Extract Non-Vowels");
[Link]("Enter your choice: "); int
choice = [Link]();

String result = "";


str = [Link]();

for (int i = 0; i < [Link](); i++) {


char ch = [Link](i);

boolean isVowel = (ch == 'a' || ch == 'e' || ch == 'i' ||


ch == 'o' || ch == 'u');

if (choice == 1 && isVowel) {


result += ch;
}

SASCMA BCA COLLEGE, Surat. 21


SYBCA-SEM4 2210-Ashish Suthar K

else if (choice == 2 && [Link](ch) && !isVowel) {


result += ch;
}
}

if (choice == 1) {
[Link]("Vowels: " + result);
}
else if (choice == 2) {
[Link]("Non-Vowels: " + result);
}
else {
[Link]("Invalid choice");
}

[Link]();
}
}

SASCMA BCA COLLEGE, Surat. 22


SYBCA-SEM4 2210-Ashish Suthar K

SASCMA BCA COLLEGE, Surat. 23


SYBCA-SEM4 2210-Ashish Suthar K

[Link] 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];

class CaseConverter { public static


void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");


String str = [Link]();

[Link]("1. UPPERCASE");
[Link]("2. lowercase");
[Link]("3. Sentence case");
[Link]("Enter your choice: "); int
choice = [Link]();

switch (choice) {
case 1:
[Link]("Result: " + [Link]());
break;

case 2:
[Link]("Result: " + [Link]());
break;

case 3:

SASCMA BCA COLLEGE, Surat. 24


SYBCA-SEM4 2210-Ashish Suthar K

String sentence = [Link](); if


([Link]() > 0) { sentence =
[Link](0, 1).toUpperCase()
+ [Link](1);
}
[Link]("Result: " + sentence);
break;

default:
[Link]("Invalid choice");
}

[Link]();
}
}

SASCMA BCA COLLEGE, Surat. 25


SYBCA-SEM4 2210-Ashish Suthar K

[Link] a Singly Linked List in java with the following operations:


• Insert at the end
• Display all elements Ans-
class SinglyLinkedList {

// Node class
class Node {
int data;

SASCMA BCA COLLEGE, Surat. 26


SYBCA-SEM4 2210-Ashish Suthar K

Node next;

Node(int data) {
[Link] = data;
[Link] = null;
}
}

Node head = null;

// Insert at the end


void insertEnd(int data) {
Node newNode = new Node(data);

if (head == null) {
head = newNode;
return;
}

Node temp = head;


while ([Link] != null) {
temp = [Link];

}
[Link] = newNode;
}

// Display all elements


void display() { if
(head == null) {

SASCMA BCA COLLEGE, Surat. 27


SYBCA-SEM4 2210-Ashish Suthar K

[Link]("List is empty");
return;
}

Node temp = head;


while (temp != null) {
[Link]([Link] + " -> ");
temp = [Link];
}
[Link]("NULL");
}

// Main method public static void


main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();

[Link](10);
[Link](20);
[Link](30); [Link](40);
[Link]("Singly Linked
List:");

[Link]();
}
}

SASCMA BCA COLLEGE, Surat. 28


SYBCA-SEM4 2210-Ashish Suthar K

[Link] a Java program that takes two numbers as input and performs
division. Handle exception for division by zero and invalid input.
Ans-import [Link];

public class DivisionScanner { public


static void main(String[] args) {

Scanner sc = new Scanner([Link]);

try {
[Link]("Enter first number: ");
double num1 = [Link]();

[Link]("Enter second number: ");


double num2 = [Link]();

double result = num1 / num2;


[Link]("Result = " + result);

}
catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed.");
}
catch ([Link] e) {
[Link]("Error: Please enter valid numbers only.");
}
catch (Exception e) {
[Link]("Error: " + [Link]());
}

SASCMA BCA COLLEGE, Surat. 29


SYBCA-SEM4 2210-Ashish Suthar K

finally {
[Link]();

}
}
}

[Link] a java program that takes five numbers from the command line and
prints them ascending order. Ans-
import [Link];

public class AscendingScanner {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


int[] numbers = new int[5];

[Link]("Enter five numbers:");

SASCMA BCA COLLEGE, Surat. 30


SYBCA-SEM4 2210-Ashish Suthar K

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


numbers[i] = [Link]();
}

// Sorting logic (Bubble Sort)


for (int i = 0; i < 5; i++) { for
(int j = i + 1; j < 5; j++) { if
(numbers[i] > numbers[j]) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}

[Link]("Numbers in ascending order:");


for (int n : numbers) {
[Link](n + " ");
}

[Link]();
}
}

SASCMA BCA COLLEGE, Surat. 31


SYBCA-SEM4 2210-Ashish Suthar K

[Link] a Student class with attributes: name, roll no and marks.


implement a method display() to show student details. Create two student
objects and display their details. Ans-
class Student {

String name;
int rollNo;
int marks;

// Method to display student details


void display() {
[Link]("Name : " + name);
[Link]("Roll No : " + rollNo);
[Link]("Marks : " + marks);
[Link]("--------------------");
}

public static void main(String[] args) {

SASCMA BCA COLLEGE, Surat. 32


SYBCA-SEM4 2210-Ashish Suthar K

// First student object


Student s1 = new Student();
[Link] = "Rahul";
[Link] = 1;
[Link] = 85;

// Second student object


Student s2 = new Student();
[Link] = "Priya";

[Link] = 2;
[Link] = 90;

// Display details
[Link](); [Link]();
}
}

SASCMA BCA COLLEGE, Surat. 33


SYBCA-SEM4 2210-Ashish Suthar K

17. Write a java program to Create a thread that prints "Hello from Thread"
five times using the thread class. Ans- class HelloThread extends Thread {

public void run() {


for (int i = 1; i <= 5; i++) {
[Link]("Hello from Thread");
}
}

public static void main(String[] args) {

HelloThread t = new HelloThread();


[Link](); // start the thread
}
}

SASCMA BCA COLLEGE, Surat. 34

You might also like