Question

In: Computer Science

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

JAVA

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

JAVA

The test cases for Integer and String is given in the code.

Please add test cases for Character, Boolean and Double type etc.

// 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());

    }

}

Solutions

Expert Solution

// 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 ("integer : "+iObj.getObject ());
    
 
 
 
      // instance of String type
      
Test < String > sObj = new Test < String > ("Print this string");
    
 
System.out.println ("String :" +sObj.getObject ());
    
 
 //instance of Character type
Test < Character > cObj = new Test < Character > ('C');
    
 
System.out.println ("Character : "+cObj.getObject ());
    

//instance of Double type
Test < Double > dObj = new Test < Double > (3.1454685222);

System.out.println ("Double : "+dObj.getObject ());


//instance of Boolean type    
Test < Boolean > bObj = new Test < Boolean > (true);
    
 
System.out.println ("Boolean : "+bObj.getObject ());
    
 //instance of Float type
Test < Float > fObj = new Test < Float > (1.23456f);
    
 
System.out.println ("Float : "+fObj.getObject ());
    
 
 //instance of Byte type
 
Test < Byte > byObj = new Test < Byte > (Byte.MAX_VALUE);
    
 
System.out.println ("Byte : "+byObj.getObject ());
 
 
//instance of Long type   
Test < Long > lObj = new Test < Long > (21000000000L);
    
 
System.out.println ("Long : "+lObj.getObject ());

 
 
} 
 
}

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


Related Solutions

Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is to fill in the code marked as "...your code here...". A detailed explanation follows the code. import java.util.*; public class OddManOut {    public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("How many random Integers to produce?"); int num = sc.nextInt();    ArrayList<Integer> randomInts = createRandomList(num); System.out.println("The random list is: "); System.out.println(randomInts);    removeOdds( randomInts ); System.out.println("The random list with only...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any other C++ IDE and run it. You will have to implement a small intentional bug in your program // This program uses a function that returns a value. #include <iostream> using namespace std; // Function prototype int sum(int num1, int num2); int main() {    int value1 = 20,   // The first value        value2 = 40,   // The second value        total;         //...
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java...
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java SE version only). If you do not have the latest Java installation to match, Netbeans will direct you to download the needed version of Java. If needed, download and install the latest Oracle Official Java Software Development Kit Standard Edition (Java SDK SE), and JUnit. I suggest you do this via the bundle referred to as The Java Development Kit (JDK). Download the architecture...
4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp...
4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp file. Compile and run. You will see that it is not showing output for Base class pointer to base class object data, not showing derived class output completely(missing word "Saving" in the output) and not showing any output for Base class pointer to derived class object(Saving). Fix these issue and submit the output. // AccountPolymorphism.cpp : This file contains the 'main' function. Program execution...
Write in java The submission utility will test your code against a different input than the...
Write in java The submission utility will test your code against a different input than the sample given. When you're finished, upload all of your .java files to Blackboard. Grading: Each problem will be graded as follows: 0 pts: no submission 1 pts: submitted, but didn't compile 2 pts: compiled, but didn't produce the right output 5 pts: compiled and produced the right output Problem 1: "Letter index" Write a program that inputs a word and an unknown number of...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution. Develop three functions for temperature-conversion. Create a function called F_to_K that converts and return temperatures in Fahrenheit to Kelvin and store results in ‘F_to_K2.txt’. Create a function called C_to_R that converts and return temperatures in Celsius to Rankine and store results in ‘C_to_R2.txt’. Create a function called C_to_F that...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution. Write a user defined function ‘My_FunctionGen’. It accepts, the time vector ‘t’ with 8000 uniformly spaced values within the range of 0 to 8, Frequecy scalars ‘f1<100H’ and ‘f2<80Hz’ and Amplitude scalars ‘A1’, ‘A2’ and ‘A3’ as input arguments. It delivers x1, x2 and x3 and x4 as outputs...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution. Generate the following vectors and also implement the Table-1 operations-map by employing the switch statement and an appropriate loop. Generate a vector ‘DEC1’ as follow. DEC1 = 5     1     3     0     2    5     0     2 Concatenate vector ‘DEC1’ eight times in order to obtain a bigger row vector ‘DEC’...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your...
Write a MATLAB code to obtain the following. Keep your code commented whenever required. Copy your source code and command widow outcomes and screen shots of any plots in your solution. Develop three functions for temperature-conversion. Create a function called F_to_K that converts and return temperatures in Fahrenheit to Kelvin and store results in ‘F_to_K2.txt’. Create a function called C_to_R that converts and return temperatures in Celsius to Rankine and store results in ‘C_to_R2.txt’. Create a function called C_to_F that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT