Question
Please complete addition, subtraction, and comparison logic as shown with SpecializedList below. public class SpecializedList implements SpecializedListInterface { SListNode head; SListNode tail; private int size;
Please complete addition, subtraction, and comparison logic as shown with SpecializedList below.
public class SpecializedList implements SpecializedListInterface {
SListNode head; SListNode tail; private int size; SListNode forward; private SListNode backward;
public SpecializedList() { head = null; tail = null; size = 0; forward = null; backward = null; }
public void resetForward() { forward = head; }
public byte getNextItem() { byte item = forward.data; forward = forward.next; if (forward == null) { forward = head; } return item; }
public void resetBackward() { backward = tail; }
public byte getPriorItem() { byte item = backward.data; backward = backward.previous; if (backward == null) { backward = tail; } return item; } public int lengthIs() { return size; }
public void insertFront(byte item) { SListNode node = new SListNode(item); if (head == null) { head = node; tail = node; } else { node.next = head; head.previous = node; head = node; } size++; }
public void insertEnd(byte item) { SListNode node = new SListNode(item); if (tail == null) { head = node; tail = node; } else { node.previous = tail; tail.next = node; tail = node; } size++; }
public class SListNode { public byte data; public SListNode next; public SListNode previous;
public SListNode(byte data) { this.data = data; this.next = null; this.previous = null; } } }
import a1.SpecializedList.SListNode;
public class LargeInt implements LargeIntInterface { private SpecializedList underlyingList; private boolean isNegative;// assign public LargeInt(String input) { this.underlyingList = new SpecializedList(); this.isNegative = false; if (input.charAt(0) == '-') { this.isNegative = true; input = input.substring(1); } for (int i = input.length() - 1; i >= 0; i--) { this.underlyingList.insertFront((byte) (input.charAt(i) - '0')); } } @Override public LargeInt add(LargeInt other) { LargeInt result = new LargeInt("0"); result.underlyingList = addLists(this.underlyingList, other.underlyingList); if (this.isNegative == other.isNegative) { result.isNegative = this.isNegative; } else { result.isNegative = (compareLists(this.underlyingList, other.underlyingList) < 0) ? this.isNegative : other.isNegative; } return result; } @Override public LargeInt subtract(LargeInt other) { LargeInt result = new LargeInt("0");// assign 0 to the LargeInt. result.underlyingList = subtractLists(this.underlyingList, other.underlyingList); if (this.isNegative == other.isNegative) { result.isNegative = (compareLists(this.underlyingList, other.underlyingList) < 0) ? other.isNegative : this.isNegative; } else { result.isNegative = this.isNegative; } return result; } @Override public void setNegative() { this.isNegative = !this.isNegative; } @Override public String toString() { StringBuilder sb = new StringBuilder(); if (this.isNegative) { sb.append("-"); } for (SListNode node = this.underlyingList.head ; node != null; node = node.next) { sb.append(node.data); } return sb.toString(); } private SpecializedList addLists(SpecializedList list1, SpecializedList list2) { // Addition logic here } private SpecializedList subtractLists(SpecializedList list1, SpecializedList list2) { // Subtraction logic here } private int compareLists(SpecializedList list1, SpecializedList list2) { // Comparison logic here } }
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