finish the java programming a Begin by creating a new class

finish the java programming

a. Begin by creating a new class named MyHugeIntegerStack which implements a stack ADT

that holds variables of type MyHugeInteger. The underlying data structure must be an array

of MyHugeInteger no greater than 50 elements in size. This stack must implement the

following interface methods:

boolean push(MyHugeInteger hugeInt) – which pushes the hugeInt object onto the stack

and the return value is true if everything went well or false if the stack is too full for the

push to complete successfully

MyHugeInteger pop() - this method is to remove and return the value at the top of the

stack and return null if the stack is empty

boolean isEmpty() - returns true is the stack is empty, false otherwise

void clear() - clears the contents of the stack

b. Next, create a new class called MyStringQueue which implements a queue ADT that holds

variables of type String. The underlying data structure must be an array of String no greater

than 50 elements in size. This queue may be of the standard or circular type, the choice is

yours. This queue must implement the following interface methods:

boolean enqueue(String str) – this method is to add str to the back of the queue and

return true if successful or false if the queue is too full for this item.

String dequeue() - this method is to remove and return the item at the front of the queue

and return null if the queue is empty

boolean isEmpty() - returns true is the queue is empty, false otherwise

void clear() - clears the contents of the queue

String toString() - this method is to return a String with each element in the queue on a

new line, starting with the element at the head of the queue

c. Extend the functionality of the MyHugeInteger class by adding the following

methods:

void incrementBy(MyHugeInteger hugeInt) – this method is intended to increment the

object\'s internal large number value by the number specified in hugeInt

void decrementBy(MyHugeInteger hugeInt) – this method is intended to decrement the

object\'s internal large number value by the number specified in hugeInt

d. Create a new class named MyHugeIntegerCalculator that provides the following method

(this class does not need a constructor):

MyHugeInteger calculate(MyStringQueue postfixInput) – Here, postfixInput is a String

queue whose elements are comprised of operators and operands. The operators are either

“+” or “-”, denoting addition and subtraction, respectively. The operands are strings of

numerical digits, each denoting a large integer. These operators and operands are given

in postfix order. This method is to remove the String elements from the queue, one-byone

and, with the help of a MyHugeIntegerStack, carry out the calculations that are

prescribed by the queue. When all operations have been carried out, return the result. In

the event that the input to this method is invalid (i.e. too many or too few operators or

operands) then the method is to return null.

Solution

public class MyHugeIntegerCalculator { public static MyHugeInteger calculate(MyStringQueue postfixInput) { MyHugeIntegerStack myHugeIntegerStack = new MyHugeIntegerStack(); while (!postfixInput.isEmpty()) { String queueElement = postfixInput.dequeue(); if (!queueElement.equals(\"+\") && !queueElement.equals(\"-\")) { myHugeIntegerStack.push(new MyHugeInteger(Integer.parseInt(queueElement))); } else { MyHugeInteger hugeInteger1 = myHugeIntegerStack.pop(); MyHugeInteger hugeInteger2 = myHugeIntegerStack.pop(); if (hugeInteger1 == null || hugeInteger2 == null) { return null; } if (queueElement.equals(\"+\")) { hugeInteger2.incrementBy(hugeInteger1); myHugeIntegerStack.push(hugeInteger2); } else { hugeInteger2.decrementBy(hugeInteger1); myHugeIntegerStack.push(hugeInteger2); } } } if (!myHugeIntegerStack.isEmpty()) { MyHugeInteger myHugeInteger = myHugeIntegerStack.pop(); if (!myHugeIntegerStack.isEmpty()) { return null; } else { return myHugeInteger; } } return null; } } class MyStringQueue { private String[] queue; private int front; private int rear; private static final int LENGTH = 50; public MyStringQueue() { queue = new String[LENGTH]; front = 0; rear = 0; } public boolean enqueue(String str) { // if (front == rear) return false; if (rear == LENGTH) { if (front > 0) { rear = 0; } else { return false; } } queue[rear++] = str; return true; } public String dequeue() { if (isEmpty()) { return null; } int front = this.front; if (this.front == LENGTH) { this.front = 0; } else { this.front++; } return queue[front]; } public boolean isEmpty() { return front == rear; } public void clear() { queue = new String[LENGTH]; front = 0; rear = 0; } @Override public String toString() { if (isEmpty()) return null; StringBuilder stringBuilder = new StringBuilder(); int counter = front; do { if (counter == LENGTH) counter = 0; stringBuilder.append(queue[counter]); if (counter != (rear -1)) { stringBuilder.append(\"\ \"); } } while (counter++ != (rear-1)); return stringBuilder.toString(); } } class MyHugeIntegerStack { private MyHugeInteger[] stack; private int top; private int rear; private static final int LENGTH = 50; public MyHugeIntegerStack() { stack = new MyHugeInteger[LENGTH]; top = 0; } public boolean push(MyHugeInteger hugeInt) { if (top == LENGTH) return false; else { stack[top++] = hugeInt; return true; } } public MyHugeInteger pop() { if (isEmpty()) return null; else { return stack[--top]; } } public boolean isEmpty() { return top == 0; } public void clear() { stack = new MyHugeInteger[LENGTH]; top = 0; } } class MyHugeInteger { private int hugeInt; public MyHugeInteger(int hugeInt) { this.hugeInt = hugeInt; } public int getHugeInt() { return hugeInt; } public void incrementBy(MyHugeInteger hugeInt) { this.hugeInt += hugeInt.getHugeInt(); } public void decrementBy(MyHugeInteger hugeInt) { this.hugeInt -= hugeInt.getHugeInt(); } }
finish the java programming a. Begin by creating a new class named MyHugeIntegerStack which implements a stack ADT that holds variables of type MyHugeInteger. T
finish the java programming a. Begin by creating a new class named MyHugeIntegerStack which implements a stack ADT that holds variables of type MyHugeInteger. T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site