java asignmentfinal (1)
java asignmentfinal (1)
Assisgement
Course Code:CSE 332
Course Title: Advanced Programming
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
{ try {
while (scanner.hasNextLine()) {
System.out.println(data);
scanner.close();
} catch (FileNotFoundException e) {
System.err.println("Error: File not found. Please make sure the file exists and is
accessible.");
e.printStackTrace();
Output:
2.
Code:
int indexToAccess =
5; try {
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
Output:
3
Code:
super(message);
}
}
class BankAccount {
if (initialBalance < 0) {
if (amount < 0) {
try {
account.deposit(500);
} catch (NegativeAmountException e) {
}
finally {
System.out.println("Deposit operation completed.");
}
try {
account.withdraw(2000);
System.out.println("Withdrew 2000. Current Balance: " + account.getBalance());
try {
account.deposit(-100);
}
catch (NegativeAmountException e)
{
System.out.println("Deposit failed: " + e.getMessage());
}
finally {
System.out.println("Deposit operation completed.");
}
try {
account.withdraw(300);
Output:
4.
Code:
super(message);
class BankAccount {
this.balance = initialBalance;
if (amount <= 0) {
balance += amount;
balance -= amount;
{ return balance;
{try {
BankAccount(1000); account.deposit(500);
account.withdraw(300);
{ System.err.println(e.getMessage());
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
Output:
5.
Code:
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
);
if (!matcher.matches()) {
validateEmail(email);
} catch (InvalidEmailFormatException e) {
} catch (NullPointerException e) {
}
}
Output:
6.
Code:
deptThread.start();
nameThread.start();
try {
nameThread.join();
} catch (InterruptedException e) {
idThread.start();
} catch (InterruptedException e) {
System.out.println("Dept");
try {
} catch (InterruptedException e) {
{@Override
System.out.println("ID");
Output:
7
Code:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
int col2 =
matrix2[0].length;
ExecutorService executor =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
executor.submit(() -> {
result[row][j] = sum;
});
executor.shutdown();
while (!executor.isTerminated())
{} return result;
System.out.println();
Output:
8.
Code:
{this.number = number;
this.factorial = 1;
factorial = calculateFactorial(number);
{return factorial;
{ long result = 1;
for (int i = 2; i <= n;
i++) {result *= i;
} return result;
{ int n = 6;
oddThread.start();
try {
evenThread.join();
oddThread.join();
} catch (InterruptedException e)
{ e.printStackTrace();
Output:
9.
Code:
@Override
try {
} catch (InterruptedException e) {
@Override
try {
} catch (InterruptedException e) {
}
uppercaseThread.start();
lowercaseThread.start();
try {
uppercaseThread.join();
lowercaseThread.join();
} catch (InterruptedException e) {
Output:
10.
Code:
this.start = start;
this.end = end;
this.sum = 0;
} @Override
{sum += i;
{ return sum;
{if (i == numThreads - 1) {
start = end + 1;
end += segmentSize;
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
long totalSum = 0;
totalSum += thread.getSum();
}
}
Output:
11.
Code:
import java.util.*;
String paragraph = "Write a program that takes a paragraph of text as input and counts the
occurrences of each word. Additionally, identify the five most common words and display them along
with their frequencies.";
HashMap<>();
System.out.println("Word Counts:");
count = 0;
break;
count++;
Output:
12.
Code:
import java.util.Scanner;
if (isSubstringPresent) {
} else {
} scanner.close();
output:
13.
Code:
import java.util.Scanner;
if (!word.isEmpty()) {
capitalizedSentence.append(capitalizedWord).append(" ");
Output:
14.
Code:
reversedSentence.append(words[i]); if
(i > 0) {
reversedSentence.append(" ");
return reversedSentence.toString();
world";
}
Output:
15.
Code:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
if (charCountMap.containsKey(c)) {
System.out.println("Character counts:");
scanner.close();
}
}
Output:
16.
Code:
import java.util.Scanner;
scanner.close();
}
Output:
17.
Code:
B.substring(14);
Ouput:
18.
Code
import java.util.LinkedList;
import java.util.Queue;
class TicketBookingSystem
public TicketBookingSystem() {
ticketRequests.offer(customerName);
System.out.println("\nProcessing bookings:");
while (!ticketRequests.isEmpty()) {
System.out.println("Pending bookings:");
System.out.println(customerName);
TicketBookingSystem();
bookingSystem.addBookingRequest("Alice");
bookingSystem.addBookingRequest("Bob");
bookingSystem.addBookingRequest("Charlie");
bookingSystem.processBookings();
bookingSystem.displayBookingStatus();
}
Output:
19.
Code:
import
java.util.ArrayList; class
Car {
{this.price = price;
this.brand = brand;
this.speed = speed;
{return price;
{return speed; }
210));
Output:
20.
Code:
import java.util.Arrays;
studentIDs = new
double[capacity];
if (studentIDs[i] == null) {
studentIDs[i] =
studentID; grades[i] =
grade;
return;
grades[i] =
0.0; return;
System.out.println("Student IDs:");
if (id != null) {
System.out.println(id);
System.out.println("Grades:");
{ if (studentIDs[i] != null) {
gradebook.addStudent("1001", 85.5);
gradebook.addStudent("1002", 90.0);
gradebook.addStudent("1003", 75.0);
gradebook.displayStudentIDs();
gradebook.displayGrades();
gradebook.removeStudent("1002");
gradebook.displayStudentIDs();
gradebook.displayGrades();
Output:
21.
Code:
import java.util.Scanner;
import java.util.Stack;
class Student {
{ this.name = name;
this.id = id;
{ return name;
{return id;
}@Override
i++) {
int menu;
do {
System.out.println("\nMenu:");
System.out.println("4. Exit");
menu = scanner.nextInt();
switch (menu) {
case 1:
int id = scanner.nextInt();
break;
case 2:
if (!studentStack.isEmpty()) {
} else {
break;
case 3:
if (!studentStack.isEmpty()) {
} else {
break;
case 4:
break;
default:
} while (menu !=
4);scanner.close();
Output:
22.
Code:
import
java.util.ArrayList; import
java.util.List;
ArrayList<>(); stringList.add("apple");
stringList.add("banana");
stringList.add("apple");
stringList.add("orange");
stringList.add("banana");
stringList.add("grape");
stringList.add("banana");
System.out.println(stringList);
removeDuplicates(stringList);
System.out.println(stringList);
{ if
(!uniqueList.contains(str)) {
uniqueList.add(str);
stringList.clear();
stringList.addAll(uniqueList);
Output: