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

Step 1 The Pair Class Many times in writing software we come across problems tha

ID: 3788651 • Letter: S

Question

Step 1 The Pair Class Many times in writing software we come across problems that require us of values. For to store a pair example, a coordinate system would require an x, y value pair or a hash key value pair Your task on this assignment is table as a data, one type but to create a simple template pair class called Pair that has two type parameters. You should call these parameters F and s. Functionality Constructor You should create a working constructor that takes two type parameters one for each of the pair values. get First This is an accessor method that will return the first of the pair values. get second This is an accessor method that will return the second of the pair values. set First This is a mutator method that will set the first pair value setsecond This is a mutator method that will set the second pair value

Explanation / Answer

Pair.java

public class Pair<F, S> {
private F first; //first member of pair
private S second; //second member of pair

public Pair(F first, S second) {
this.first = first;
this.second = second;
}

public void setFirst(F first) {
this.first = first;
}

public void setSecond(S second) {
this.second = second;
}

public F getFirst() {
return first;
}

public S getSecond() {
return second;
}
}

LinkedList.java

template <class T> class Node
{
private T data; //the object information
private Node* next; //pointer to the next node element

public void setData(T val)
{
this.data = val;
}
public void setNextNull()
{
this.next = NULL;
}
public void setNext(Node *tmp)
{
this.next = tmp;
}
public Node* getNext()
{
return this.next;
}
};

template <class T> class LinkedList extends Node
{
private Node<T> *head = new Node<T>;
private Node<T> *tail = new Node<T>;

public LinkedList()
{
head = NULL;
tail = NULL;
}

public ~LinkedList()
{

}

//Method adds info to the end of the list
public void append(T info)
{
if(head == NULL) //if our list is currently empty
{
head = new Node<T>; //Create new node of type T
head->setData(info);
tail = head;
}
else //if not empty add to the end and move the tail
{
Node* temp = new Node<T>;
temp->setData(info);
temp->setNextNull();
tail->setNext(temp);
tail = tail->getNext();
}
}
public void addFront(T info)
{
if(head == NULL) //if our list is currently empty
{
head = new Node<T>; //Create new node of type T
head->setData(info);
tail = head;
}
else //if not empty add to the end and move the tail
{
Node* temp = new Node<T>;
temp->setData(info);
temp->setNext(head);
head = temp;
}
}

//insert after given node
public void insert(Node *node,T info)
{
Node *temp = new Node<T>;
temp->setData(info);
temp->setNext(node->getNext());
node->setNext(temp);
}
public void delete(Node *node)
{
Node *temp = new Node<T>;
temp = head;
while(temp->getNext() != node && temp->getNext() )
{
temp = temp->getNext();
}
if(temp)
{
temp->setNext(node->getNext());
delete node;
}
}
public Integer size(Node *head)
{
int sz=0;
Node * temp = new Node<T>;
temp = head;
while(temp)
{
sz++;
temp = temp->getNext();
}
return sz;
}
}

PairList.java

public class PairList< E, F > extends LinkedList< Pair< E, F > >
{
/**
* Basic constructor
*/
public PairList()
{
super();
}

/**
* Constructor from two lists
*
* @param eList List of the first object class
* @param fList list of the second object class
*/
public PairList( LinkedList< E > eList, LinkedList< F > fList )
{
super();
if( eList.size() != fList.size() )
{
throw new GameException( 4, "Pair list sizes do not match: e(" +
eList.size() + "), f(" + fList.size() +
")" );
}

ListIterator< E > eIter = eList.listIterator();
ListIterator< F > fIter = fList.listIterator();

while( eIter.hasNext() )
{
this.add( new Pair<>( eIter.next(), fIter.next() ) );
}
}

/**
* Constructor from two arrays
*
* @param eArray Array of the first object class
* @param fArray Array of the second object class
*/
public PairList( E[] eArray, F[] fArray )
{
super();

if( eArray.length != fArray.length )
{
throw new GameException( 4, "Pair list sizes do not match: e(" +
eArray.length + "), f(" + fArray.length +
")" );
}

for( int i = 0; i < eArray.length; i++ )
{
this.add( new Pair<>( eArray[ i ], fArray[ i ] ) );
}
}


/**
* Get a list of all the first object class
*
* @return 1-D list of the first object class
*/
public LinkedList< E > get1()
{
LinkedList< E > ret = new ArrayList<>( this.size() );
ListIterator< Pair< E, F > > iter = this.listIterator();

while( iter.hasNext() )
{
ret.add( iter.next().get1() );
}

return ret;
}

/**
* Get a list of all the second object class
*
* @return 1-D list of the second object class
*/
public LinkedList< F > get2()
{
LinkedList< F > ret = new ArrayList<>( this.size() );
ListIterator< Pair< E, F > > iter = this.listIterator();

while( iter.hasNext() )
{
ret.add( iter.next().get2() );
}

return ret;
}
}

It may not compile at your local system as I create template instead of generic class So It is just for understanding as per your question.

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