JAVA Assignment
JAVA Assignment
1) Write a program to cover all Java OOPS concepts. Topics need to cover:
Code:
// b) Class constructor
public Vehicle(String model, int year) {
this.model = model;
this.year = year;
}
// f) Inheritance
class Car extends Vehicle {
String brand;
// b) Class constructor
public Car(String model, int year, String brand) {
super(model, year);
this.brand = brand;
}
// e) Method overriding
@Override
public void start() {
System.out.println("Car is starting");
}
// d) Method overloading
public void start(String sound) {
System.out.println(sound);
}
}
// g) Interface
interface Electric {
void charge();
}
// h) Abstract class
abstract class HeavyVehicle {
abstract void loadCargo();
}
// c) Polymorphism
class Truck extends Vehicle implements Electric {
public Truck(String model, int year) {
super(model, year);
}
// e) Method overriding
@Override
public void start() {
System.out.println("Truck is starting");
}
// g) Interface implementation
@Override
public void charge() {
System.out.println("Truck is charging");
}
}
@Override
public void turnOn() {
System.out.println("Washing Machine is turning on");
}
}
// c) Polymorphism
Vehicle myVehicle = truck;
myVehicle.start();
// f) Inheritance
car.start();
car.start("Vroom");
// g) Interface
Electric myTruck = truck;
myTruck.charge();
Output:
2) Design a Java program that performs various string operations and uses control
statements for user input validation. The program should allow the user to perform
the following operations:
a) Concatenate Strings: The user can enter two strings and the program
should concatenate them.
b) Find Length of a String: The user can enter a string, and the program
should display its length.
c) Convert to Uppercase and Lowercase: The user can enter a string, and the
program should display it in both uppercase and lowercase.
d) Extract Substring: The user can enter a string and specify the starting and
ending index, and the program should extract and display the substring.
e) Split a Sentence: The user can enter a sentence, and the program should
split it into words and display them.
f) Reverse a String: The user can enter a string, and the program should
reverse and display it.
g) Requirements:
i) Use control statements (if-else, switch, loops) for input validation
and handling possible errors.
ii) Implement a user-friendly console interface for the user to interact
with the program.
iii) Cover all string concepts, such as concatenation, length, uppercase
and lowercase conversion, substring extraction, splitting, and
reversal.
Code:
import java.util.Scanner;
while (true) {
System.out.println("Select an operation:");
System.out.println("a) Concatenate Strings");
System.out.println("b) Find Length of a String");
System.out.println("c) Convert to Uppercase and Lowercase");
System.out.println("d) Extract Substring");
System.out.println("e) Split a Sentence");
System.out.println("f) Reverse a String");
System.out.println("g) Exit");
switch (choice) {
case "a":
concatenateStrings(scanner);
break;
case "b":
findStringLength(scanner);
break;
case "c":
convertCase(scanner);
break;
case "d":
extractSubstring(scanner);
break;
case "e":
splitSentence(scanner);
break;
case "f":
reverseString(scanner);
break;
case "g":
System.out.println("Exiting program...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please enter a
valid option (a-g).");
}
Code:
import java.io.*;
import java.nio.file.*;
import java.util.Scanner;
while (true) {
System.out.println("Select a file operation:");
System.out.println("a) Create a new directory");
System.out.println("b) Create a new text file and write con-
tent to it");
System.out.println("c) Read the content from an existing text
file");
System.out.println("d) Append new content to an existing text
file");
System.out.println("e) Copy the content from one text file to
another");
System.out.println("f) Delete a text file");
System.out.println("g) List all files and directories in a
given directory");
System.out.println("h) Search for a specific file in a dir-
ectory and its subdirectories");
System.out.println("i) Rename a file");
System.out.println("j) Get information about a file");
System.out.println("k) Exit");
switch (choice) {
case "a":
createDirectory(scanner);
break;
case "b":
createFileAndWrite(scanner);
break;
case "c":
readFile(scanner);
break;
case "d":
appendToFile(scanner);
break;
case "e":
copyFile(scanner);
break;
case "f":
deleteFile(scanner);
break;
case "g":
listFilesAndDirectories(scanner);
break;
case "h":
searchFile(scanner);
break;
case "i":
renameFile(scanner);
break;
case "j":
getFileInformation(scanner);
break;
case "k":
System.out.println("Exiting program...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please enter a
valid option (a-k).");
}
try {
Files.copy(Paths.get(sourcePath), Paths.get(destPath), Stand-
ardCopyOption.REPLACE_EXISTING);
System.out.println("File copied successfully.");
} catch (IOException e) {
System.out.println("An error occurred while copying the file:
" + e.getMessage());
}
}
// i) Rename a file
private static void renameFile(Scanner scanner) {
System.out.print("Enter the current file path: ");
String currentPath = scanner.nextLine();
if (oldFile.renameTo(newFile)) {
System.out.println("File renamed successfully.");
} else {
System.out.println("Failed to rename file.");
}
}
Output:
4) Design a Java program that covers all thread-related topics, demonstrating various
multithreading concepts in Java. The program should allow users to perform the
following tasks:
a) Lists:
i) Add an element: The user can add an element to the list.
ii) Remove an element: The user can remove an element from the list.
iii) Display all elements: The user can view all elements in the list.
b) Sets:
i) Add an element: The user can add an element to the set.
ii) Remove an element: The user can remove an element from the set.
iii) Display all elements: The user can view all elements in the set.
c) Maps:
i) Add a key-value pair: The user can add a key-value pair to the map.
ii) Remove a key-value pair: The user can remove a key-value pair from
the map.
iii) Display all key-value pairs: The user can view all key-value pairs in
the map.
d) Requirements:
i) Implement separate classes for each type of collection
(ListManager, SetManager, MapManager).
ii) Use appropriate collection classes (e.g., ArrayList, LinkedList,
HashSet, TreeMap) to store the elements and key-value pairs.
iii) Use inheritance and polymorphism to manage different types of
collections.
iv) Implement exception handling to handle possible errors (e.g.,
element not found in the list/set, duplicate keys in the map).
v) Provide a user-friendly console interface for the user to interact with
the Collection Management System.
e)Cover all Java collections topics, including Lists, Sets, and Maps
CollectionManagementSystem.java:
import java.util.*;
switch (choice) {
case 1:
manageList(scanner, listManager);
break;
case 2:
manageSet(scanner, setManager);
break;
case 3:
manageMap(scanner, mapManager);
break;
case 4:
System.out.println("Exiting program...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please
enter a valid option (1-4).");
}
}
}
switch (choice) {
case 1:
System.out.print("Enter element to add: ");
String elementToAdd = scanner.nextLine();
listManager.addElement(elementToAdd);
break;
case 2:
System.out.print("Enter element to remove:
");
String elementToRemove = scanner.nextLine();
listManager.removeElement(elementToRemove);
break;
case 3:
listManager.displayElements();
break;
case 4:
return;
default:
System.out.println("Invalid choice. Please
enter a valid option (1-4).");
}
}
}
switch (choice) {
case 1:
System.out.print("Enter element to add: ");
String elementToAdd = scanner.nextLine();
setManager.addElement(elementToAdd);
break;
case 2:
System.out.print("Enter element to remove:
");
String elementToRemove = scanner.nextLine();
setManager.removeElement(elementToRemove);
break;
case 3:
setManager.displayElements();
break;
case 4:
return;
default:
System.out.println("Invalid choice. Please
enter a valid option (1-4).");
}
}
}
switch (choice) {
case 1:
System.out.print("Enter key: ");
String key = scanner.nextLine();
System.out.print("Enter value: ");
String value = scanner.nextLine();
mapManager.addEntry(key, value);
break;
case 2:
System.out.print("Enter key to remove: ");
String keyToRemove = scanner.nextLine();
mapManager.removeEntry(keyToRemove);
break;
case 3:
mapManager.displayEntries();
break;
case 4:
return;
default:
System.out.println("Invalid choice. Please
enter a valid option (1-4).");
}
}
}
}
ListManager.java
package Task5;
import java.util.ArrayList;
import java.util.List;
public ListManager() {
list = new ArrayList<>();
}
public void addElement(String element) {
list.add(element);
System.out.println("Element added to the list.");
}
SetManager.java
package Task5;
import java.util.HashSet;
import java.util.Set;
public SetManager() {
set = new HashSet<>();
}
MapManager.java
package Task5;
import java.util.HashMap;
import java.util.Map;
public MapManager() {
map = new HashMap<>();
}
public void addEntry(String key, String value) {
if (map.containsKey(key)) {
System.out.println("Key already exists in the
map.");
} else {
map.put(key, value);
System.out.println("Key-value pair added to the
map.");
}
}
Code:
Employee.java:
class Employee {
private String employeeID;
private String name;
private String department;
private double salary;
@Override
public String toString() {
return "Employee ID: " + employeeID + ", Name: " + name + ", De-
partment: " + department + ", Salary: " + salary;
}
}
EmployeeManagementSystem.java:
import java.util.*;
while (true) {
System.out.println("\nEmployee Management System");
System.out.println("1) Add new employee");
System.out.println("2) Update employee details");
System.out.println("3) Delete an employee");
System.out.println("4) Display all employees");
System.out.println("5) Search for an employee");
System.out.println("6) Exit");
switch (choice) {
case 1:
addEmployee(scanner);
break;
case 2:
updateEmployee(scanner);
break;
case 3:
deleteEmployee(scanner);
break;
case 4:
displayAllEmployees();
break;
case 5:
searchEmployee(scanner);
break;
case 6:
System.out.println("Exiting program...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please enter a
valid option (1-6).");
}
}
}
if (employees.containsKey(employeeID)) {
System.out.println("Employee ID already exists. Please use a
unique ID.");
return;
}
Output: