0% found this document useful (0 votes)
16 views8 pages

Assignment No.: 01 Title:: Language Compiler/IDE JDK Version OS

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)
16 views8 pages

Assignment No.: 01 Title:: Language Compiler/IDE JDK Version OS

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/ 8

Assignment No.

: 01

Title:

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.

Objectives:

1. To understand the fundamentals of Java programming.


2. To learn user input handling in Java using Scanner class.
3. To implement conditional logic for mathematical operations.
4. To apply looping constructs for repeated operations.
5. To ensure robust programming through exception handling.

Assignment Outcomes (AOs):

By the end of this assignment, students will be able to:

1. Demonstrate the use of Java classes, objects, methods, and operators in real-time
applications.
2. Apply conditional statements to implement decision-making in problem solving.
3. Employ looping constructs for repeated execution of tasks.
4. Implement exception handling to make applications fault-tolerant and reliable.
5. Design and execute a dynamic calculator program using Java.

Tools and Environment Used:

• Language: Java
• Compiler/IDE: IntelliJ IDEA / Eclipse / VS Code / Terminal with javac
• JDK Version: JDK 11 or above
• OS: Windows/Linux/macOS

Theory:

Introduction to Java Basics:

Class and Object:

1
A class is a blueprint or template for creating objects.

An object is an instance of a class that represents real-world entities.

Syntax:

class ClassName
{
// fields
// methods
}

ClassName obj = new ClassName(); // object creation

Example:

class Calculator
{
void add(int a, int b)
{
System.out.println(a + b);
}
}
public class Main
{
public static void main(String[] args)
{
Calculator c = new Calculator();
c.add(5, 3);
}
}

Methods in Java:

A method defines a block of code that performs a specific task.

Methods can take input parameters and return values.

Syntax:

returnType methodName(parameters)
{
// method body
return value;
}

Conditional Statements in Java

Conditional statements are used to control the flow of a program based on certain conditions.
They allow decision-making and branching in the program.

2
a) if Statement

Executes a block of code only if the given condition is true.

Syntax:

if (condition)
{
// statements to be executed
}

Example:

int num = 10;


if (num > 0)
{
System.out.println("Number is positive");
}

b) if-else Statement

Executes one block of code if the condition is true, and another block if it is false.

Syntax:

if (condition)
{
// true block
}
else
{
// false block
}

Example:

int num = -5;


if (num >= 0)
{
System.out.println("Non-negative number");
}
else
{
System.out.println("Negative number");
}

c) if-else-if Ladder

Used to check multiple conditions sequentially.

Syntax:

3
if (condition1)
{
// block1
}
else if (condition2)
{
// block2
}
else
{
// default block
}

Example:

int marks = 85;


if (marks >= 90)
{
System.out.println("Grade A");
}
else if (marks >= 75)
{
System.out.println("Grade B");
}
else
{
System.out.println("Grade C");
}

d) switch Statement

Provides multiple branching based on a variable’s value.

Syntax:

switch (expression)
{
case value1:
// block
break;
case value2:
// block
break;
default:
// default block
}

Example:

int day = 3;
switch (day)
{
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;

4
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Other day");
}

Loops in Java

Loops are used to execute a block of code repeatedly until a condition is satisfied.

a) for Loop

Used when the number of iterations is known in advance.

Syntax:

for(initialization; condition; update)


{
// statements
}

Example:

for (int i = 1; i <= 5; i++)


{
System.out.println("Iteration: " + i);
}

b) while Loop

Used when the number of iterations is not fixed; checks the condition before executing.

Syntax:

while (condition)
{
// statements
}

Example:

int i = 1;
while (i <= 5)
{
System.out.println("Iteration: " + i);
i++;
}

c) do-while Loop

Executes the loop body at least once before checking the condition.

5
Syntax:

do
{
// statements
} while (condition);

Example:

int i = 1;
do
{
System.out.println("Iteration: " + i);
i++;
} while (i <= 5);

Exception Handling in Java

Exception handling provides a way to handle runtime errors gracefully without crashing the
program.

Keywords Used:

• try: Defines a block of code to test for exceptions.


• catch: Defines a block of code to handle exceptions.
• finally: Defines a block of code that always executes.
• throw: Used to explicitly throw an exception.
• throws: Declares exceptions that a method may throw.

Example:
try
{
int result = 10 / 0; // will cause ArithmeticException
}
catch (ArithmeticException e)
{
System.out.println("Error: Division by zero!");
}
finally
{
System.out.println("Execution completed.");
}

Scanner Class in Java

The Scanner class is part of the java.util package. It is used to take dynamic input from the
user such as integers, floating-point numbers, strings, and characters.

6
Key Features:

• Reads input from keyboard (System.in).


• Provides methods to read different data types.
• Helps in making programs interactive.

Commonly Used Methods:

• nextInt() → Reads an integer.


• nextDouble() → Reads a double value.
• nextLine() → Reads a string (line).
• next() → Reads a single word.
• next().charAt(0) → Reads a single character.

Syntax:
import java.util.Scanner;

public class InputExample


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter an integer: ");


int num = sc.nextInt();

System.out.print("Enter a decimal number: ");


double d = sc.nextDouble();

System.out.print("Enter your name: ");


String name = sc.next();

System.out.println("Integer: " + num);


System.out.println("Double: " + d);
System.out.println("Name: " + name);

sc.close();
}
}

Conclusion:
The Java calculator efficiently handles user inputs and performs operations using conditional
logic and loops. It also includes error handling to ensure smooth and reliable execution.

7
Algorithm:

Step 1: Start the program.


Step 2: Import the Scanner class from java.util package.
Step 3: Create a Calculator class with the main() method.
Step 4: Inside main(), create a Scanner object to read user input.
Step 5: Initialize a variable choice to store the user’s decision to continue or exit.
Step 6: Use a do-while loop to repeatedly perform calculator operations until the user decides to
exit.
Step 7: Prompt the user to enter two numbers (num1 and num2).
Step 8: Display the list of available mathematical operations (+, -, *, /, %).
Step 9: Accept the operator choice from the user.
Step 10: Use a switch-case statement (or if-else) to perform the selected operation:

• Case + → Perform addition.


• Case - → Perform subtraction.
• Case * → Perform multiplication.
• Case / → Perform division (check for division by zero using exception handling).
• Case % → Perform modulo (check for zero divisor).
• Default → Display “Invalid operator”.
Step 11: Display the result of the operation.
Step 12: If any invalid input or error occurs, catch the exception and display an error
message.
Step 13: Ask the user if they want to continue (y/n).
Step 14: If the user enters y or Y, repeat from Step 7. Otherwise, exit the loop.
Step 15: Display “Calculator closed.”
Step 16: Stop the program.

You might also like