Question

In: Computer Science

Write a generic class Pair which has two type parameters—F andS—each representing the type of...

Write a generic class Pair which has two type parameters—F and S—each representing the type of the first and second element of the pair, respectively. Add set and get methods for the first and second elements of the pair. (Hint: The class header should be public class Pair.)

Write a separate PairTest class to test class Pair. Create 2 Pair types and test your get and set methods on the following pair types:

  • Pair

    p1 takes Integer and String types as a pair
  • Pair

    p2 takes String and Integer

PairTest should output enough information to the terminal to show that your generic class Pair is able to set() and get() different types.

Solutions

Expert Solution

Pair.java

public class Pair {
  
   private F first;
   private S second;
   /**
   * @return the first element
   */
   public F getFirst() {
       return first;
   }
   /**
   * @param first the element first to set
   */
   public void setFirst(F first) {
       this.first = first;
   }
   /**
   * @return the second element
   */
   public S getSecond() {
       return second;
   }
   /**
   * @param second the element second to set
   */
   public void setSecond(S second) {
       this.second = second;
   }
}

PairTest.java

public class PairTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       /*Instance of Generic class Pair with parameters F-String and S-Integer*/
       Pair p1 = new Pair<>();
       p1.setFirst("Java");
       p1.setSecond(10);
      
       System.out.printf("The elements for First Pair are %s and %d.\n", p1.getFirst(),p1.getSecond());
      
       /*Instance of Generic class Pair with parameters F-Integer and S-String*/
       Pair p2 = new Pair<>();
       p2.setFirst(20);
       p2.setSecond("Python");

       System.out.printf("The elements for Second Pair are %d and %s.", p2.getFirst(),p2.getSecond());
   }

}

Output


Related Solutions

Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing...
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average). Use the following main function in driver1b.cpp as a starting...
Write the definition for a generic / Template class called time that has hours and minutes...
Write the definition for a generic / Template class called time that has hours and minutes as structure. The class has the following member functions: SetTime to set the specified value in object ShowTime to display time object Sum to sum two time object & return time Write the definitions for each of the above member functions. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Convert the string into a list (let us refer to it as lista). An element in the generated list is the position of the corresponding letter in the parameter string in the English alphabet. Example: ‘C’...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and test it using a stack of Car, Integer, and String Stack<E> For Programming Lab 6, you are to write your own Generic Collection for a Stack that will use your Node<E> from Lab 5 UML for Stack<E> Stack<E> - top : Node<E> - numElements : int + Stack( ) + push( element : E ) : void + pop( ) : E + size(...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and...
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method)...
Write the class Square that takes X,Y and Side as parameters (these are used in the...
Write the class Square that takes X,Y and Side as parameters (these are used in the __init__ method) Write the method draw that will draw the square at location X,Y of size Side from the __init__ parameters using turtle graphics. Instantiate the class. Call the draw method The result of the program's execution will be to draw a square. Good descriptive comments are vital. No comments, no grade. There should be comments after every line, and you need to explain...
Write in main • setArray() – This method has two integer parameters, one for the rows...
Write in main • setArray() – This method has two integer parameters, one for the rows of a two-dimensional array and the other for the columns of a two-dimensional array. The method will return a two-dimensional array of integers with rows and columns given by the parameters. In addition, the method will use the Random class, along with the nextInt() method, to give each element of the two-dimensional array a value between 1 and 20. • display() – This is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT