Java Assignment Final
Java Assignment Final
1. Write a Java Program to Display string with capital letter which are inputted through command
lin. Display those string(s) which starts with �B�
Ans:-
import java.util.Scanner;
class DisplayStringsWithB {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("No strings provided.");
return;
}
for (String str : args) {
String upperCaseStr = str.toUpperCase();
if (upperCaseStr.startsWith("B")) {
System.out.println(upperCaseStr);
}
}
}
}
2. Write an application that creates and start three threads, each thread is instantiated from the
same class. It executes a loop with 5 iterations. First thread display "BEST"', second thread
display "OF" and last thread display "LUCK". All threads sleep for 1000 ms. The application
waits for all threads to complete and display a message.
Ans:-
thread1.start();
thread2.start();
thread3.start();
107-YOGIRAJ KACHHAD 1
Java Programming language
thread1.join();
thread2.join();
thread3.join();
System.out.println("All threads have completed execution.");
}
}
3. Write a Java code that handles the custom exception like when a user gives input as Floating
point number then it raises exception with appropriate message.
Ans:-
import java.util.Scanner;
class FloatingPointException extends Exception {
public FloatingPointException(String message) {
super(message);
}
}
class CustomExceptionExample {
public static void checkForFloatingPoint(String input) throws FloatingPointException {
try {
Double.parseDouble(input);
throw new FloatingPointException("Error: Floating point number detected. Please enter a
whole number.");
} catch (NumberFormatException e) {
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter a number: ");
String userInput = scanner.nextLine();
try {
checkForFloatingPoint(userInput);
System.out.println("You entered a valid number: " + userInput);
} catch (FloatingPointException e) {
System.out.println(e.getMessage());
}
}
}
4. Create class EMPLLOYEE in java with id, name and salary as data members. Create 5 Different
employee objects by taking input from user. Display all the information of an employee which is
having maximum salary.
Ans:-
import java.util.Scanner;
class Employee {
int id;
107-YOGIRAJ KACHHAD 2
Java Programming language
String name;
double salary;
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public void displayInfo() {
System.out.println("Employee ID: " + id);
System.out.println("Employee Name: " + name);
System.out.println("Employee Salary: " + salary);
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Employee[] employees = new Employee[5];
for (int i = 0; i < 5; i++) {
System.out.println("Enter details for Employee " + (i + 1));
5. Write a java program that accept string from command line and display each character in
Capital at delay of one second.
Ans:-
class DisplayCharactersWithDelay {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Please provide a string as a command line argument.");
return;
107-YOGIRAJ KACHHAD 3
Java Programming language
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.err.println("Thread was interrupted");
}
}
System.out.println();
}
}
6. Write a Java program that prompts the user to input the base and height of a triangle.
Accordingly calculates and displays the area of a triangle using the formula (base* height) / 2,
and handles any input errors such as non-numeric inputs or negative values for base or height.
Additionally, include error messages for invalid input and provide the user with the option to
input another set of values or exit the program.
Ans:-
import java.util.Scanner;
class TriangleAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
try {
System.out.print("Enter the base of the triangle: ");
double base = Double.parseDouble(scanner.nextLine());
if (base <= 0) {
System.out.println("Base must be a positive number. Please try again.");
continue;
}
if (height <= 0) {
System.out.println("Height must be a positive number. Please try again.");
continue;
}
107-YOGIRAJ KACHHAD 4
Java Programming language
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter numeric values for base and height.");
}
7. Write a java program that creates Singly Link List to perform create, insert, delete and display
node using menu driven program.
Ans:-
import java.util.Scanner;
class SinglyLinkedList {
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
107-YOGIRAJ KACHHAD 5
Java Programming language
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
class LinkedListMenuDriven {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
SinglyLinkedList list = new SinglyLinkedList();
while (true) {
System.out.println("Menu:");
System.out.println("1. Create");
107-YOGIRAJ KACHHAD 6
Java Programming language
System.out.println("2. Insert");
System.out.println("3. Delete");
System.out.println("4. Display");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter data to create a node: ");
int createData = scanner.nextInt();
list.create(createData);
break;
case 2:
System.out.print("Enter data to insert a node: ");
int insertData = scanner.nextInt();
list.insert(insertData);
break;
case 3:
System.out.print("Enter data to delete a node: ");
int deleteData = scanner.nextInt();
list.delete(deleteData);
break;
case 4:
list.display();
break;
case 5:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
8. Write a java application which accepts two strings. Merge both the strings using alternate
characters of each one.
For example:
If Stringl is: "Very"", and
String2 is: "Good"
Then result should be: "VGeoroyd".
Ans:-
import java.util.Scanner;
class MergeStrings {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first string: ");
107-YOGIRAJ KACHHAD 7
Java Programming language
9.Write a java code that handles the custom exception like when a user gives input as floating
//point number than it raise exception with appropriate message.
Ans:-
import java.util.Scanner;
class CustomExceptionExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
String input = scanner.nextLine();
try {
double number = Double.parseDouble(input);
if (number % 1 != 0) {
throw new FloatingPointException("Error: Floating point number detected.");
}
System.out.println("You entered a valid integer: " + (int) number);
} catch (FloatingPointException e) {
System.out.println(e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input. Please enter a valid number.");
}
}
107-YOGIRAJ KACHHAD 8
Java Programming language
10.Create STUDENT class having data members roll and name. Create 5 objects of STUDENT class.
take input from the user and print all student's data in ascending order of name with interval of 10
ms.
Ans:-
import java.util.Scanner;
class STUDENT {
int roll;
String name;
class StudentSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
STUDENT[] students = new STUDENT[5];
107-YOGIRAJ KACHHAD 9
Java Programming language
}
}
}
11.Write a Java Program that accepts string data. Extract either All Vowels or All Non-Vowels from
given Data According to Options Selection. Also Provide an Option to Display Output in Uppercase
or Lowercase.
Ans:-
import java.util.Scanner;
class ExtractVowelsNonVowels {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose an option:");
System.out.println("1. Extract Vowels");
System.out.println("2. Extract Non-Vowels");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
System.out.println("Choose case:");
System.out.println("1. Uppercase");
System.out.println("2. Lowercase");
int caseChoice = scanner.nextInt();
if (caseChoice == 1) {
System.out.println(result.toString().toUpperCase());
} else {
System.out.println(result.toString().toLowerCase());
}
}
107-YOGIRAJ KACHHAD 10
Java Programming language
12.Write a Java Program that Accepts String Data from User and then Provide options for Changing
case into Any of the Following. (UPPERCASE, lowercise, Sentence case, tOGGLE CASE).
Ans:-
import java.util.Scanner;
class ChangeCase {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose an option:");
System.out.println("1. UPPERCASE");
System.out.println("2. lowercase");
System.out.println("3. Sentence case");
System.out.println("4. TOGGLE CASE");
int choice = scanner.nextInt();
switch (choice) {
case 1:
result = input.toUpperCase();
break;
case 2:
result = input.toLowerCase();
break;
case 3:
result = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
break;
case 4:
StringBuilder toggleCase = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isUpperCase(c)) {
toggleCase.append(Character.toLowerCase(c));
} else {
toggleCase.append(Character.toUpperCase(c));
}
}
result = toggleCase.toString();
break;
default:
System.out.println("Invalid choice");
return;
}
107-YOGIRAJ KACHHAD 11
Java Programming language
}
}
import java.util.*;
class Book {
String title;
String author;
String publication;
double price;
class BookInfo {
public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of books: ");
int n = scanner.nextInt();
scanner.nextLine();
107-YOGIRAJ KACHHAD 12
Java Programming language
14.Write a java application which accepts 10 names of student and their age. Sort names and age
in descending order. Display the names of students using thread class at interval of one second
Ans:-
import java.util.*;
class Student {
String name;
int age;
@Override
public void run() {
try {
System.out.println(name);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
107-YOGIRAJ KACHHAD 13
Java Programming language
class StudentInfo {
public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
List<Student> students = new ArrayList<>();
15.Design Item class having Item_Code, Name, Price, and Stock in hand as data member.Add
appropriate member functions. Write a Java program that accepts details of N Item and display
items details in ascending order of their stock.
Ans:-
import java.util.*;
class Item {
String itemCode;
String name;
double price;
int stock;
107-YOGIRAJ KACHHAD 14
Java Programming language
class ItemInfo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
107-YOGIRAJ KACHHAD 15