JavaForBeginner-Assignment
JavaForBeginner-Assignment
ques 1:
import java.util.Scanner;
import java.util.InputMismatchException;
System.out.println("Average Grades:");
for (int i = 0; i < numStudents; i++) {
double sum = 0;
// Corrected format specifiers for printing the highest and lowest grades
System.out.printf("Highest Grade: %.0f (%s)\n", highestGrade,
highestGradeStudent);
System.out.printf("Lowest Grade: %.0f (%s)\n", lowestGrade,
lowestGradeStudent);
scanner.close();
}
}
ques 2:
import java.util.Scanner;
public class Main {
private static final int REGULAR_HOURS = 160;
private static final double OVERTIME_MULTIPLIER = 1.5;
ques 1:
import java.util.Scanner;
class Car {
String type;
// Constructor
public Car(String type) {
this.type = type;
}
switch (type.toLowerCase()) {
case "standard":
dailyRate = 2000;
break;
case "suv":
dailyRate = 4000;
break;
default:
dailyRate = 2000;
break;
}
scanner.close();
}
}
ques 2:
import java.util.Scanner;
class BankAccount {
String accountHolderName;
int accountNumber;
double balance;
// Constructor
public BankAccount(String accountHolderName, int accountNumber, double
initialDeposit) {
this.accountHolderName = accountHolderName;
this.accountNumber = accountNumber;
this.balance = initialDeposit;
}
// Constructor
public CurrentAccount(String accountHolderName, int accountNumber, double
initialDeposit, double overdraftLimit) {
super(accountHolderName, accountNumber, initialDeposit);
this.overdraftLimit = overdraftLimit;
}
if (accountType.equalsIgnoreCase("SavingsAccount")) {
SavingsAccount savingsAccount = new SavingsAccount(accountHolderName,
accountNumber, initialDeposit);
System.out.println("Account Created: SavingsAccount for " +
savingsAccount.accountHolderName +
" with Account Number " +
savingsAccount.accountNumber);
savingsAccount.displayAccountDetails();
} else if (accountType.equalsIgnoreCase("CurrentAccount")) {
double overdraftLimit = scanner.nextDouble();
} else {
System.out.println("Invalid Account Type. Please enter either
SavingsAccount or CurrentAccount.");
}
scanner.close();
}
}
ques 3:
import java.util.Scanner;
scanner.close();
}
}
Advance level
ques 1:
import java.io.*;
import java.util.Scanner;
if (choice == 1) {
// Writing to file
writeFile(fileName, content.toString());
} else if (choice == 2) {
// Reading from file
String content = readFile(fileName);
} else {
System.out.println("Invalid choice. Please select 1 or 2.");
}
} catch (FileNotFoundException e) {
System.out.println("Error Message: File not found. Please check the
file name and try again.");
} catch (IOException e) {
System.out.println("Error Message: An I/O error occurred.");
} catch (SecurityException e) {
System.out.println("Error Message: Access denied. You do not have the
necessary permissions.");
} finally {
scanner.close();
}
}
ques 2:
import java.util.Scanner;
try {
return decrypted.toString();
}
}
ques 3:
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
scanner.close();
}
Expert level
ques 1:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student {
int id;
String name;
int age;
String major;
@Override
public String toString() {
return String.format("ID: %d, Name: %s, Age: %d, Major: %s", id, name, age,
major);
}
}
switch (choice) {
case 1:
String addDetails = scanner.nextLine();
String[] addTokens = addDetails.split("\\s+");
addStudent(addTokens);
break;
case 2:
scanner.close();
}
if (found) {
System.out.println("Student with ID " + id + " is deleted.");
} else {
System.out.println("Student not found.");
}
} catch (Exception e) {
System.out.println("Error deleting student. Please ensure you enter a
valid ID.");
}
}
ques 2:
import java.util.Scanner;
// The public class should be named Main and match the filename Main.java
public class Main {
if (choice == 1) {
// CustomLinkedList operations
CustomLinkedList linkedList = new CustomLinkedList();
} else if (choice == 2) {
// CustomStack operations
CustomStack stack = new CustomStack();
scanner.close();
}
Node(int data) {
this.data = data;
this.next = null;
}
}
public CustomLinkedList() {
head = null;
size = 0;
}
public CustomStack() {
list = new CustomLinkedList();
}
ques 3:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
class Book {
private int id;
private String title;
private String author;
private int year;
@Override
public String toString() {
return "ID: " + id + ", Title: " + title + ", Author: " + author + ", Year:
" + year;
}
}
class Library {
private Map<Integer, Book> booksById;
private TreeMap<String, Book> booksByTitle;
public Library() {
booksById = new HashMap<>();
booksByTitle = new TreeMap<>();
}
// Add books
for (int i = 0; i < numBooks; i++) {
String[] bookDetails = scanner.nextLine().split(", ");
int id = Integer.parseInt(bookDetails[0].split("=")[1].trim());
String title = bookDetails[1].split("=")[1].trim();
String author = bookDetails[2].split("=")[1].trim();
int year = Integer.parseInt(bookDetails[3].split("=")[1].trim());
library.addBook(new Book(id, title, author, year));
}
// Search books by ID
int searchId = Integer.parseInt(scanner.nextLine().trim());
System.out.println("Search ID " + searchId + ":");
System.out.println(library.searchById(searchId));
// Remove a book by ID
int removeId = Integer.parseInt(scanner.nextLine().trim());
library.removeBookById(removeId);
System.out.println("Removed book with ID " + removeId);
scanner.close();
}
}