Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSE 274 – Spring, 2023

Homework #3 – Due Friday, March 17, 2023 by 11:59 p.m.

No late assignments accepted for any reason

Stacks and Queues

General requirements (not following any of these could result in a score of 0):

● Code is expected to compile and meet basic coding standards for readability

● You may use as many Java classes as you need- putting everything in main is NOT a good coding solution.  Main method should be in a class by itself.

● Use the coding standards linked in your Canvas class under Useful Links.  Failure to do so will result in deductions.

● Make sure you cite any code not written by you and add your name to the author list if you made changes.  Put your name on any file you start from scratch. It is expected that no more than 25% of the code is cited or copied from examples given or websites

The Problem

This homework includes 2 unrelated problems.  So you will have two separate main methods.  Both problems are most easily solved using  a Stack implementation.  You may use the java.util.Stack iLibrary class for these problems or use your own Stack implementation that we did in lab 3.  If you use your own implementation- make sure to include the java file for that implementation. Both programs should repeat until user indicates they wish to exit.  

Problem 1:

Create a Java program that accepts a String containing a mathematical equation such as “(2+5) – 66” from the user.   The program then must determine if the parenthesis in the problem are balanced and alert the user if the equation is valid or invalid based on the parenthesis. Note that I will test with additional test cases.  An empty String could be used to terminate the program.

Some test cases:

String

Valid

“(2+5) – 66”

Yes

“((3)”

No

“((2 * 6) + 33 +2)”

Yes

“()”

Yes

“))((“

No

“())”

No

“(()”

No

Example Output:

Please enter an equation (or -1 to exit) :  (2+2)

Equation is balanced.

Please enter an equation (or -1 to exit) :  -1

Good bye.

Problem 2:

Create a different Java program that accepts a positive integer from the user and converts it to base 2 (a binary number).  A negative number could be used to terminate the program. Your output may contain leading zeros and/or spaces between every four digits if you desire.

Hint: Conversion can be performed by repetitious division by 2 and then taking the remainders of division in reverse order (this is where Stack comes in).  For example, number 6 requires 3 divisions: 6/2 =3 remainder 0, 3/2 = 1 remainder 1, ½ = 0 remainder 1.  For a binary number of 110.

Some test cases:

Decimal Number

Binary Number

88

1011000

1

1

569

1000111001

16

10000

25

1 1001

Example Output:

Please enter a positive number (or -1 to exit) :  2

Binary equivalent is 10

Please enter a positive number (or -1 to exit) :  -1

Good bye.

Grading Criteria(P1 = problem 1, P2= problem 2)

Criteria

Points

P1 correctly passes test cases

25

P1 uses a Stack

10

P1 repeats until user is done

10

P2 correctly passes test cases

25

P2 uses a Stack

10

P2 repeats until user is done

10

Code follows coding standards

10