Question: Open FunWithStack.java, it contains three methods defined there as skeletons, and main to test them. Utilize Stack class defined in java.util, create the stacks as
Open FunWithStack.java, it contains three methods defined there as skeletons, and main to test them. Utilize Stack class defined in java.util, create the stacks as follow: Stack
Decimal to Binary Conversion:
utilizing a stack trace the algorithm on paper
analyze the sample run
describe the algorithm in pseudocode
implement and test
Ancient Egyptian Multiplication:
go to http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication and analyze the given algorithm
analyze sample runs
why is this method significant to computer scientists?
utilizing two stacks trace the algorithm on paper
describe the algorithm in pseudocode
implement and test
Elimination of Adjacent Duplicates:
given an array of integers create ArrayList
analyze sample runs
describe the algorithm in pseudocode
implement and test
public class FunWithStack { public void decimalToBinary() { System.out.println("DECIMAL TO BINARY CONVERTER"); // TODO PROJECT #1 Scanner keyboard = new Scanner(System.in); Stack stack = new Stack<>(); try { do { System.out.println(" Please enter a positive integer, or type \"stop\""); int decimalNumber = keyboard.nextInt(); System.out.print(decimalNumber + " in binary is --> "); while(decimalNumber != 0){ int reminder = decimalNumber % 2; stack.push(reminder); decimalNumber /= 2; } while(!(stack.isEmpty())){ System.out.print(stack.pop()); } // YOUR CODE GOES HERE System.out.println(); } while (true); } catch (InputMismatchException ime) { System.out.println("Done with conversion. "); } } public void ancientMultiplier() { // TODO PROJECT #1 int smallest = 0; int largest = 0; Scanner keyboard = new Scanner(System.in); // http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication Stack op1 = new Stack<>(); Stack op2 = new Stack<>(); try { do { System.out.println(" Please enter operand 1, or type \"stop\""); int num1 = keyboard.nextInt(); System.out.println(num1); System.out.println("Please enter operand 2, or type \"stop\""); int num2 = keyboard.nextInt(); System.out.println(num2); System.out.println("The smaller operand is " + smallest + " and the larger operand is " + largest); // YOUR CODE GOES HERE System.out.println(); } while (true); } catch (InputMismatchException ime) { System.out.println("Done with conversion. "); } } public ArrayList noAdjacentDuplicates(int... input) { // TODO PROJECT #1 ArrayList result = new ArrayList<>(); Stack stack = new Stack<>(); System.out.println("input = " + Arrays.toString(input)); return result; } public static void main(String[] args) { FunWithStack funWithStack = new FunWithStack(); System.out.println("*** DECIMAL TO BINARY CONVERTER ***"); funWithStack.decimalToBinary(); System.out.println("*** ANCIENT MULTIPLIER ***"); funWithStack.ancientMultiplier(); System.out.println("*** ELIMINATING ADJACENT DUPLICATES ***"); System.out.println("--> testcase #1"); ArrayList result = funWithStack.noAdjacentDuplicates(1, 5, 6, 8, 8, 8, 0, 1, 1, 0, 6, 5); ArrayList expected = new ArrayList<>(); expected.add(1); if (result.equals(expected)) System.out.println("result = " + result + " CORRECT"); else { System.out.println("INCORRECT, expected: " + expected); System.out.println("got: " + result); } System.out.println(" ---> testcase #2"); result = funWithStack.noAdjacentDuplicates(1, 9, 6, 8, 8, 8, 0, 1, 1, 0, 6, 5); expected.clear(); expected.add(1); expected.add(9); expected.add(5); if (result.equals(expected)) System.out.println("result = " + result + " CORRECT"); else { System.out.println("INCORRECT, expected: " + expected); System.out.println("got: " + result); } System.out.println(" ---> testcase #3"); result = funWithStack.noAdjacentDuplicates(1, 1, 6, 8, 8, 8, 0, 1, 1, 0, 6, 5); expected.clear(); expected.add(5); if (result.equals(expected)) System.out.println("result = " + result + " CORRECT"); else { System.out.println("INCORRECT, expected: " + expected); System.out.println("got: " + result); } System.out.println(" ---> testcase #4"); result = funWithStack.noAdjacentDuplicates(1, 1, 1, 5, 6, 8, 8, 8, 0, 1, 1, 0, 6, 5); expected.clear(); if (result.equals(expected)) System.out.println("result = " + result + " CORRECT"); else { System.out.println("INCORRECT, expected: " + expected); System.out.println("got: " + result); } System.out.println("Done!"); } } SAMPLE RUN:
*** DECIMAL TO BINARY CONVERTER ***
Please enter a positive integer, or type "stop"
6
6 in binary is --> 110
Please enter a positive integer, or type "stop"
12345
12345 in binary is --> 11000000111001
Please enter a positive integer, or type "stop"
stop
Done with conversion.
*** ANCIENT MULTIPLIER ***
Please enter operand1, or type "stop"
12345
Please enter operand2
6789
The smaller operand is: 6789; and the larger operand is: 12345
--> Creating the mapping table:
1 --> 12,345
2 --> 24,690
4 --> 49,380
8 --> 98,760
16 --> 197,520
32 --> 395,040
64 --> 790,080
128 --> 1,580,160
256 --> 3,160,320
512 --> 6,320,640
1024 --> 12,641,280
2048 --> 25,282,560
4096 --> 50,565,120
---> Calculating the result
6,789 * 12,345 is: 50,565,120 + 25,282,560 + 6,320,640 + 1,580,160 + 49,380 + 12,345 = 83,810,205
Please enter operand1, or type "stop"
4
Please enter operand2
5
The smaller operand is: 4; and the larger operand is: 5
--> Creating the mapping table:
1 --> 5
2 --> 10
4 --> 20
---> Calculating the result
4 * 5 is: 20 = 20
Please enter operand1, or type "stop"
stop
Done multiplying
*** ELIMINATING ADJACENT DUPLICATES ***
--> testcase #1
input = [1, 5, 6, 8, 8, 8, 0, 1, 1, 0, 6, 5]
result = [1] CORRECT
---> testcase #2
input = [1, 9, 6, 8, 8, 8, 0, 1, 1, 0, 6, 5]
result = [1, 9, 5] CORRECT
---> testcase #3
input = [1, 1, 6, 8, 8, 8, 0, 1, 1, 0, 6, 5]
result = [5] CORRECT
---> testcase #4
input = [1, 1, 1, 5, 6, 8, 8, 8, 0, 1, 1, 0, 6, 5]
result = [] CORRECT
Done!
Process finished with exit code 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
