0% found this document useful (0 votes)
6 views24 pages

Java Final

The document outlines various system designs including Online Shopping, Banking, Library Management, Student Result, Ride Sharing, Exam Registration, Hospital Record, Online Quiz, and University Portal Systems. Each system includes class structures, methods, exception handling, file I/O, and main functionalities. The designs emphasize object-oriented principles such as inheritance, generics, and associations.

Uploaded by

azmansikder143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Java Final

The document outlines various system designs including Online Shopping, Banking, Library Management, Student Result, Ride Sharing, Exam Registration, Hospital Record, Online Quiz, and University Portal Systems. Each system includes class structures, methods, exception handling, file I/O, and main functionalities. The designs emphasize object-oriented principles such as inheritance, generics, and associations.

Uploaded by

azmansikder143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Online Shopping System


Scenario:

An e-commerce system manages customers, products, and orders.

Part A: Base Class – Product

Create a class Product with:

●​ productId (int)
●​ productName (String)
●​ price (double)

Method:

●​ void displayProduct() → prints product details

Part B: Derived Class – ElectronicProduct

●​ warranty (int)

Override:

●​ displayProduct()

Part C: Association – Customer & Order

Create:

Customer

●​ customerId (int)
●​ customerName (String)

Order

●​ orderId (int)
●​ customer (Customer)
●​ productList (ArrayList<Product>)

Methods:

●​ addProduct(Product p)
●​ calculateTotal()

Part D: Service Class

Create OrderService:

●​ void placeOrder(Order o)
Part E: Main

●​ Create multiple products


●​ Add to ArrayList
●​ Assign to order

2. Banking System
Scenario:

A banking system stores account details and handles transactions.

Part A: Class – BankAccount

Attributes:

●​ accountNumber (int)
●​ balance (double)

Methods:

●​ deposit(double amount)
●​ withdraw(double amount)

Part B: Custom Exception

Create:​
InsufficientBalanceException

Throw when:

●​ withdrawal > balance

Part C: File Handling

Create class BankFileService:

●​ saveAccount(BankAccount acc) → write to file


●​ readAccounts() → read from file

Part D: Exception Handling

Handle:

●​ IOException
●​ Custom exception

Part E: Main

●​ Create accounts
●​ Perform transactions
●​ Save & read from file

import [Link].*;

