D2 Slot
D2 Slot
PART – A: Answer any ALL Questions, Each Question Carries 10 Marks (5×10=50 Marks)
1. A national bank wants to develop an ATM System that provides secure and seamless banking
services to its customers. The system should allow users to log in using their credentials and
perform basic banking operations, including Checking Balance, Depositing Money, Withdrawing
Money, and Exiting the System. The ATM system must incorporate authentication and transaction
validation to ensure security and reliability.
Users must enter a valid PIN to access their account.
The system should provide a menu-driven interface using control statements (if-else,
switch-case, loops).
Check their account balance.
Deposit money by entering a valid amount.
Withdraw money while ensuring the account has sufficient balance.
Exit the system gracefully.
The system should be secure, allowing only three failed login attempts before
blocking access.
The system should display appropriate error messages for invalid transactions (e.g.,
insufficient balance, incorrect PIN).
The system should maintain user sessions until they choose to exit.
Design and implement a Java-based ATM program that uses control statements (if-else,
while, switch-case) to manage user authentication and banking transactions efficiently.
(10 M)
Sol: import java.util.Scanner;
public class ATMSystem {
private static final int PIN = 1234; // Predefined PIN for simplicity
private static final int MAX_ATTEMPTS = 3;
private double balance;
private int attempts;
private boolean authenticated;
public ATMSystem() {
this.balance = 1000.0; // Initial balance
this.attempts = 0;
Page 1 of 11
this.authenticated = false;
}
Page 2 of 11
break;
case 2:
System.out.print("Enter deposit amount: ");
double depositAmount = scanner.nextDouble();
depositMoney(depositAmount);
break;
case 3:
System.out.print("Enter withdrawal amount: ");
double withdrawAmount = scanner.nextDouble();
withdrawMoney(withdrawAmount);
break;
case 4:
System.out.println("Exiting... Thank you for using our ATM.");
authenticated = false;
break;
default:
System.out.println("Invalid option. Please try again.");
}
}
scanner.close();
}
private void checkBalance() {
System.out.println("Your current balance is: $" + balance);
}
private void depositMoney(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("$" + amount + " deposited successfully.");
checkBalance();
} else {
System.out.println("Invalid amount. Please enter a positive value.");
}
Page 3 of 11
}
private void withdrawMoney(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("$" + amount + " withdrawn successfully.");
checkBalance();
} else if (amount > balance) {
System.out.println("Insufficient balance. Transaction failed.");
} else {
System.out.println("Invalid amount. Please enter a positive value.");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ATMSystem atm = new ATMSystem();
System.out.println("Welcome to National Bank ATM");
while (!atm.authenticated && atm.attempts < MAX_ATTEMPTS) {
System.out.print("Enter your PIN: ");
int enteredPin = scanner.nextInt();
atm.authenticateUser(enteredPin);
}
if (atm.authenticated) {
atm.displayMenu();
}
scanner.close();
}
}
2. A data science research team is working with large matrices to store information related to social
network connections. However, most of the matrices contain a majority of zero elements, making
them sparse matrices. To optimize storage and processing time, the team wants to identify whether
a given matrix is sparse before proceeding with computations.
Page 4 of 11
A sparse matrix is defined as a matrix where the number of zero elements is greater than half of the
total elements.
a. As a software developer, you have been tasked with writing a Java program that:
b. Accepts a matrix as input from the user.
c. Determines whether the matrix is sparse.
d. Displays an appropriate message indicating whether the given matrix is sparse or not.
(10 M)
Sol:
import java.util.Scanner;
scanner.close();
}
}
Page 5 of 11
3. A software company is expanding its workforce and needs a system to manage its employees. The
company has different roles, but all employees share some common characteristics. The HR
Department is responsible for adding new employees to the company database. The system should
include:
I. A base class Employee with:
a. A method work() that prints "Employee is working."
b. A method getSalary() that returns a default salary value.
II. A subclass HRManager that:
a. Overrides the work() method to print "HR Manager is managing recruitment."
b. Adds a new method addEmployee(), which prints "New employee added to the company."
Your task is to write a Java program that demonstrates the concept of inheritance and method
overriding using these classes. (10 M)
Sol:
class Employee {
// Method to display work message
public void work() {
System.out.println("Employee is working.");
}
Page 6 of 11
// Creating HRManager object
HRManager hr = new HRManager();
hr.work();
hr.addEmployee();
System.out.println("HR Manager salary: $" + hr.getSalary());
}
}
4. Imagine you are designing a vehicle inventory system for a two-wheeler dealership. The dealership
sells different models of Honda Activa and Hero Access scooters. You need to create a system
that models these vehicles and their pricing. Define a class named Vehicle with the following
attributes:
I. A String field for the vehicle's name.
II. A double field for the vehicle's price.
III. A constructor to initialize the vehicle's name.
IV. A method setPrice() to set the vehicle’s price.
V. Methods getName() and getPrice() to return the vehicle's name and price.
Implement two child classes: HondaActiva and HeroAccess. Each class should inherit from
Vehicle. In each class, override the setPrice() method to set the price: Honda Activa: ₹90,000 and
Hero Access: ₹85,000. Use a constructor to assign the vehicle’s name and call setPrice() to
initialize the price. Write a program that creates objects for Honda Activa and Hero Access.
Display the name and price of both vehicles using the appropriate methods.
(10 M)
Sol:
class Vehicle {
private String name;
private double price;
Page 7 of 11
}
}
5. Design a Java program that allows users to book airline tickets using the TicketManager class. The
program should prompt the user to enter the departure airport, destination airport, passenger type,
and travel class (Use Scanner Class). Implement the TicketManager class to issue tickets based on
different criteria:
I. The first method should issue a ticket with a default fare.
II. The second method should issue a ticket based on the passenger type, with the following
fare structure:
Adult: Rs. 5000, Child: Rs. 2500, and Senior Citizen: Rs. 4000
III. The third method should issue a ticket based on both the passenger type and travel class,
with the following travel class multipliers applied to the base fares:
Economy Class: Base fare (no multiplier, i.e., 1.0x), Business Class: 1.5x the base fare and
First Class: 2.0x the base fare
Page 8 of 11
The program should prompt the user to enter the required details (departure, destination, passenger
type, and travel class). Based on the user input, an appropriate ticket should be generated using the
TicketManager class. Allow users to input their desired values, generate the corresponding airline
ticket details, and display the ticket information using fare calculation. (10 M)
class TicketManager {
// Method to issue a ticket with default fare
public void issueTicket(String departure, String destination) {
System.out.println("Ticket Issued: " + departure + " to " + destination);
System.out.println("Fare: Rs. 5000 (Default Fare)");
}
Page 9 of 11
break;
case "senior citizen":
baseFare = 4000;
break;
default:
System.out.println("Invalid passenger type. Using default fare.");
baseFare = 5000;
}
double multiplier;
switch (travelClass.toLowerCase()) {
case "economy":
multiplier = 1.0;
break;
case "business":
multiplier = 1.5;
break;
case "first class":
multiplier = 2.0;
break;
default:
System.out.println("Invalid travel class. Using economy fare.");
multiplier = 1.0;
}
Page 10 of 11
String passengerType = scanner.nextLine();
scanner.close();
}
}
Page 11 of 11