Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create the following Java files: Stack.Java StackTester.Java Calculator.Java Cal

ID: 3706803 • Letter: C

Question

Create the following Java files:
            Stack.Java
            StackTester.Java
            Calculator.Java
            CalculatorTester.Java

The Stack object needs the attributes below. You may add others as necessary.
            data             array of integers, containing the data for the stack
            count            integer, indicating the current number of items in the stack
            maxSize       integer, indicating the maximum size of the data array (may be unnecessary)
            verbose       boolean, indicating whether verbose mode is on

In the Stack object, implement the Stack methods described below. You may create others if necessary.

In the StackTester object, instantiate three new Stacks, using each of the three constructors created. Test all methods implemented several times to demonstrate they work correctly.

Double-check all errors for a full or empty stack. Try to push into a full Stack, or pop/peek from an empty Stack. The arithmetic methods all need two numbers to work correctly, so make sure the program behaves correctly for Stacks that are empty or contain only one element. The fibon method needs one integer in the stack to work correctly. The toString and toTable methods should work just fine, even for zero elements.

In the Calculator object constructor, instantiate a new Stack object attribute, with an initial list of integers for instantiation. You may create other attributes as necessary, such as a verbose attribute.

Within Calculator, write a text-based user-interface around the stack, allowing the user to enter numbers and perform calculations, as shown in the example To start, write a method named acceptCommand, that accepts a one-character command from the user. Use a switch-case statement on the character to call other methods within Calculator. Don’t implement the commands yet, simply code the method structure and include the verbose messages for each command. The user should be able to enter the following commands:
            e/E       Enter new number (push)
            +          Add the top-two numbers (B + A), replace with the result
            -           Subtract the top-two numbers (B – A), replace with the result
            *          Multiply the top-two numbers (B * A), replace with the result
            /           Divide the top-two numbers (B / A), replace with the result (integer division)
            p/P       Perform Power on the top-two numbers (BA), replace with the result
            f/F       Perform Fibonacci’s algorithm to the top number, replace with result
            v/V      Print a vertical list of the data (formatted nicely)
            m/M     Toggle verbose mode (on/off)
            <          Sort numbers in ascending order
            >          Sort numbers in descending order
            ?          Help, display commands available     
            q/Q      Quit program

One at a time, test and implement the Calculator methods, and demonstrate they work properly. Test for full, empty, or incomplete stacks as well.

Write a main control function, execute, that simply runs acceptCommand in a loop until the user quits the program.

In the CalculatorTester object, instantiate a new Calculator object, then call execute. True to form for OOP, a tester typically just instantiates a new object and sets it in motion.

--------------Stack methods-----------

Stack()

Constructor with no parameters, creating an empty array of size 10.

Stack(int maxSize)

Constructor with one parameter, the size of the array. Creating an empty array of the appropriate size.

Stack(int maxSize, int[] initialData)

Constructor with two parameters, the size of the array, and the initial data for the stack. Creating an empty array of the appropriate size, and copy the initial data over to the attribute. Ensure that there are no more than maxSize numbers in the initialData.

boolean push(int num_)

If there is space, push num_ onto the Stack data. Return true if the parameter was pushed, and false if the Stack was full.

boolean pop()

Pop the top element from the Stack. Return true if element was popped, and false if the Stack was empty.

int peek()

Return the top element of the Stack. If the Stack is empty, return 0. (Discussion: is there a better solution?)

boolean add()

If fewer than two numbers are on the Stack, return false and do nothing. Otherwise, pop two numbers (A and B), push B+A back onto the stack, and return true.

boolean subtract()

If fewer than two numbers are on the Stack, return false and do nothing. Otherwise, pop two numbers (A and B), push B-A back onto the stack, and return true.

boolean multiply()

If fewer than two numbers are on the Stack, return false and do nothing. Otherwise, pop two numbers (A and B), push B*A back onto the stack, and return true.

boolean divide()

If fewer than two numbers are on the Stack, return false and do nothing. Otherwise, pop two numbers (A and B), push B/A (integer devision) back onto the stack, and return true. Also check for division by zero.

boolean fibon()

If the Stack is empty, return false. Otherwise, pop the top number (A), apply the recursive Fibonnaci algorithm (as discussed in class), push the result back onto the stack, and return true.

boolean power()

If fewer than two numbers are on the stack, return false and do nothing. Otherwise, pop two numbers (A and B), push BA (using the recursive form discussed in class), push back onto the stack, and return true.

void sortAscending()

Sort the Stack data in ascending order, using the bubble sort discussed in class.

void sortDescending()

Sort the Stack data in descending order, using the bubble sort discussed in class

Explanation / Answer

StackTester.java:

package com.java.stack;

public class StackTester {

public static void main(String[] args) {

int[] initialData = {4, 6, 3, 7, 1, 8};

Stack s1 = new Stack();

Stack s2 = new Stack(5);

Stack s3 = new Stack(9, initialData);

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

System.out.println(s3.toTable());

//pushing 1 element to each Stack

s1.push(17);

s2.push(19);

s3.push(21);

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

//popping top element from stack s3

s3.pop();

System.out.println(" Popping top element from stack s3.. " + s3);

//adding top 2 elements of Stack s3 and pushing the result

s3.add();

System.out.println(" Adding top 2 elements of Stack s3 and pushing the result.. " + s3);

//subtracting top 2 elements of Stack s3 and pushing the result

s3.subtract();

System.out.println(" Subtracting top 2 elements of Stack s3 and pushing the result.. " + s3);

//multiplying top 2 elements of Stack s3 and pushing the result

s3.multiply();

System.out.println(" multiplying top 2 elements of Stack s3 and pushing the result.. " + s3);

//dividing top 2 elements of Stack s3 and pushing the result

s3.divide();

System.out.println(" dividing top 2 elements of Stack s3 and pushing the result.. " + s3);

s3.push(14);

s3.push(18);

//fibonacci on s3

s3.fibon();

System.out.println(" fibonacci on s3.. " + s3);

//fecthing the peek of the Stack s3

System.out.println(" fecthing the peek of the Stack s3: " + s3.peek());

//calculating the power of top two elements of s3 and pushing the result

s3.power();

System.out.println(" Calculating the power of top two elements of s3 and pushing the result.. " + s3);

s3.push(28);

s3.push(24);

System.out.println(" Before sorting: " + s3);

//sorting the array in ascending order

s3.sortAscending();

System.out.println(" Sorting the array in ascending order.. " + s3);

s3.push(34);

s3.push(38);

//sorting the array in descending order

s3.sortDescending();

System.out.println(" Sorting the array in ascending order.. " + s3);

}

}

Stack.java:

package com.java.stack;

import java.util.Arrays;

public class Stack {

int[] data;

int count;

int maxSize;

boolean verbose;

public Stack() {

data = new int[10];

}

public Stack(int maxSize) {

this.maxSize = maxSize;

data = new int[maxSize];

}

public Stack(int maxSize, int[] initialData) {

this.maxSize = maxSize;

data = new int[maxSize];

data = initialData;

}

public boolean push(int num_) {

if (data.length < maxSize) {

data = Arrays.copyOf(data, data.length + 1);

data[data.length - 1] = num_;

return true;

} else {

return false;

}

}

public boolean pop() {

if (data.length > 0) {

data = Arrays.copyOfRange(data, 1, data.length-1);

return true;

} else {

return false;

}

}

public int peek() {

if (data.length > 0) {

return data[data.length - 1];

} else {

return 0;

}

}

public boolean add() {

int A, B;

if (data.length > 1) {

B = peek(); //copied top element to B

pop(); //deleted the top element

A = peek(); //copied the current top element to A

pop(); //deleted the current top element

// return push(B+A);

push(B+A);

return true;

} else {

return false;

}

}

public boolean subtract() {

int A, B;

if (data.length > 1) {

B = peek(); //copied top element to B

pop(); //deleted the top element

A = peek(); //copied the current top element to A

pop(); //deleted the current top element

// return push(B-A);

push(B-A);

return true;

} else {

return false;

}

}

public boolean multiply() {

int A, B;

if (data.length > 1) {

B = peek(); //copied top element to B

pop(); //deleted the top element

A = peek(); //copied the current top element to A

pop(); //deleted the current top element

// return push(B*A);

push(B*A);

return true;

} else {

return false;

}

}

public boolean divide() {

int A, B;

if (data.length > 1) {

B = peek(); //copied top element to B

pop(); //deleted the top element

A = peek(); //copied the current top element to A

pop(); //deleted the current top element

// return push(B/A);

if (A != 0) {

push(B/A);

} else {

System.out.println("Division is not possible as A=0");

}

return true;

} else {

return false;

}

}

public boolean fibon() {

if (data.length > 0) {

int A = peek();

pop();

push(A + (A+1)); //adding 1 to the top element

return true;

} else {

return false;

}

}

public boolean power() {

int A, B;

if (data.length > 1) {

B = peek(); //copied top element to B

pop(); //deleted the top element

A = peek(); //copied the current top element to A

pop(); //deleted the current top element

push((int)Math.pow(B, A)); //calculates B^A and pushes to Stack

return true;

} else {

return false;

}

}

public void sortAscending() {

//Arrays.sort(data);

//--------OR--------------

int temp = 0;

for (int i = 0; i < data.length; i++) {

for (int j = 1; j < (data.length - i); j++) {

if (data[j - 1] > data[j]) {

temp = data[j - 1];

data[j - 1] = data[j];

data[j] = temp;

}

}

}

}

public void sortDescending() {

//Arrays.sort(data, Collections.reverseOrder());

//--------OR--------------

int temp = 0;

for (int i = 0; i < data.length; i++) {

for (int j = 1; j < (data.length - i); j++) {

if (data[j - 1] < data[j]) {

temp = data[j - 1];

data[j - 1] = data[j];

data[j] = temp;

}

}

}

}

@Override

public String toString() {

String res = "Array Elements: ";

return res + Arrays.toString(data);

}

public String toTable() {

String toTable = " Array ------ ";

for (int i = 0; i < data.length; i++) {

toTable += " " + data[i] + " ";

}

toTable += "------";

return toTable;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote