0% found this document useful (0 votes)
4 views3 pages

Ooops 11

The document contains Java code demonstrating concepts such as method overloading, inheritance, and exception handling. It includes classes for calculating areas of geometric shapes, managing bank accounts, and handling student marks with eligibility checks. The code also features a simple menu-driven interface for user interactions.

Uploaded by

msreenath283
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)
4 views3 pages

Ooops 11

The document contains Java code demonstrating concepts such as method overloading, inheritance, and exception handling. It includes classes for calculating areas of geometric shapes, managing bank accounts, and handling student marks with eligibility checks. The code also features a simple menu-driven interface for user interactions.

Uploaded by

msreenath283
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
You are on page 1/ 3

// method overloading // Rectangle input // single inheritance

System.out.print(“Enter length of rectangle: “);

Import java.io.*; Double length = Double.parseDouble(reader.readLine()); Import java.io.*;

System.out.print(“Enter breadth of rectangle: “);

Class Shape { Double breadth = Double.parseDouble(reader.readLine()); Class Account {

// Area of rectangle: length * breadth System.out.println(“Rectangle Area: “ + shape.area(length, breadth)); String acno; // Account number

Public double area(double length, double breadth) { String name; // Account holder’s name

Return length * breadth; // Triangle input

} System.out.print(“Enter base of triangle: “); Public Account(String acno, String name) {

Double base = Double.parseDouble(reader.readLine()); This.acno = acno;

// Area of triangle: 0.5 * base * height System.out.print(“Enter height of triangle: “); This.name = name;

Public double area(double base, double height, boolean isTriangle) { Double height = Double.parseDouble(reader.readLine()); }

If (isTriangle) { System.out.println(“Triangle Area: “ + shape.area(base, height, true)); }

Return 0.5 * base * height;

} // Circle input Class Savings extends Account {

Return -1; // Invalid case System.out.print(“Enter radius of circle: “); Double balance;

} Float radius = Float.parseFloat(reader.readLine()); Final double MIN_BALANCE = 1000;

System.out.println(“Circle Area: “ + shape.area(radius));

// Area of circle: π * radius * radius } Public Savings(String acno, String name, double initialBalance) {

Public double area(float radius) { } Super(acno, name);

Return Math.PI * radius * radius; This.balance = initialBalance >= MIN_BALANCE ? initialBalance : MIN_BALANCE;

} }

Public void deposit(double amount) {

Public class Exam { Balance += amount;

Public static void main(String[] args) throws IOException { System.out.println(“Deposited: “ + amount);

Bu eredReader reader = new Bu eredReader(new InputStreamReader(System.in)); }

Shape shape = new Shape();

Public void withdraw(double amount) {

If (balance – amount >= MIN_BALANCE) { While (!exit) { }

Balance -= amount; System.out.println(“\nBank Menu:”); }

System.out.println(“Withdrawn: “ + amount); System.out.println(“1. Deposit”);

} else { System.out.println(“2. Withdraw”); Br.close();

System.out.println(“Error: Insu icient balance or withdrawal would go below minimum balance.”); System.out.println(“3. Display Balance”); }

} System.out.println(“4. Exit”); }

} System.out.print(“Choose an option: “);

Int choice = Integer.parseInt(br.readLine());

Public void displayBalance() {

System.out.println(“Account: “ + acno); Switch (choice) {

System.out.println(“Name: “ + name); Case 1:

System.out.println(“Current Balance: “ + balance); System.out.print(“Enter deposit amount: “);

} Double depositAmount = Double.parseDouble(br.readLine());

} Account.deposit(depositAmount);

Break;

Public class Exam { Case 2:

Public static void main(String[] args) throws IOException { System.out.print(“Enter withdrawal amount: “);

InputStreamReader isr = new InputStreamReader(System.in); Double withdrawAmount = Double.parseDouble(br.readLine());

Bu eredReader br = new Bu eredReader(isr); Account.withdraw(withdrawAmount);

Break;

// Create an Account and Savings object Case 3:

System.out.print(“Enter Account Number: “); Account.displayBalance();

String acno = br.readLine(); Break;

System.out.print(“Enter Account Holder’s Name: “); Case 4:

String name = br.readLine(); System.out.println(“Exiting…”);

Exit = true;

Savings account = new Savings(acno, name, 1500); // Initial balance 1500 Break;

Default:

Boolean exit = false; System.out.println(“Invalid choice. Please try again.”);


// exeption If (math < 60 || phy < 60 || chem < 60) { // multi level inheritance

Throw new ArithmeticException(“Not eligible: Marks in one or more subjects below 60%”);

Import java.io.*; } Import java.io.*;

Public class Exam { If (percentage < 50) { Class Student {

Public static void main(String[] args) throws IOException { Throw new ArithmeticException(“Not eligible: Total percentage below 50%”); String regNo; // Registration number

Index obj = new Index(); } String name; // Name of the student

Obj.calculate();

} System.out.println(“Eligible. Total Percentage: “ + percentage + “%”); Public Student(String regNo, String name) {

} This.regNo = regNo;

} catch (ArithmeticException e) { This.name = name;

Class Index { System.out.println(e.getMessage()); }

Public void calculate() throws IOException { } }

InputStreamReader isr = new InputStreamReader(System.in); }

Bu eredReader br = new Bu eredReader(isr); } Class CE_Marks extends Student {

Double ceMarks; // Continuous Evaluation marks

Try {

System.out.print(“Enter Math marks: “); Public CE_Marks(String regNo, String name, double ceMarks) {

Int math = Integer.parseInt(br.readLine()); Super(regNo, name);

This.ceMarks = ceMarks;

System.out.print(“Enter Physics marks: “); }

Int phy = Integer.parseInt(br.readLine()); }

System.out.print(“Enter Chemistry marks: “); Class ESE_Marks extends CE_Marks {

Int chem = Integer.parseInt(br.readLine()); Double eseMarks; // End-Semester Exam marks

Double total = math + phy + chem; Public ESE_Marks(String regNo, String name, double ceMarks, double eseMarks) {

Double percentage = total / 3.0; Super(regNo, name, ceMarks);

This.eseMarks = eseMarks;

} Public class Exam { // interface

Public static void main(String[] args) throws IOException {

Public double calculateTotalMarks() { InputStreamReader isr = new InputStreamReader(System.in); Import java.io.*;

Return (ceMarks / 50.0) * 100 + (eseMarks / 75.0) * 100; Bu eredReader br = new Bu eredReader(isr);

} Interface Savings {

// Get student details Void withdraw(double amount);

Public String calculateGrade() { System.out.print(“Enter Registration Number: “); Double MIN_BAL = 500;

Double totalMarks = calculateTotalMarks(); String regNo = br.readLine(); }

If (totalMarks >= 85) { System.out.print(“Enter Name: “);

Return “A”; String name = br.readLine(); Class Account implements Savings {

} else if (totalMarks >= 70) { System.out.print(“Enter CE Marks (out of 50): “); String acno;

Return “B”; Double ceMarks = Double.parseDouble(br.readLine()); String name;

} else if (totalMarks >= 50) { System.out.print(“Enter ESE Marks (out of 75): “); String address;

Return “C”; Double eseMarks = Double.parseDouble(br.readLine()); Double bal;

} else {

Return “F”; // Create an instance of ESE_Marks and display the details Account(String acno, String name, String address, double bal) {

} ESE_Marks student = new ESE_Marks(regNo, name, ceMarks, eseMarks); This.acno = acno;

} Student.displayDetails(); This.name = name;

} This.address = address;

Public void displayDetails() { } This.bal = bal >= MIN_BAL ? bal : MIN_BAL;

System.out.println(“Registration Number: “ + regNo); }

System.out.println(“Name: “ + name);

System.out.println(“CE Marks: “ + ceMarks + “/50”); Public void deposit(double amount) {

System.out.println(“ESE Marks: “ + eseMarks + “/75”); If (amount > 0) {

System.out.println(“Total Marks: “ + calculateTotalMarks()); Bal += amount;

System.out.println(“Grade: “ + calculateGrade()); System.out.println(“Deposited: “ + amount);

} } else {

} System.out.println(“Invalid deposit amount.”);

}
} System.out.print(“Enter Address: “);

String addr = br.readLine(); Case 3:

Public void withdraw(double amount) { System.out.print(“Enter Initial Balance: “); Account.enquiry();

If (amount > 0 && (bal – amount) >= MIN_BAL) { Double bal = Double.parseDouble(br.readLine()); Break;

Bal -= amount;

System.out.println(“Withdrawn: “ + amount); Account account = new Account(acno, name, addr, bal); Case 4:

} else { Running = false;

System.out.println(“Cannot withdraw. Balance would fall below minimum.”); Boolean running = true; System.out.println(“Exiting…”);

} While (running) { Break;

} System.out.println(“\n--- Menu ---“);

System.out.println(“1. Deposit”); Default:

Public void enquiry() { System.out.println(“2. Withdraw”); System.out.println(“Invalid choice.”);

System.out.println(“\nAccount Details:”); System.out.println(“3. Enquiry”); }

System.out.println(“Account Number: “ + acno); System.out.println(“4. Exit”); }

System.out.println(“Name: “ + name); System.out.print(“Choose an option: “); }

System.out.println(“Address: “ + address); Int choice = Integer.parseInt(br.readLine()); }

System.out.println(“Current Balance: “ + bal);

} Switch (choice) {

} Case 1:

System.out.print(“Enter deposit amount: “);

Public class Exam { Double dep = Double.parseDouble(br.readLine());

Public static void main(String[] args) throws IOException { Account.deposit(dep);

Bu eredReader br = new Bu eredReader(new InputStreamReader(System.in)); Break;

// Get account details before menu Case 2:

System.out.print(“Enter Account Number: “); System.out.print(“Enter withdrawal amount: “);

String acno = br.readLine(); Double wdr = Double.parseDouble(br.readLine());

System.out.print(“Enter Name: “); Account.withdraw(wdr);

String name = br.readLine(); Break;

You might also like