Question: Application #1 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

Application #1

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 myStackName = new Stack<>();

1. Decimal to Binary Conversion:

a. utilizing a stack trace the algorithm on paper

b. analyze the sample run

c. describe the algorithm in pseudocode

d. implement and test

2. Ancient Egyptian Multiplication:

a. go to http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication and analyze the given algorithm

b. analyze sample runs

c. why is this method significant to computer scientists?

d. utilizing two stacks trace the algorithm on paper

e. describe the algorithm in pseudocode

f. implement and test

3. Elimination of Adjacent Duplicates:

a. given an array of integers create ArrayList that contains all the numbers remaining after eliminating all adjacent duplicates in one pass using one stack

b. analyze sample runs

c. describe the algorithm in pseudocode

d. implement and test

package Lab04;

import java.text.DecimalFormat; import java.util.*;

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 --> ");

// YOUR CODE GOES HERE

System.out.println(); } while (true); } catch (InputMismatchException ime) { System.out.println("Done with conversion. "); } }

public void ancientMultiplier() { // TODO PROJECT #1 // http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication Stack op1 = new Stack<>(); Stack op2 = new Stack<>();

}

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!