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.
Introduction to generic programming
Implementing a type-safe Box class and retrieving values
Report a typo
Sample Input 1:
5Sample Output 1:
5Sample Input 2:
helloSample Output 2:
helloWrite 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
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.