Implementing a type-safe Box class and retrieving values

Report a typo

Create a type-safe Box class that has a single instance variable for the type it's defined with, alongside an appropriate constructor. Implement a method named get that will return the value. Then, you will be provided with a string, integer or a float. Your task is to put the provided value into an object of Box and then print its value. Note that the box should be able to hold any data type. The input will be a single line of either string, integer or float and your output should be the same value.

Sample Input 1:

5

Sample Output 1:

5

Sample Input 2:

hello

Sample Output 2:

hello
Write a program in Java 25
import java.util.*;

public class Main {
static class Box<T> {
// your code here
}

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

if(sc.hasNextInt()) {
int num = sc.nextInt();
// your code here
} else if (sc.hasNextFloat()) {
float num = sc.nextFloat();
// your code here
} else {
String str = sc.next();
// your code here
}
}
}

Create a free account to access the full topic