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;         //...
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 and post it for other students to debug. To be able to receive your full discussion points, you need to submit the following. Following is your check list and rubric       Attach your .cpp file/s with an implemented bug - 20pnts       Describe what the code...
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...
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
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...
Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste...
Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste the whole solution I was left with. Thank you in advance ! Lab 5 – Class Statistics Write a program which will uses the file Lab5Data.txt containing student names and the points they had earned at the end of the class. The program should use one or more arrays to create a report with the following information: -A table containing the student name, total...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT