Note: The Program should be written in Java Language: General Instructions Write
ID: 3735449 • Letter: N
Question
Note: The Program should be written in Java Language:
General Instructions Write a program which will add digit strings. The input strings will be read Byte objects. The program will evaluate the sum of each byte pair. The program will write each input pair as well as the results to an output file according to the format specified below. The results will include meaningful messages about erroneous string input or addition overflow when applicable. The program will continue to read and evaluate strings until the end of the input file is reached. You will need to define Byte as a class. The input format is given at the bottom of the page. Special Instructions This assignment should be written as a two class Java program. You will develop a class called Byte. The main program should be in its own class with a main method. You are to handle additions of both positive and negative binary strings with the following assumptions: the first string is stored using two's complement and the second using 8-bit biased notation. The result should be a bit string in two's complement (including a decimal interpretation of the result). To keep track of overflow keep the following scenarios in mind: 1) 2) 3) Adding a positive and a negative cannot create overflow. Adding two positives can create overflow, indicated when the highest order bit of the result is 1 after the operation Adding two negatives can create underflow, indicated when the highest order bit of the result is 0 after the operationExplanation / Answer
Please find the code below with detailed inline comments.
CODE
==================
DLLNode.java
*******************************
// Implements nodes holding info of class <T> for a doubly linked list.
//----------------------------------------------------------------------------
public class DLLNode<T>
{
private T info;
private DLLNode<T> forward, back;
public DLLNode(T info)
{
this.info = info; forward = null; back = null;
}
public void setInfo(T info){this.info = info;}
public T getInfo(){return info;}
public void setForward(DLLNode<T> forward){this.forward = forward;}
public void setBack(DLLNode<T> back){this.back = back;}
public DLLNode getForward(){return forward;}
public DLLNode getBack(){return back;}
}
LargeIntlist.java
********************************
import java.util.Iterator;
public class LargeIntList
{
protected DLLNode<Byte> listFirst; // Ref to the first node on list
protected DLLNode<Byte> listLast; // Ref to the last node on the list
protected int numElements; // Number of elements in the list
public LargeIntList()
// Creates an empty list object
{
numElements = 0;
listFirst = null;
listLast = null;
}
public int size()
// Returns the number of elements on this list.
{
return numElements;
}
public Iterator<Byte> forward()
// Returns an Iterator that iterates from front to rear.
{
return new Iterator<Byte>()
{
private DLLNode<Byte> next = listFirst; // next node to return
public boolean hasNext()
// Returns true if the iteration has more elements; otherwise false.
{
return (next != null);
}
public Byte next()
// Returns the next element in the iteration.
// Throws NoSuchElementException - if no more elements
{
if (!hasNext())
throw new IndexOutOfBoundsException("Illegal invocation of " +
" next in LargeIntList forward iterator. ");
Byte hold = next.getInfo(); // holds info for return
next = next.getForward();
return hold;
}
public void remove()
// Throws UnsupportedOperationException.
{
throw new UnsupportedOperationException("Unsupported remove " +
"attempted on LargeIntList forward iterator.");
}
};
}
public Iterator<Byte> reverse()
// Returns an Iterator that iterates rear to front.
{
return new Iterator<Byte>()
{
private DLLNode<Byte> next = listLast; // next node to return
public boolean hasNext()
// Returns true if the iteration has more elements; otherwise false.
{
return (next != null);
}
public Byte next()
// Returns the next element in the iteration.
// Throws NoSuchElementException - if no more elements
{
if (!hasNext())
throw new IndexOutOfBoundsException("Illegal invocation of " +
"next in LargeIntList reverse iterator. ");
Byte hold = next.getInfo(); // holds info for return
next = next.getBack();
return hold;
}
public void remove()
// Throws UnsupportedOperationException.
{
throw new UnsupportedOperationException("Unsupported remove " +
"attempted on LargeIntList forward iterator.");
}
};
}
public void addFront (byte element)
// Adds the value of element to the beginning of this list
{
DLLNode<Byte> newNode = new DLLNode<Byte>(element); // node being added
newNode.setForward(listFirst);
newNode.setBack(null);
if (listFirst == null) // Adding into an empty list
{
listFirst = newNode;
listLast = newNode;
}
else // Adding into a non-empty list
{
listFirst.setBack(newNode);
listFirst = newNode;
}
numElements++;
}
public void addEnd (byte element)
// Adds the value of element to the end of this list
{
DLLNode<Byte> newNode = new DLLNode<Byte>(element); // node being added
newNode.setForward(null);
newNode.setBack(listLast);
if (listFirst == null) // Adding into an empty list
{
listFirst = newNode;
listLast = newNode;
}
else // Adding into a non-empty list
{
listLast.setForward(newNode);
listLast = newNode;
}
numElements++;
}
}
LargeInt.java
******************************
import java.util.Iterator;
public class LargeInt
{
protected LargeIntList numbers; // Holds digits
// Constants for sign variable
protected static final boolean PLUS = true;
protected static final boolean MINUS = false;
protected boolean sign;
public LargeInt()
// Instantiates an "empty" large integer.
{
numbers = new LargeIntList();
sign = PLUS;
}
public LargeInt(String intString)
// Precondition: intString contains a well-formatted integer
//
// Instantiates a large integer as indicated by intString
{
numbers = new LargeIntList();
sign = PLUS;
int firstDigitPosition; // Position of first digit in intString
int lastDigitPosition; // Position of last digit in intString
// Used to translate character to byte
char digitChar;
int digitInt;
byte digitByte;
firstDigitPosition = 0;
if (intString.charAt(0) == '+') // Skip leading plus sign
firstDigitPosition = 1;
else
if (intString.charAt(0) == '-') // Handle leading minus sign
{
firstDigitPosition = 1;
sign = MINUS;
}
lastDigitPosition = intString.length() - 1;
for (int count = firstDigitPosition; count <= lastDigitPosition; count++)
{
digitChar = intString.charAt(count);
digitInt = Character.digit(digitChar, 10);
digitByte = (byte)digitInt;
numbers.addEnd(digitByte);
}
}
public void setNegative()
{
sign = MINUS;
}
public String toString()
{
Byte element;
String largeIntString;
if (sign == PLUS)
largeIntString = "+";
else
largeIntString = "-";
int count = numbers.size();
Iterator<Byte> forward = numbers.forward();
while (forward.hasNext())
{
element = forward.next();
largeIntString = largeIntString + element;
if ((((count - 1) % 3) == 0) && (count != 1))
largeIntString = largeIntString + ",";
count--;
}
return(largeIntString);
}
protected static boolean greaterList(LargeIntList first,
LargeIntList second)
// Precondition: first and second have no leading zeros
//
// Returns true if first represents a larger number than second;
// otherwise, returns false
{
boolean greater = false;
if (first.size() > second.size())
greater = true;
else
if (first.size() < second.size())
greater = false;
else
{
byte digitFirst;
byte digitSecond;
Iterator<Byte> firstForward = first.forward();
Iterator<Byte> secondForward = second.forward();
// Set up loop
int length = first.size();
boolean keepChecking = true;
int count = 1;
while ((count <= length) && (keepChecking))
{
digitFirst = firstForward.next();
digitSecond = secondForward.next();
if (digitFirst > digitSecond)
{
greater = true;
keepChecking = false;
}
else
if (digitFirst < digitSecond)
{
greater = false;
keepChecking = false;
}
count++;
}
}
return greater;
}
protected static LargeIntList addLists(LargeIntList larger,
LargeIntList smaller)
// Precondition: larger > smaller
//
// Returns a specialized list that is a byte-by-byte sum of the two
// argument lists
{
byte digit1;
byte digit2;
byte temp;
byte carry = 0;
int largerLength = larger.size();
int smallerLength = smaller.size();
int lengthDiff;
LargeIntList result = new LargeIntList();
Iterator<Byte> largerReverse = larger.reverse();
Iterator<Byte> smallerReverse = smaller.reverse();
// Process both lists while both have digits
for (int count = 1; count <= smallerLength; count++)
{
digit1 = largerReverse.next();
digit2 = smallerReverse.next();
temp = (byte)(digit1 + digit2 + carry);
carry = (byte)(temp / 10);
result.addFront((byte)(temp % 10));
}
// Finish processing of leftover digits
lengthDiff = (largerLength - smallerLength);
for (int count = 1; count <= lengthDiff; count++)
{
digit1 = largerReverse.next();
temp = (byte)(digit1 + carry);
carry = (byte)(temp / 10);
result.addFront((byte)(temp % 10));
}
if (carry != 0)
result.addFront((byte)carry);
return result;
}
protected static LargeIntList subtractLists(LargeIntList larger,
LargeIntList smaller)
// Precondition: larger >= smaller
//
// Returns a specialized list that is the difference of the two argument lists
{
byte digit1;
byte digit2;
byte temp;
boolean borrow = false;
int largerLength = larger.size();
int smallerLength = smaller.size();
int lengthDiff;
LargeIntList result = new LargeIntList();
Iterator<Byte> largerReverse = larger.reverse();
Iterator<Byte> smallerReverse = smaller.reverse();
// Process both lists while both have digits.
for (int count = 1; count <= smallerLength; count++)
{
digit1 = largerReverse.next();
if (borrow)
{
if (digit1 != 0)
{
digit1 = (byte)(digit1 - 1);
borrow = false;
}
else
{
digit1 = 9;
borrow = true;
}
}
digit2 = smallerReverse.next();
if (digit2 <= digit1)
result.addFront((byte)(digit1 - digit2));
else
{
borrow = true;
result.addFront((byte)(digit1 + 10 - digit2));
}
}
// Finish processing of leftover digits
lengthDiff = (largerLength - smallerLength);
for (int count = 1; count <= lengthDiff; count++)
{
digit1 = largerReverse.next();
if (borrow)
{
if (digit1 != 0)
{
digit1 = (byte)(digit1 - 1);
borrow = false;
}
else
{
digit1 = 9;
borrow = true;
}
}
result.addFront(digit1);
}
return result;
}
public static LargeInt add(LargeInt first, LargeInt second)
// Returns a LargeInt that is the sum of the two argument LargeInts
{
LargeInt sum = new LargeInt();
if (first.sign == second.sign)
{
if (greaterList(first.numbers, second.numbers))
sum.numbers = addLists(first.numbers, second.numbers);
else
sum.numbers = addLists(second.numbers, first.numbers);
sum.sign = first.sign;
}
else // Signs are different
{
if (greaterList(first.numbers, second.numbers))
{
sum.numbers = subtractLists(first.numbers, second.numbers);
sum.sign = first.sign;
}
else
{
sum.numbers = subtractLists(second.numbers, first.numbers);
sum.sign = second.sign;
}
}
return sum;
}
public static LargeInt subtract(LargeInt first, LargeInt second)
// Returns a LargeInt that is the difference of the two argument LargeInts
{
LargeInt diff = new LargeInt();
// Create an inverse of second
LargeInt negSecond = new LargeInt();
negSecond.sign = !second.sign;
Iterator<Byte> secondForward = second.numbers.forward();
int length = second.numbers.size();
for (int count = 1; count <= length; count++)
negSecond.numbers.addEnd(secondForward.next());
// Add first to inverse of second
diff = add(first, negSecond);
return diff;
}
public static LargeInt multiply(LargeInt first, LargeInt second) {
LargeInt prod = new LargeInt();
byte digit1;
byte digit2;
byte temp;
byte carry = 0;
int firstLength = first.numbers.size();
int secondLength = second.numbers.size();
Iterator<Byte> secondReverse = second.numbers.reverse();
// Process both lists while both have digits
for (int i = 1; i <= secondLength; i++)
{
digit2 = secondReverse.next();
LargeIntList result = new LargeIntList();
Iterator<Byte> firstReverse = first.numbers.reverse();
for(int j = 1; j <= firstLength; j++) {
digit1 = firstReverse.next();
temp = (byte)(digit1 * digit2 + carry);
carry = (byte)(temp / 10);
result.addFront((byte)(temp % 10));
}
if(carry != 0) {
result.addFront(carry);
carry = 0;
}
if(i != 1) {
for(int k = 1; k<i; k++)
result.addEnd((byte)0);
LargeInt t = new LargeInt();
t.numbers = result;
t.sign = PLUS;
prod = add(prod, t);
}
else
prod.numbers = result;
}
if(first.sign == second.sign)
prod.sign = PLUS;
else
prod.sign = MINUS;
return prod;
}
public static void main(String args[]) {
LargeInt int1 = new LargeInt("-100000");
LargeInt int2 = new LargeInt("-106");
LargeInt prod = multiply(int1, int2);
System.out.println("145272829 * -4338389 = " + prod);
}
}
Note: Pleass run the LargeInt.java file to get the desired output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.