Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views2 pages

Java Exp 2

This document outlines an experiment for a Computer Science and Engineering course, focusing on Java programming. The aim is to create a program that converts string representations of numbers into wrapper classes and performs basic arithmetic operations while handling exceptions for invalid inputs. The provided code demonstrates the implementation of these objectives, including user input and error handling for division by zero.

Uploaded by

Specsy Guy
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)
3 views2 pages

Java Exp 2

This document outlines an experiment for a Computer Science and Engineering course, focusing on Java programming. The aim is to create a program that converts string representations of numbers into wrapper classes and performs basic arithmetic operations while handling exceptions for invalid inputs. The provided code demonstrates the implementation of these objectives, including user input and error handling for division by zero.

Uploaded by

Specsy Guy
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/ 2

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:

You might also like