Question

Copy the the code below into your IDE or an online compiler and test an additional...

Copy the the code below into your IDE or an online compiler and test an additional type with the generic class. Submit your code and execution display. IN JAVA

// A Simple Java program to show working of user defined

// Generic classes

   

// We use < > to specify Parameter type

class Test<T>

{

    // An object of type T is declared

    T obj;

    Test(T obj) {  this.obj = obj;  }  // constructor

    public T getObject()  { return this.obj; }

}

// Driver class to test above

public class Main

{

    public static void main (String[] args)

    {

        // instance of Integer type

        Test <Integer> iObj = new Test<Integer>(22);

        System.out.println(iObj.getObject());

   

        // instance of String type

        Test <String> sObj =

                          new Test<String>("Print this string");

        System.out.println(sObj.getObject());

    }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// A Simple Java program to show working of user defined

// Generic classes


// We use < > to specify Parameter type

class Test<T>

{

// An object of type T is declared

T obj;

Test(T obj) { this.obj = obj; } // constructor

public T getObject() { return this.obj; }

}

-----------------------------------------------------------------------------------

// Driver class to test above

public class Main

{

public static void main (String[] args)

{

// instance of Integer type

Test <Integer> iObj = new Test<Integer>(22);

System.out.println(iObj.getObject());


// instance of String type

Test <String> sObj =

new Test<String>("Print this string");

System.out.println(sObj.getObject());

Test <Double> dObj = new Test<Double>(22342.5);

System.out.println(dObj.getObject());

}

}

-----------------------------------------------------------------------------

SEE OUTPUT, Created for Type DOUBLE

Thanks, PLEASE UPVOTE

Add a comment
Know the answer?
Add Answer to:
Copy the the code below into your IDE or an online compiler and test an additional...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Show the output of running the class Test in the following code lines: interface A {...

    Show the output of running the class Test in the following code lines: interface A { void print (); } class C ( class B extends C implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); w class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b...

  • Complete StackArray.java code below. Use StackArrayDemo.java to test your implementation of StackArray.java. StackArray.java below. public class...

    Complete StackArray.java code below. Use StackArrayDemo.java to test your implementation of StackArray.java. StackArray.java below. public class StackArray <T> { public static int CAPACITY = 100; private final T[] elements; private int topIndex; // Constructor public StackArray() { // Initialize elements // Initialize topIndex to -1 } public T peek() { // If topIndex is less than zero, return null. // Otherwise, return element from top of the stack. } public T pop() { // If topIndex is less than zero,...

  • Ok So I have the code I need! and it runs on online codechef IDE! The...

    Ok So I have the code I need! and it runs on online codechef IDE! The code is below: import java.util.Scanner; import java.io.FileWriter; public class Main { public static void main(String[] args) {    Scanner sc = new Scanner(System.in);    double sales; System.out.print("Enter annual sales: "); sales = sc.nextDouble();    double fixed = 30000;    double commission = (7.0*sales)/100.0;    double total = fixed + commission;    System.out.println("Fixed Salary: "+fixed); System.out.println("Annual Sales: "+sales); System.out.println("Commission: "+commission); System.out.println("Total Salary: "+total);      ...

  • Comment your code. At the top of the program include your name, a brief description of...

    Comment your code. At the top of the program include your name, a brief description of the program and what it does and the due date. The program must be written in Java and submitted via D2L. The code matches the class diagram given above. The code uses Inheritance. In the following, you are given code for two classes: Coin and TestCoin. You study these classes first and try to understand the meaning of every line. You can cut and...

  • given the following two classes Within a file named Rational.java, define a public class named Rational...

    given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...

  • Show the output of running the class Test in the following code lines: a) Nothing. b)...

    Show the output of running the class Test in the following code lines: a) Nothing. b) b is an instance of A followed by b is an instance of c c) b is an instance of C d) b is an instance of A interface A { void print (); } class C {} class B extends c implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • What is the output of running class C? The three Java classes are in separate Java...

    What is the output of running class C? The three Java classes are in separate Java files in the same directory (or in the same package). class A { public AO { System.out.println("The default constructor of A"); } // end A constructor } // end class A class B extends A { public BCString s) { System.out.println(s); } // end B constructor } // end class B public class C { public static void main(String[] args) { B b =...

  • 1. What does a Java compiler do? Select one: a. Runs Java programs b. Translates byte...

    1. What does a Java compiler do? Select one: a. Runs Java programs b. Translates byte code in ".class" files into machine language c. Translates source code in ".class" files into machine language d. Translates source code in ".java" files into Java byte code in ".class" files e. Translates source code in ".java" files into machine language 2. A subclass will _____ one superclass. Select one: a. abstract b. extend c. implement d. inherit e. override 3. Consider the following...

  • Analyze the following code: public class Test { public int x; public Test(String t) { System.out.println("Test");...

    Analyze the following code: public class Test { public int x; public Test(String t) { System.out.println("Test"); public static void main(String[] args) { Test test: System.out.println(test.x); The program has a compile error because Test class does not have a default constructor The program has a compile error because test is not initialized OO The program has a compile error because x has not been initialized The program has a runtime NullPointerException while executing test.x because test is a null reference and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT