Java Generic Class | Practice | GeeksforGeeks

Generic Class in java  is a feature. We write code once and use it for any data type including user defined data types. Given an integer or a string as an input.Print the type of input and the given value as output.
Input: 
The first line will contain an integer T (number of test cases). First line of each test case will contain one integer c. Next line will contain a string if c is equal to 1 or will contain a integer if c is equal to 2.
Output: 
Print the type of the given data and the given value in new line.
Constraints: 
1 < = T < = 100
1 < = c < = 2
1<= Length of strings, integers<=103
Example:
Input:
2
1
gfg
2
5
Output:
String
gfg
Integer
5
0
i2007816 months ago
public static <E> void generic (String string){          }
+1
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.19
public class GenericClass<t> {    private T res;        GenericClass(T res)    {        this.res = res;    }        void showType()    {        System.out.println(res.getClass().getSimpleName());        System.out.println(res);    }}
0
Ramakanta Jena1 year ago
Ramakanta Jena
//Add your code here. Make a private data variable, constructor which intialize
    private String e;    private int f;
    GenericClass (String e) {        this.e = e;    }
    GenericClass (int f) {        this.f = f;    }
    // the data variable and a method showType().
    void showType () {
        if (e != null) {
            System.out.println("String");            System.out.println(e);        } else if (f >= 0){            System.out.println("Integer");            System.out.println(f);        }    }
0
Tulsi Dey2 years ago
Tulsi Dey
Solution :Execution time : 0.25 https://uploads.disquscdn.c...
0
Saransh2 years ago
Saransh
public class GenericClass<t> {     private T value;     public GenericClass(T value) {        this.value = value;    }     public void showType() {        if(value instanceof Integer) {            System.out.println("Integer");            System.out.println(this.value);        } else {            System.out.println("String");            System.out.println(this.value);        }    } }
0
Dev Kumar2 years ago
Dev Kumar
public class GenericClass<t> {    private T t;    private String type;    GenericClass(T t){        this.t = t;        this.type = t.getClass().getSimpleName();    }    public void showType(){        System.out.println(type+"\n"+t);    }}
0
Dev Kumar2 years ago
Dev Kumar
...
0
Dev Kumar2 years ago
Dev Kumar
...
0
Wendy Wang3 years ago
Wendy Wang
class GenericClass<t> {private T value;private String type;
public GenericClass(T item) {// TODO Auto-generated constructor stubthis.value = item;this.type = item.getClass().getSimpleName();}
public void showType() {System.out.println(type + "\n" + value);}}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.