600.107 Introduction to Programming in Java
Homework #10 -- Due 11:30pm on Tuesday 12/5

Overview

The purpose of this assignment is to gain more experience writing methods, specifically using the recursion technique. You are not expected to do an object-oriented solution.

Deliverables: You must submit one zip file named HW10-jhed.zip with your java file (Hw10a.java) for part A and a plain text or pdf file with your solution to Part B in your final submission.


Part A: Integer Expressions

Languages are often defined by grammar rules which tell us how to put together sentences. This is even true for programming language expressions and statements. For this assignment, you will write a program that generates five random valid arithmetic expressions based on a set of mostly recursive rules.

For this program we will use a very simplified form of arithmetic expressions with integers in the range 0-20 only. A valid expression is formed from the following (recursive) rules. We will randomly apply each rule with a certain probability as noted below in [] (so that our generated expressions are not infinitely long).

Here are some valid arithmetic expressions that might be generated. Note that the binary arithmetic operators are always surrounded by spaces.

14
20 + 4
10 - -3 + (4 / (17 - 2))
-(9 % 12)
1 * (20 / 3) % 4 - (15 + 6) * 17 / 8 + 9 - 10

You must use recursion to generate each expression! Specifically, you must have a method with this header:

    public static String makeAE()
  
that generates a random arithmetic expression by applying one of the rules above, choosing which to use according to the probabilities. If it chooses a recursive rule, then the method should call itself to generate the necessary subexpressions.

There is no input to the program. Instead, your main method should simply call the makeAE method 5 times, and display the result of each method call on the screen, one per line (similar to the example above). The only loop that your program may have is the one in main that repeats 5 times to do this.

Remember to use good styling as usual, including complete javadoc comments for your methods and making your program checkstyle compliant. Since you need to work with random data, you might want to declare a global class variable for that. You only have to create one main class file for this solution, Hw10a.java. It should be pretty short if you approach the problem correctly!

Part B: Integer Expression Tests

Suppose that instead of generating valid integer expressions, you were writing a program to check if a string is a valid integer expression. We will use the same rules as above to define a valid integer expression. For this part of the assignment, all you need to do is write tests that would be used to check such a program. You must include a variety of valid integer expressions in your tests, as well as invalid expressions. You may not use any of the above examples in your solution to this part.

Clearly identify your test strings as either valid or invalid. Include one sentence for each test string that explains what in particular it is intending to test if valid, or why it is invalid. You should have at least 5 valid and 5 invalid test strings, each with a distinct explanation. Write this up and submit it as either a plain text or a pdf document in your submission zip.


General assignment requirements, style and submission details: