Tuesday, 24 June 2025

Implement a robust Java calculator program that computes user input dynamically. ( OOPCGL 2024 Course)



PCC-205-COM: Object Oriented programming and Computer Graphics Lab (2024 Course)



Group A

1. Title: Implement a robust Java calculator program 


2. Objective: To explore & understand the principles of Object-Oriented Programming (OOP).


3. Problem Statement: Implement a robust Java calculator program that captures user input dynamically, processes mathematical operations using conditional logic and looping constructs, and ensures efficient error handling.


4. Outcomes:

After implementing the above problem statement, the expected output is as follows:

1. Dynamically user can accept any input.

2. User can perform following mathematical operations using conditional logic (operational rules R) and looping constructs.

    2.1 Addition of input a, b (a + b)

    2.2 Subtraction of input a, b (a - b)

   2.3 Multiplication of input a, b (a * b)

    2.4 Division of input a, b (a / b)

    2.5 Reminder of input a, b (a % b)

3. Error like divide by zero error gets handle by using try and catch block.


5. Software and Hardware Requirements:

5.1 Software Requirements: 

1. Operating System: 64-bit Open-source Linux or its derivative

2. Programming tools: - Open-Source Java Open JDK,

3. Programming IDE: BlueJ, Eclipse, NetBeans, JDeveloper.

5.2 Hardware Requirements:

13th Gen Intel(R) Core (TM) i5-13400   2.50 GHz, 8 GB RAM


6. Mathematical Model:

The model can be expressed as:

Given a, b ∈ R and O ∈ {+×÷%} 

Compute R using the operation rules given in Output Section above, Subject to b 0 if O∈ {÷, %}.

Where,

Input: a, b, : operator chosen by user

Operations: The calculator performs operations O on inputs a and b

Error: if b = 0 and O∈ {÷, %}.

Constraints: b! = for division (/) and modulus (%)


7. Algorithm:

Step 1: Start

Step 2: Accept input a, b and operator O belongs to +, -, x, / and mod from user dynamically.

Step 3: Now as per the operator following operations are going to be performed.

Step 3.1: if operator is + then show result = a + b

Step 3.2: if operator is - then show result = a - b

Step 3.3: if operator is * then show result = a * b

Step 3.4: if operator is / then check the constraint b (b! =0) is not equal to zero if this constraint is true then you have to throw the exception and handle it by using catch block. else if this constraint is false you have to do normal operation & show result = a / b.

Step 3.5: if operator is % (mod) then check the constraint b (b! =0) is not equal to zero if this constraint is true then you have to throw the exception and handle it by using catch block. else if this constraint is false you have to do normal operation & show result = a % b.

Step 4: Stop


8. Flowchart:


9. Program Code:



10. Sample Output:

Test Case: 01 Inputs to test all basic operation of calculator using valid inputs.


Test Case: 02 Inputs to test Exception Handling



11. Conclusion:

Hence, we learn how to apply fundamental programming constructs in Java for implementing an application.


Note: 

Dear Readers,
    if you have any suggestions or find any misconception in above implemented code, please guide me for the same. so that i can improve myself for the same. Thanking you in advance. Please reply to your valuable feedback here in comments or DM me on my what's app number +91 9096454130

data structures and algorithms Web Developer

No comments:

Post a Comment