DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
EXPERIMENT 2
Student Name: Supriya Dutta UID: 23BCS13291
Branch: CSE(2nd Year) Section/Group: Krg-803A
Semester: 4th Date of Performance: 10/02/25
Subject Name: JAVA Subject Code: 23CSP-202
1. AIM:
Write a program that takes two strings representing numbers, converts them to wrapper classes,
and performs basic arithmetic operations (addition, subtraction, multiplication, division).
2. OBJECTIVES:
To understand the concept of wrapper classes in Java and their role in converting string
representations of numbers into numeric data types.
To implement type conversion by converting input strings to appropriate wrapper class
objects such as Integer or Double.
To perform basic arithmetic operations (addition, subtraction, multiplication, and division)
using the converted numeric values.
To explore exception handling for invalid inputs and division by zero scenarios.
To enhance understanding of object-oriented programming by utilizing wrapper classes for
seamless numeric operations.
3. CODE:
import java.util.Scanner;
public class WrapperClassArithmeticNoTryCatch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
String num1 = scanner.nextLine();
System.out.print("Enter the second number: ");
String num2 = scanner.nextLine();
if (isNumeric(num1) && isNumeric(num2)) {
Double number1 = Double.parseDouble(num1);
Double number2 = Double.parseDouble(num2);
double addition = number1 + number2;
double subtraction = number1 - number2;
double multiplication = number1 * number2;
double division = number2 != 0 ? number1 / number2 : Double.NaN;
System.out.println("\nResults:");
System.out.println("Addition: " + addition);
System.out.println("Subtraction: " + subtraction);
System.out.println("Multiplication: " + multiplication);
if (number2 != 0) {
System.out.println("Division: " + division);
} else {
System.out.println("Division: Undefined (division by zero)");
}
} else {
System.out.println("Error: Please enter valid numeric strings.");
}
scanner.close();
}
public static boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
4. OUTPUT: