Question: how would you make the code return hex instead of int? Java public ReallyLongHex add(ReallyLongHex rightOp) { //Get greatest number of digits int comparison =

how would you make the code return hex instead of int? Java

how would you make the code return hex instead of int? Java

public ReallyLongHex add(ReallyLongHex rightOp) { //Get greatest number of digits int comparison = this.compareTo(rightOp); int digits; if(comparison == 1) digits = this.getLength(); else digits = rightOp.getLength(); Node thisIndex = firstNode; Node rOpIndex = rightOp.firstNode; ReallyLongInt sum = new ReallyLongHex(); Node sumIndex = null; //Holder for carry digits when sum of to individual int is above 9 int tens = 0; //Add each term in the integers for(int i = 0; i 9) { int ones = indexSum % 10; term = new Integer(ones); tens = 1; } else term = new Integer(indexSum); //Initialze sum if sum hasn't been intialized yet if(sum.isEmpty() && sumIndex == null) { sum.firstNode = new Node(term); sumIndex = sum.firstNode; } else { sumIndex.next = new Node(term); sumIndex = sumIndex.next; } thisIndex = thisIndex.next; rOpIndex = rOpIndex.next; } sum.numberOfEntries++; } //Add 10's if there are leftover digits if(tens == 1) { System.out.println(sumIndex.data); Integer term = new Integer(tens); sumIndex.next = new Node(term); sum.numberOfEntries++; } return sum; }

To help you out with the assignment, we have implemented the methods above for you, with comments. See the code in ReallyLongHex.java. 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: 0x1 2 3 4 5 6789 + 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!