Question

22. Define a generic method which takes as parameters (1) a reference to an object of...

22. Define a generic method which takes as parameters (1) a reference to an object of a generic type and (2) an array of references to that generic type. Your method must loop through the array, and for the first reference in the array which is null, assign that reference to point to the same object as the reference parameter, then return. The method should have void as its return type.

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

=================================

JAVA PROGRAM

=================================

/////////////////////GenericTest.java/////////////////////////////

/**
* Class: GenericTest
* Purpose: To test theGenericMethod as defined
* @param <E>
*/
public class GenericTest<E> {
   //main mehtod to test theGenericMethod
   public static void main(String[] args){
       System.out.println("Test1: Testing generic method on type Integer..");
       //create GenericTest instance of Integer
       GenericTest<Integer> test1 = new GenericTest<Integer>();
       Integer intArr[] = new Integer[5]; //declare an intArr with capacity of 5
       //assign first 3 integers
       intArr[0] = 10;
       intArr[1] = 20;
       intArr[2] = 30;
       Integer theInt = 40; //reference integer
       test1.print(intArr); //print the original integer array
       System.out.println("Calling theGenericMethod for Integer... ");
       test1.theGenericMethod(theInt, intArr); //call generic method
       test1.print(intArr);//again print the changes contents of the array
      
       System.out.println("-----------------------------");
      
       System.out.println("Test2: Testing generic method on type String..");
       //create GenericTest instance of String
       GenericTest<String> test2 = new GenericTest<String>();
       String strArr[] = new String[5];//declare a strArr with capacity of 5
       //assign first 2 strings
       strArr[0] = "Apple";
       strArr[1] = "Banana";
       String theStr = "Mango"; //reference String
       test2.print(strArr); //print the original String array
       System.out.println("Calling theGenericMethod for String... ");
       test2.theGenericMethod(theStr, strArr);//call generic method
       test2.print(strArr);//again print the changes contents of the array
      
   }
  
   /**
   * this method is an additional method
   * to print the contents of the generic array
   * @param arr
   */
   public void print(E[] arr){
       System.out.println("The elements in array: ");
       for(E item: arr){
           System.out.print(item+" ");//print item
       }
       System.out.println();
   }
  
   /**
   * theGenericMethod takes as parameters (1) refObject of a generic type
   * and (2) theGenericArr generic type array.
   * The method must loop through the array, and for the first reference
   * in the array which is null, assign refObject to point to the same object
   * as the reference parameter, then return.
   * @param refObject
   * @param theGenericArr
   */
   public void theGenericMethod(E refObject,E[] theGenericArr){
       //traverse through theGenericArr
       for(int i = 0; i < theGenericArr.length; i++){
           E currentObj = theGenericArr[i];//get current item in array
           if(currentObj == null){ //if it is null
               theGenericArr[i] = refObject; //assign refObject to that index of array
               return;
           }
       }
   }

}

================================

OUTPUT

================================

Test1: Testing generic method on type Integer..
The elements in array:
10 20 30 null null
Calling theGenericMethod for Integer...
The elements in array:
10 20 30 40 null
-----------------------------
Test2: Testing generic method on type String..
The elements in array:
Apple Banana null null null
Calling theGenericMethod for String...
The elements in array:
Apple Banana Mango null null

Add a comment
Know the answer?
Add Answer to:
22. Define a generic method which takes as parameters (1) a reference to an object of...
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
  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • Write a generic method gReplace() that takes a 2D array of generic elements and two scalars...

    Write a generic method gReplace() that takes a 2D array of generic elements and two scalars of the same type as the array elements. The function must replace all occurrences of the first scalar with the second one. (scalar: given int x, x is a scalar of type int) with java

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

  • Write method createTeams() that Has no parameters Has return type void Instantiate the member variable of...

    Write method createTeams() that Has no parameters Has return type void Instantiate the member variable of type ArrayList<Team> Instantiates two instances of class Team one for TeamOne one for TeamTwo set names for each team as appropriate add each instance to the ArrayList<Team> member variable Instantiate the member variable of class Scanner passing “System.in” as the argument to the constructor Using System.out.println() static method, prompt the user for the human player’s name Instantiate an instance of class String set equal...

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

  • JAVA Write a static method that takes an array of a generic type as its only...

    JAVA Write a static method that takes an array of a generic type as its only argument. The method should display the array and return the number of elements in the array. Test the method in main with at least three arrays of objects. SAMPLE OUTPUT Here is an Integer array 12 21 7 16 8 13 That array held 6 elements Here is a String array one two three four That array held 4 elements Here is a Double...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Programming Problem: define a C++ Bucket class with the following methods: Bucket() constructor that takes two...

    Programming Problem: define a C++ Bucket class with the following methods: Bucket() constructor that takes two parameters, a string name and an int parameter, the bucket capacity print) takes no paramers. Prints the bucket name, capacity and contents. Returns void. fill) that takes no parameters, fills the bucket to capacity. Returns void. empty ) that takes no parameters, empties the bucket. Returns void pourinto) takes one parameter, a reference to a Bucket object, returns an int that is the number...

  • Program Overview This brief exercise is designed for you to consider how reference variables behave when...

    Program Overview This brief exercise is designed for you to consider how reference variables behave when you are passing them as arguments by-value. Instructions Name your class References.java. 1. Implement the following methods. 1.1 Method name: readStudentNames Purpose: This method accepts an array of strings as a parameter. It loops through the array and asks the user to input names. It populates the array of strings with the names. Parameters: an array of Strings, stringArray Return type: void In the...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

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
ADVERTISEMENT