Question
I have added the Linked List class below to help with my question. In the ReallyLongHex class which extends the LinkedDS, I need help with
I have added the Linked List class below to help with my question. In the ReallyLongHex class which extends the LinkedDS, I need help with the add method to add two hex objects together. Even any type of pseudo code would be helpful.
Here is the direction I was given:
public ReallyLongHex add(ReallyLongHex rightOp)
Return a NEW ReallyLongHex that is the sum of the current ReallyLongHex and the parameter ReallyLongHex, without altering the original values. For example:
ReallyLongHex X = new ReallyLongHex("123456789");
ReallyLongHex Y = new ReallyLongHex("987654321");
ReallyLongHex Z;
Z = X.add(Y);
System.out.println(X + " + " + Y + " = " + Z);
should produce the output: 0x123456789 + 0x987654321 = 0xAAAAAAAAA
Be careful to handle carries correctly and to process the nodes in the correct order. Since the numbers are stored with the most significant digit at the beginning, the add() method can be implemented by first reversing the chains then traversing both chains in a systematic way. This must be done efficiently using references to traverse the lists. In other words, you should start at the beginning of each ReallyLongHex and traverse one time while doing the addition. The KEY is that for add() (and subtract()) you should access each Node in the list only 1 time TOTAL. This is true for other methods as well. Think 5 how you can do this with reference variables. Also, be careful to handle numbers with differing numbers of digits.
public class ReallyLongHex extends LinkedDS
implements Comparable
{
// Instance variables are inherited. You may not add any new instance variables
// Default constructor
public ReallyLongHex()
{
super();
}
// Note that we are adding the digits here in the END. This results in the
// MOST significant digit first in the chain.
// It is assumed that String s is a valid representation of an
// unsigned integer with no leading zeros.
public ReallyLongHex(String s)
{
super();
char c;
// Iterate through the String, getting each character and adding it
// at the end of the list.
for (int i = 0; i < s.length(); i++)
{
c = s.charAt(i);
if ((('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')))
{
this.addItem(c);
}
else throw new NumberFormatException("Illegal digit " + c);
}
}
// Simple call to super to copy the nodes from the argument ReallyLongHex
// into a new one.
public ReallyLongHex(ReallyLongHex rightOp)
{
super(rightOp);
}
// Method to put digits of number into a String. We traverse the chain
// to add the digits to a StringBuilder.
public String toString()
{
StringBuilder sb = new StringBuilder();
if (numOfEntries > 0)
{
sb.append("0x");
for (Node curr = firstNode; curr != null;
curr = curr.getNextNode())
{
sb.append(curr.getData());
}
}
return sb.toString();
}
// You must implement the methods below. See the descriptions in the
// assignment sheet
public ReallyLongHex add(ReallyLongHex rightOp)
{
return null;
}
public class LinkedDS
{
protected Node firstNode;
protected static int numOfEntries;
public LinkedDS()
{
initializeDataFields();
}
public LinkedDS(LinkedDS
{
//copys the oldList and cre
}
//PrimQ methods
public boolean addItem(T newEntry)
{
//adds new object to end of list
}
public T removeItem() //fix infinite loop
{
//removes and returns the oldest item
}
public boolean empty()
{
//checks if linked list is empty
}
public int size()
{
return numOfEntries;
}
public void clear()
{
initializeDataFields();
}
//Reorder Methods
public void reverse()
{
//reverses the nodes of the list
}
public void shiftRight()
{
//removes last item and puts it in the front
}
public void shiftLeft()
{
//removes first item and puts it last
}
public void leftShift(int num)
{
//shifts contents num places to the left deleting the items that are shifted out
}
public void rightShift(int num)
{
//shifts num places to right, deleting the num number of items at the end of the list
}
public void leftRotate(int num)
{
// shift the contents of the list num places to the left, but rather than removing nodes from the list you will simply change their ordering in a cyclic way.
}
public void rightRotate(int num) //does not work need to fix to move num Nodes not 1
{
//opposite of above in other direction
}
//Override toString
public String toString()
{
}
private void initializeDataFields()
{
firstNode = null;
numOfEntries = 0;
}
private Node getNodeAt(int givenPosition)
{
}
public class Node {
private T data; // entry in bag
private Node next; // link to next nodes
public Node(T dataPortion) {
this(dataPortion, null);
} // end constructor
public Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
public T getData() {
return data;
} // end getData
public void setData(T newData) {
data = newData;
} // end setData
public Node getNextNode() {
return next;
} // end getNextNode
public void setNextNode(Node nextNode) {
next = nextNode;
} // end setNextNode
} // end Node
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started