class InsufficientBalanceException extends Exception {

InsufficientBalanceException(String m) {

super(m);

class BankAccount {

int acc;

double bal;

void deposit(double a) {

bal += a;

void withdraw(double a) throws InsufficientBalanceException {

if (a > bal) throw new InsufficientBalanceException("Low balance");

bal -= a;

public class Main {

public static void main(String[] args) throws Exception {

BankAccount b = new BankAccount();

[Link](100);
try {

[Link](200);

} catch (Exception e) {

[Link]([Link]());

FileWriter fw = new FileWriter("[Link]");

[Link]("Saved");

[Link]();

3. Library Management System


Scenario:

A library stores books and members using generics.

Part A: Class – Book

●​ bookId
●​ title
●​ author

Part B: Class – Member

●​ memberId
●​ memberName

Part C: Generic Class

Create:​
Library<T>

Attributes:

●​ ArrayList<T> items

Methods:
●​ addItem(T item)
●​ removeItem(T item)
●​ displayItems()

Part D: Association

Create:​
IssueRecord

●​ Book
●​ Member

Part E: Main

●​ Use Library<Book>
●​ Use Library<Member>

import [Link].*;

class Library<T> {

ArrayList<T> list = new ArrayList<>();

void add(T t) {

[Link](t);

void display() {

for (T t : list) [Link](t);

public class Main {

public static void main(String[] args) {

Library<String> b = new Library<>();

[Link]("Book1");
Library<Integer> m = new Library<>();

[Link](101);

[Link]();

[Link]();

4. Student Result System

Scenario:

System calculates student results and stores them in file.

Part A: Base Class – Student

●​ id
●​ name

Method:

●​ double calculateResult()

Part B: Derived Class – ScienceStudent

●​ labMarks

Override:

●​ calculateResult()

Part C: Exception

Create:​
InvalidMarksException

Part D: File Handling

Write results to:

●​ "[Link]"

Part E: Main

●​ Input marks
●​ Handle invalid input
●​ Save results

import [Link].*;

class Student {

int id;

String name;

double calculateResult() {

return 50;

class ScienceStudent extends Student {

double lab;

double calculateResult() {

return 50 + lab;

public class Main {

public static void main(String[] args) throws Exception {

ScienceStudent s = new ScienceStudent();

[Link] = 20;

FileWriter fw = new FileWriter("[Link]");

[Link]("Result: " + [Link]());

[Link]();

}
}

5. Ride Sharing System


Scenario:

A ride-sharing app manages drivers and rides.

Part A: Base Class – Ride

●​ rideId
●​ distance

Method:

●​ calculateFare()

Part B: Derived Class – PremiumRide

●​ extraCharge

Override:

●​ calculateFare()

Part C: Generic Class

RideManager<T extends Ride>

●​ store rides in ArrayList


●​ method:
○​ addRide(T r)
○​ calculateTotalFare()

Part D: Main

●​ Create multiple rides


●​ Store in manager

6. Exam Registration System


Scenario:

Students register for exams and data is stored.


Part A: Class – Student

●​ id
●​ name

Part B: Class – Exam

●​ examName
●​ date

Part C: Exception

DuplicateRegistrationException

Part D: Collection

Use:

●​ HashSet<Student>

Part E: File I/O

●​ Save registrations to file

Part F: Main

●​ Prevent duplicate registration


●​ Store data

import [Link].*;

import [Link].*;

class Student {

int id;

String name;

Student(int id, String name) {

[Link] = id;

[Link] = name;

public int hashCode() { return id; }


public boolean equals(Object o) {

Student s = (Student) o;

return id == [Link];

public class Main {

public static void main(String[] args) throws Exception {

HashSet<Student> set = new HashSet<>();

Student s1 = new Student(1, "A");

Student s2 = new Student(1, "A");

[Link](s1);

[Link](s2); // duplicate ignored

FileWriter fw = new FileWriter("[Link]");

for (Student s : set) {

[Link]([Link] + " " + [Link] + "\n");

[Link]();

7. Hospital Record System

Scenario:

Hospital stores patient records generically.


Part A: Class – Patient

●​ id
●​ name

Part B: Generic Class

Record<T>

Methods:

●​ addRecord(T r)
●​ displayRecords()

Part C: Exception

RecordNotFoundException

Part D: File Handling

●​ Save records to file


●​ Read records

Part E: Main

●​ Add patients
●​ Handle missing record

import [Link].*;

import [Link].*;

class Patient {

int id;

String name;

Patient(int id, String name) {

[Link] = id;

[Link] = name;

public String toString() {


return id + " " + name;

class Record<T> {

ArrayList<T> list = new ArrayList<>();

void addRecord(T r) {

[Link](r);

void displayRecords() {

for (T t : list) [Link](t);

public class Main {

public static void main(String[] args) throws Exception {

Record<Patient> r = new Record<>();

[Link](new Patient(1, "Ali"));

[Link]();

FileWriter fw = new FileWriter("[Link]");

[Link]("Saved");

[Link]();

}
8. Online Quiz System

Scenario:

Quiz system evaluates different types of questions.

Part A: Base Class – Question

●​ questionText

Method:

●​ checkAnswer()

Part B: Derived Classes

●​ MCQQuestion
●​ TrueFalseQuestion

Part C: Collection

●​ ArrayList<Question>

Part D: Exception

InvalidAnswerException

Part E: Main

●​ Store questions
●​ Evaluate answers

Ans:​
import [Link].*;

class Question {
String text;

void checkAnswer(String ans) {}


}

class MCQQuestion extends Question {


String correct = "A";

void checkAnswer(String ans) {


if ([Link](correct)) [Link]("Correct");
else [Link]("Wrong");
}
}

class TrueFalseQuestion extends Question {


String correct = "true";
void checkAnswer(String ans) {
if ([Link](correct)) [Link]("Correct");
else [Link]("Wrong");
}
}

public class Main {


public static void main(String[] args) {
ArrayList<Question> list = new ArrayList<>();

MCQQuestion q1 = new MCQQuestion();


TrueFalseQuestion q2 = new TrueFalseQuestion();

[Link](q1);
[Link](q2);

for (Question q : list) {


[Link]("A");
}
}
}

9. ​
Design a “University Portal System” that includes:

●​ Students, Courses (Inheritance)


●​ Registration (Association)
●​ Generic Storage System
●​ File Saving (File I/O)
●​ Error Handling (Exception)
●​ Use ArrayList / HashMap

Ans:​
import [Link].*;
import [Link].*;

class Student {
int id;
String name;

Student(int id, String name) {


[Link] = id;
[Link] = name;
}
}

class Course {
String courseName;
Course(String c) {
courseName = c;
}
}

class Registration {
Student s;
Course c;

Registration(Student s, Course c) {
this.s = s;
this.c = c;
}

public String toString() {


return [Link] + " " + [Link] + " -> " + [Link];
}
}

class Storage<T> {
ArrayList<T> list = new ArrayList<>();

void add(T t) {
[Link](t);
}

void display() {
for (T x : list) [Link](x);
}
}

public class Main {


public static void main(String[] args) throws Exception {
Student s = new Student(1, "A");
Course c = new Course("Java");

Registration r = new Registration(s, c);

Storage<Registration> st = new Storage<>();


[Link](r);
[Link]();

FileWriter fw = new FileWriter("[Link]");


[Link]([Link]());
[Link]();
}
}
1.​
import [Link].*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String text = [Link]();

String[] parts = [Link]("_");

int total = 0;

for (int i = 0; i < [Link]; i++) {


total += parts[i].length();
}

if (total % 3 == 0) {
[Link]("YES");
} else {
int need = 3 - (total % 3);
[Link]("Need: " + need);
}
}
}

2.

class Address {
String house_no;
int road_no;
String area;

Address(String h, int r, String a) {


house_no = h;
road_no = r;
area = a;
}

public String toString() {


return house_no + ", Road-" + road_no + ", " + area;
}
}

class Authors {
String name;
int age;
Address adrs;
String paper_name;
int no_of_papers;

Authors(String n, int age, Address ad, String p, int num) {


[Link] = n;
[Link] = age;
[Link] = ad;
this.paper_name = p;
this.no_of_papers = num;
}

public String toString() {


return name + " | " + age + " | " + adrs +
" | " + paper_name + " | " + no_of_papers;
}
}

public class Test{


public static void main(String[] args){
Authors[] arr = new Authors[5];
arr[0] = new Authors("A", 30, new Address("12A", 5, "Dhaka"), "AI", 2);
arr[1] = new Authors("B", 25, new Address("22B", 3, "Khulna"), "ML", 1);
arr[2] = new Authors("C", 40, new Address("33C", 2, "Rajshahi"), "DS", 3);
arr[3] = new Authors("D", 35, new Address("44D", 7, "Sylhet"), "CV", 1);
arr[4] = new Authors("E", 28, new Address("55E", 9, "Chittagong"), "NLP", 4);

for(int i=0;i<[Link];i++){
if(arr[i].no_of_papers>1){
[Link](arr[i].toString());
}
}
}

1.​
import [Link].*;
import [Link].*;

class CarNoException extends Exception {


CarNoException(String message) {
super(message);
}
}

class Cars {
String no;
String model;
double price;

Cars(String no, String model, double price) throws CarNoException {


if ([Link]() < 3 || [Link]() > 5) {
throw new CarNoException("Invalid Car No " + no);
}
[Link] = no;
[Link] = model;
[Link] = price;
}
public void setno(String no){
[Link] = no;
}
public String getno(){
return no;
}

public void setmodel(String model){


[Link] = model;
}
public String getmodel(){
return model;
}

public void setprice(double price){


[Link] = price;
}
public double getprice(){
return price;
}
public double taxes() {
return price * 0.05;
}

public String toString() {


return no + " " + model + " " + price + " Tax:" + taxes();
}
}

public class Test {


public static void main(String[] args) {

try {
File f = new File("[Link]");
Scanner sc = new Scanner(f);

FileWriter fw = new FileWriter("[Link]");


while ([Link]()) {
String line = [Link]();
String[] data = [Link](" ");

String no = data[0];
String model = data[1];
double price = [Link](data[2]);

try {
Cars c = new Cars(no, model, price);

[Link]([Link]() + "\n");

if ([Link]("_")) {
[Link]("Car No: " + no);
}

} catch (CarNoException e) {
[Link]([Link]());
}
}

[Link]();
[Link]();

} catch (Exception e) {
[Link](e);
}
}
}
1.​
import [Link].*;
import [Link].*;

class InvalidYearException extends Exception{


InvalidYearException(String message){
super(message);
}
}

class Cars{
String no;
String model;
double price;
int year;

Cars(String no, String model, double price, int year) throws InvalidYearException{
if(year<1800){
throw new InvalidYearException("Invalid year, "+year);
}
[Link] = no;
[Link] = model;
[Link] = price;
[Link] = year;
}

public void setno(String no){


[Link] = no;
}
public String getno(){
return no;
}

public void setmodel(String model){


[Link] = model;
}
public String getmodel(){
return model;
}

public void setprice(double price){


[Link] = price;
}
public double getprice(){
return price;
}
public void setyear(int year){
[Link] = year;
}
public int getyear(){
return year;
}

public double taxes() {


return price * 0.05;
}

public String toString() {


return "No: " + no + " |Model: " + model + " |Price: " + price +
" |Year: " + year + " |Tax: " + taxes();
}
}

public class Set_2{


public static void main(String[] args){
try{
File fr = new File("[Link]");
Scanner sc = new Scanner(fr);

FileWriter fw = new FileWriter("[Link]");

while([Link]()){
String line = [Link]();
String[] data = [Link](" ");
if(data[0].length()==4)
{
[Link](line + "\n");
}
}
[Link]();
[Link]();

File f2 = new File("[Link]");


Scanner sc2 = new Scanner(f2);

while([Link]()){
String line = [Link]();
String[] data = [Link](" ");

String no = data[0];
String model = data[1];
double price = [Link](data[2]);
int year = [Link](data[3]);
try{
Cars c = new Cars(no, model, price, year);

if([Link]("C") || [Link]("A")){
[Link]([Link]());
}

}catch (InvalidYearException e){


[Link]([Link]());
}
}

[Link]();

}catch (Exception e){


[Link](e);
}
}
}

You might also like