Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the output of the code is in integer value how would you fix it in order for the code to output hex value public ReallyLongHex

the output of the code is in integer value how would you fix it in order for the code to output hex value

image text in transcribedimage text in transcribedimage text in transcribed

public ReallyLongHex subtract(ReallyLongHex rightOp)

{

int digits = this.getLength();

Node thisIndex = firstNode;

Node rOpIndex = rightOp.firstNode;

ReallyLongInt diff = new ReallyLongInt();

int carry = 0;

//Add each term in the integers

for(int i = 1; i

{

if(thisIndex == null)

{

throw new ArithmeticException("Invalid Difference -- Negative Number");

}

else if(rOpIndex == null)

{

Integer term = new Integer(thisIndex.data);

diff.add(1 , term);

}

else

{

int indexDiff = thisIndex.data - rOpIndex.data - carry;

if (indexDiff

{

carry = 1;

int ones = 10 + indexDiff;

Integer term = new Integer(ones);

diff.add(1 , term);

}

else

{

Integer term = new Integer(indexDiff);

diff.add(1 , term);

}

thisIndex = thisIndex.next;

rOpIndex = rOpIndex.next;

}

if(carry == 1) throw new ArithmeticException("Invalid Difference -- Negative Number");

diff.reverse();

return diff;

}

if(carry == 1) throw new ArithmeticException("Invalid Difference -- Negative Number");

diff.reverse();

return null;

}

public ReallyLongHex subtract (ReallylongHex rightop) Return a NEW ReallyLongHex that is the difference of the current ReallyLongHex and the parameter ReallyLongHex. Since ReallyLongHex is specified to be non-negative, if rightOp is greater than the current ReallyLongHex, you should throw an ArithmeticException. Otherwise, subtract digit by digit (borrowing if necessary) as expected. As with the add() method, you must implement this efficiently via a single traversal of both lists (after reversing the numbers). This method is tricky because it can result in leading zeros, which we don't want. Be careful to handle this case (and consider the tools provided by LinkedDS that will allow you to handle it). For example: ReallyLongHex X = new ReallyLongHex (" 123456") ; ReallyLongHex Y = new ReallyLongHex (" 123455") ; ReallyLongHex Z; z = x.subtract(Y) ; system . out.println(X + " - " + + " = " + Z); should produce the output: 0x123456 - 0x1234550x1 As with the add() method, be careful to handle numbers with differing numbers of digits. Also note that borrowing may extend over several digits. See RLHTest.java for some example cases public ReallyLongHex subtract (ReallylongHex rightop) Return a NEW ReallyLongHex that is the difference of the current ReallyLongHex and the parameter ReallyLongHex. Since ReallyLongHex is specified to be non-negative, if rightOp is greater than the current ReallyLongHex, you should throw an ArithmeticException. Otherwise, subtract digit by digit (borrowing if necessary) as expected. As with the add() method, you must implement this efficiently via a single traversal of both lists (after reversing the numbers). This method is tricky because it can result in leading zeros, which we don't want. Be careful to handle this case (and consider the tools provided by LinkedDS that will allow you to handle it). For example: ReallyLongHex X = new ReallyLongHex (" 123456") ; ReallyLongHex Y = new ReallyLongHex (" 123455") ; ReallyLongHex Z; z = x.subtract(Y) ; system . out.println(X + " - " + + " = " + Z); should produce the output: 0x123456 - 0x1234550x1 As with the add() method, be careful to handle numbers with differing numbers of digits. Also note that borrowing may extend over several digits. See RLHTest.java for some example cases

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions