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
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
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