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...
For each pair of functions f and g, write whether f is in O(g), Ω(g), or...
For each pair of functions f and g, write whether f is in O(g), Ω(g), or Θ(g). (latex format below)        \begin{enumerate}            \item $f = (n+1000)^4$, $g = n^4 - 3n^3$            \item $f = \log_{1000} n$, $g = \log_2 n$            \item $f = n^{1000}$, $g = n^2$            \item $f = 2^n$, $g = n!$            \item $f = n^n$, $g = n!$        \end{enumerate}
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 a Racket function "combine" that takes two functions, f and g, as parameters and evaluates...
Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define (h x) (sub1 (add1 x))). You will need to use...
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’...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT