Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class template called GenericVector. The class should have the followin

ID: 3575156 • Letter: C

Question

Create a class template called GenericVector. The class should have the following members: a) Private Data Member: vec (a vector of type parameter). b) Public Member Functions: push: This public function should accept an argument and insert its value at the end of the vector. (Hint: can use vector’s push_back method) last: This function should accept no argument and return the value of the last element of the vector. print: This function will print all the elements in vector. c) After you have created the class template, develop a test program (containing main() ) that will do the following: i. declare an object of GenericVector where the type parameter will be replaced by double. ii. enter at least 3 double values into the vec data member of object created in (i). iii. use the last function to print the last element of the object’s vec data member. iv. use the print function to print all elements of the object’s vec data member. use a stream manipulator to ensure that 2 digits are printed after decimal.

Explanation / Answer

// New class With name of GenericVector

package Sample;

import java.text.DecimalFormat;
import java.util.Vector;


public class GenricVector<E> {

   private Vector<E> vec = new Vector<E>();   // Creating a vector with type data type
   public void push(E element){               // Creating push method
       vec.add(element);
   }
  
  
   public E last(){                       // LAst method to get last element of vector
       return vec.lastElement();
   }
  
  
   public void print(){                   // print method to print elements in vector
       System.out.println(" Elements in vectors are:: ");
       for (E singleVec : vec){
           System.out.println(singleVec);
       }
   }
  
  
   public static void main(String []args){
          
       GenricVector<Double> newGenericObject = new GenricVector<Double>();       // Creating object of GnericVector
      
       newGenericObject.push(1.0);                   // Pushing sample values
       newGenericObject.push(2.0);
       newGenericObject.push(3.5566);
      
       Double lastElement = newGenericObject.last();   // Getting last element of vector
       DecimalFormat df = new DecimalFormat("#.##");    
       lastElement = Double.valueOf(df.format(lastElement));   // formatting double to get 2 digits from decimal
      
       System.out.println(" Last element of vector :: " + lastElement );
      
       newGenericObject.print();   // Printing the elements in vector
      
   }
  
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote