Question: Create a java program that takes in user input to define the size and elements of a stack then allows the user to perform simple
Create a java program that takes in user input to define the size and elements of a stack then allows the user to perform simple comands on the stack as listed below. Example of the output is provided,
Using the given Stack.java and StackTester.java code create the following Java files Calculator.Java CalculatorTester.Java
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. Dont 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 Fibonaccis 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.
**************Exanple Output******************
What size would you like the stack array?
=> 6
How many initial numbers?
=> 3
Enter initial numbers:
Number #1 => -4
Number #2 => 8
Number #3 => -12
Command => ?
Available Commands:
e/E enter new number
+ add top two numbers on stack
(other commands omitted for space)
q/Q quit program
Command => m
Verbose mode ON
Command => v
Displaying Vertical Table
+------------+
| -12 |
| 8 |
| -4 |
+------------+
Command => e
Entering new number
Enter number => 7
7 added to Stack
Command => v
Displaying Vertical Table
+------------+
| 7 |
| -12 |
| 8 |
| -4 |
+------------+
Command => +
Adding -12 + 7 = -5
Returning -5 to Stack
Command => -
Subtracting 8 -5 = 13
Returning 13 to Stack
Command => *
Multiplying -4 * 13 = -52
Returning -52 to the Stack
Command => v
Displaying Vertical Table
+------------+
| -52 |
+------------+
Command => /
Cannot divide with only one number in Stack
Stack unchanged
Command => m
Verbose mode OFF
Command => e
Enter number => 7
Command => v
+------------+
| 7 |
| -52 |
+------------+
Command => /
Command => v
+------------+
| -7 |
+------------+
Command => e
Enter Number => -2
Command => /
Command => e
Enter Number => 80
Command => e
Enter Number => 14
Command => e
Enter Number => 75
Command => e
=> -99
Command => e
=> 52
Command => v
+------------+
| 52 |
| -99 |
| 75 |
| 14 |
| 80 |
| 3 |
+------------+
Command => e
Stack is full, with 6 numbers. Cannot enter new number.
Command => -
Command => v
+------------+
| -151 |
| 75 |
| 14 |
| 80 |
| 3 |
+------------+
Command => +
Command => v
+------------+
| -76 |
| 14 |
| 80 |
| 3 |
+------------+
Command => +
Command => v
+------------+
| -62 |
| 80 |
| 3 |
+------------+
Command => +
Command => v
+------------+
| 18 |
| 3 |
+------------+
Command => -
Command => v
+------------+
| -15 |
+------------+
Command => *
Only one number in stack, cannot add.
Stack unchanged.
Command => e
Enter Number => 5
Command => v
+------------+
| 5 |
| -15 |
+------------+
Command => /
Command => v
+------------+
| -3 |
+------------+
Command => m
Verbose mode ON
Command => e
Entering new number
Enter Number => 12
Command => f
Fibonacci on 12, result is 144
Pushing 144 onto Stack
Command => v
+------------+
| 144 |
| -3 |
+------------+
Command => m
Verbose mode OFF
Command => +
Command => v
+------------+
| 141 |
+------------+
Command => e
Enter Number => 31
Command => e
Enter Number => -415
Command => e
Enter Number => 9
Command => e
Enter Number => 26
Command => e
Enter Number => -53
Command => v
+------------+
| -53 |
| 26 |
| 9 |
| -415 |
| 31 |
| 141 |
+------------+
Command => <
Command => v
+------------+
| -415 |
| -53 |
| 9 |
| 26 |
| 31 |
| 141 |
+------------+
Command => >
Command => v
+------------+
| 141 |
| 31 |
| 26 |
| 9 |
| -53 |
| -415 |
+------------+
Command => Q
Exiting program.
********************Stack.java******************************
import java.util.*;
public class Stack
{ private int[] data; private int count; private int maxSize; boolean verbose; public Stack(int[] data, int count, int maxSize, boolean verbose) { this.data = new int[maxSize]; this.count = data.length; this.maxSize = maxSize; this.verbose = verbose; for (int i = 0; i < count; i++) { this.data[i] = data[i]; } } public Stack() { this.maxSize = 10; this.data = new int[maxSize]; this.count = 0; this.verbose = false; } public Stack(int maxSize) { this.maxSize = maxSize; this.data = new int[maxSize]; this.count = 0; this.verbose = false; } public Stack(int[] data, int maxSize) { this.data = data; this.maxSize = maxSize; this.count = 0; this.verbose = false; } public int[] getData() { if (verbose) { String temp = " "; temp += "*------ getData method called ------*"; System.out.println(temp); } return data; } public int getCount() { if (verbose) { String temp = " "; temp += "*------ getCount method called ------*"; System.out.println(temp); } return count; } public int getMaxSize() { if (verbose) { String temp = " "; temp += "*------ getMaxSize method called ------*"; System.out.println(temp); } return maxSize; } public void setCount(int count) { if (verbose) { String temp = " "; temp += "*------ setCount method called ------*"; System.out.println(temp); } this.count = count; } public void setMaxSize(int maxSize) { if (verbose) { String temp = " "; temp += "*------ setMaxSize method called ------*"; System.out.println(temp); } this.maxSize= maxSize; } public boolean push(int num_) { if (verbose) { String temp = " "; temp += "*------ push method called, "+num_+" pushed------*"; System.out.println(temp); } if (count == maxSize) { System.out.println("Stack is full, number not pushed."); return false; } else if(maxSize > 0) { data[count] = num_; count++; } return true; } public boolean pop() { if (verbose) { String temp = " "; temp += "*------ pop method called-------*"; System.out.println(temp); } if (count == 0) { return false; } else if(maxSize > 0) { data[count-1] = 0; count--; } return true; } public int peek() { if (verbose) { String temp = " "; temp += "*------ peek method called------*"; System.out.println(temp); } if (count == 0) { return 0; } return data[count - 1]; } public boolean add() { if (verbose) { String temp = " "; temp += "*------ add method called------*"; System.out.println(temp); } if (count < 2) { return false; } else if(count >= 2) { int a = this.peek(); this.pop(); int b = this.peek(); this.pop(); push(b+a); } return true; } public boolean subtract() { if (verbose) { String temp = " "; temp += "*------ subtract method called------*"; System.out.println(temp); } if (count < 2) { return false; } else if(count >= 2) { int a = this.peek(); this.pop(); int b = this.peek(); this.pop(); push(b-a); } return true; } public boolean multiply() { if (verbose) { String temp = " "; temp += "*------ multiply method called------*"; System.out.println(temp); } if (count < 2) { return false; } else if(count >= 2) { int a = this.peek(); this.pop(); int b = this.peek(); this.pop(); push(b*a); } return true; } public boolean divide() { if (verbose) { String temp = " "; temp += "*------ divide method called------*"; System.out.println(temp); } if (count < 2) { return false; } else if(count >= 2) { int a = this.peek(); this.pop(); int b = this.peek(); this.pop(); push(b/a); } return true; } public int fibonacci(int n) { { if (verbose) { String temp = " "; temp += "*------ fibonacci method called------*"; System.out.println(temp); } if (n == 0) return 1; if (n == 1) return 1; return fibonacci(n-2) + fibonacci(n - 1); } } public boolean fibon() { { if (verbose) { String temp = " "; temp += "*------ fibon method called------*"; System.out.println(temp); } if (count < 1) return false; else { int n = this.peek(); this.pop(); int result = fibonacci(n); this.push(result); return true; } } } public int powerFunction(int a, int b){ if (verbose){ String temp = " "; temp += "*------ powerFunction method called ------*"; System.out.println(temp); } if (a == 0) { return 1; } return b * powerFunction(a - 1 , b); } public boolean power() { if (verbose) { String temp = " "; temp += "*------ power method called ------*"; System.out.println(temp); } if (count < 1) { return false; } else { int a = this.peek(); this.pop(); int b = this.peek(); this.pop(); int result = powerFunction(a, b); this.push(result); return true; } } public void sortDescending() { { if (verbose) { String temp = " "; temp += "*------ sortAscending method called------*"; System.out.println(temp); } for(int i = 0; i < count-1; i++) { for (int j = 0; j < count - 1; j++) { if (data[j] > data[j+1]) { int temp = data[j]; data[j] = data[j + 1]; data[j+1] = temp; } } } } } public void sortAscending() { if (verbose) { String temp = " "; temp += "*------ sortDescending method called ------*"; System.out.println(temp); } for (int i = 0; i < count - 1; i++){ for (int j = 0; j < count -1; j++){ if (data[j + 1] > data[j]){ int temp = data[j + 1]; data[j + 1] = data[j]; data[j] = temp; } } } } public void verboseOn() { this.verbose = true; } public void verboseOff() { this.verbose = false; }
public String toString() { if (verbose) { String temp = " "; temp += "*------ toString method called ------*"; System.out.println(temp); } String result = "+--------------+"; for(int i = count - 1; i >= 0; i--) { result += String.format(" | %1$12s |", this.data[i]); } result += " +--------------+"; return result; } }
***********************StackTester.java************************
import java.util.*;
public class StackTester { public static void main(String []args) { int[] stack = {}; Stack newStack = new Stack(stack, 0, 10, false); newStack.verboseOn(); newStack.push(5); newStack.push(10); newStack.push(102); newStack.push(1); newStack.push(369); newStack.push(87); newStack.push(95); newStack.push(123); newStack.push(45); newStack.push(3971); newStack.push(230); System.out.println(newStack); newStack.pop(); newStack.pop(); newStack.pop(); System.out.println(newStack); newStack.add(); System.out.println(newStack); newStack.subtract(); System.out.println(newStack); newStack.multiply(); System.out.println(newStack); newStack.divide(); System.out.println(newStack); newStack.pop(); newStack.pop(); newStack.pop(); newStack.push(2); newStack.push(3); newStack.push(4); System.out.println(newStack); newStack.fibon(); System.out.println(newStack); newStack.pop(); newStack.pop(); newStack.pop(); newStack.push(2); newStack.push(5); System.out.println(newStack); newStack.power(); System.out.println(newStack); newStack.pop(); newStack.push(5); newStack.push(10); newStack.push(102); newStack.push(1); newStack.push(369); newStack.push(87); newStack.sortAscending(); System.out.println(newStack); newStack.sortDescending(); System.out.println(newStack); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
