deV
Programs on Wrapper Classes
Question 1:
Write a Java program to find the minimum
and maximum value of an integer using
Integer wrapper class methods.
Solution 1:
public class MinMaxIntegerExample
{
public static void main(String[] args) {
// Finding minimum and maximum
values using Integer wrapper class
int minValue = Integer.MIN_VALUE;
int maxValue = Integer.MAX_VALUE;
// Displaying the results
System.out.println("Minimum Value
of Integer: " + minValue);
System.out.println("Maximum Value
of Integer: " + maxValue);
}
}
Question 2:
Write a Java program that uses wrapper
classes to calculate the sum of two double
values entered by the user.
Solution 2:
import java.util.Scanner;
public class DoubleSumExample
{
public static void main(String[] args) {
// Using wrapper classes to calculate
the sum of two double values
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the first
double value: ");
Double num1 =
scanner.nextDouble();
System.out.print("Enter the second
double value: ");
Double num2 =
scanner.nextDouble();
Double sum = num1 + num2;
// Displaying the result
System.out.println("Sum of the two
double values: " + sum);
}
}
Question 3:
Write a Java program to demonstrate
autoboxing and unboxing for a boolean
value.
Solution 3:
public class BooleanAutoboxingExample
{
public static void main(String[] args) {
// Autoboxing: converting boolean to
Boolean
Boolean boolObj = true;
// Unboxing: converting Boolean to
boolean
boolean boolValue = boolObj;
// Displaying the results
System.out.println("Autoboxing: " +
boolObj);
System.out.println("Unboxing: " +
boolValue);
}
}
Question 4:
Write a Java program to convert an int
value to a String using the Integer
wrapper class.
Solution 4:
public class
IntToStringConversionExample
{
public static void main(String[] args) {
// Converting int to String using
Integer wrapper class
int intValue = 42;
String stringValue =
Integer.toString(intValue);
// Displaying the result
System.out.println("Converted String
value: " + stringValue);
}
}
Question 5:
Write a Java program to demonstrate the
usage of wrapper classes in a collection
(e.g., ArrayList).
Solution 5:
import java.util.ArrayList;
public class
WrapperClassInCollectionExample
{
public static void main(String[] args) {
// Using wrapper classes in an
ArrayList
ArrayList<Integer> numbersList =
new ArrayList<>();
// Adding values using autoboxing
numbersList.add(10);
numbersList.add(20);
numbersList.add(30);
// Displaying the values
System.out.println("Numbers in the
ArrayList: " + numbersList);
// Accessing values using unboxing
int firstNumber = numbersList.get(0);
// Displaying the result
System.out.println("First number in
the ArrayList: " + firstNumber);
}
}
These simple Java programs cover
various aspects of wrapper classes,
including finding minimum and maximum
values, performing calculations,
demonstrating autoboxing and unboxing,
and using wrapper classes in a collection.
THE END