I need help with Java homework assignment. I had already uploaded my assignment handout (named CSC 413 A3 P2 and capture). I also uploaded all the java files for you to have a look at it. Please follow the instruction on the assignment handout. Please if I missing any files that necessary for you or any confusion about the order, please feel free to ask me.
Assignment Setup Click on the GitHub Classroom link provided on iLearn to automatically create your GitHub repository for the assignment, which will include the starter code for the project. See the “Git, GitHub, and IntelliJ Setup Guide” document for instructions on setting up the rest of the project on your local machine. Note that the folder for test code will be included in the starter project, but testing is not required for this assignment. Detailed Requirements This is a continuation of Assignment 3, Part 1 where we designed and implemented an Interpreter which can parse and run programs written in a simplified version of Python. Part 2 adds some additional features, with new statement and expression types, including: ● Running while loops and ranged-based for loops ● Defining and calling functions, with parameters and proper variable scoping To begin with, I strongly recommend reviewing material from Lecture 17 and Lecture 18, where we went through a high-level breakdown of the assignment’s various features. You will be provided with the fully implemented classes from Part 1, in addition to some partially implemented pieces that are relevant for Part 2. Some of these classes will have certain sections marked with a comment: // TODO: Implement. These sections require you to fill in the remaining implementation to complete the class. In some cases, you will need to add and fully implement classes yourself -- in particular, subclasses of Statement and Expression. Once again, I’ve provided interfaces for the various classes that you should not modify. The signatures for all public methods other than constructors cannot be changed. That includes visibility modifiers, method names, parameter lists, and return types. Changing these will result in you losing points on the assignment. In addition, any instance variables you declare in any classes for the assignment must be marked private. Failure to do so will also result in points being deducted. Over the next few sections of this document, I’ll describe the order in which I would recommend approaching the assignment. BlockStatement and WhileStatement The first feature you should try to add to the Interpreter is the WhileStatement. while x < 10:="" print(x)="" x="x" +="" 1="" in="" this="" example,="" the="" body="" of="" the="" while="" loop="" (print(x)="" and="" x="x" +="" 1)="" will="" repeatedly="" run="" as="" long="" as="" the="" condition="" (x="">< 10)="" is="" true.="" the="" whilestatement="" has="" an="" extremely="" similar="" structure="" to="" the="" ifstatement.="" they="" both="" have="" boolean="" conditions="" (represented="" by="" the="" condition="" class="" and="" its="" subclasses),="" and="" they="" both="" have="" bodies.="" for="" that="" reason,="" you="" are="" required="" to="" find="" a="" way="" to="" minimize="" code="" duplication="" between="" the="" two="" classes.="" i="" suggest="" using="" a="" common,="" abstract="" superclass="" (such="" as="" blockstatement)="" which="" would="" still="" implement="" the="" statement="" interface,="" but="" would="" contain="" everything="" common="" to="" ifstatement="" and="" whilestatement.="" this="" is="" somewhat="" similar="" to="" the="" relationship="" between="" arithmeticexpression="" (an="" abstract="" class)="" and="" its="" subclasses,="" addexpression,="" subtractexpression,="" etc.="" in="" that="" example,="" arithmeticexpression="" stores="" what="" is="" common="" to="" all="" of="" its="" subclasses,="" while="" the="" concrete="" subclasses="" know="" how="" to="" implement="" the="" actual="" evaluate="" behavior.="" likewise,="" blockstatement="" can="" store="" what="" is="" common="" between="" ifstatement="" and="" whilestatement,="" whereas="" ifstatement="" and="" whilestatement="" know="" how="" to="" implement="" the="" actual="" run="" behavior="" that="" determines="" what="" happens="" when="" those="" statements="" are="" run.="" see="" lecture="" 17="" for="" a="" more="" detailed="" description="" of="" how="" blockstatement="" and="" whilestatement="" can="" be="" implemented.="" once="" finished,="" you="" should="" be="" able="" to="" run="" the="" “programs/simple-while-statement.txt”="" and="" “programs/complex-while-statement.txt”="" programs.="" forstatement="" the="" forstatement="" has="" some="" similarities="" to="" the="" whilestatement,="" in="" that="" they’re="" both="" types="" of="" loops,="" but="" the="" forstatement="" expresses="" the="" way="" it="" loops="" differently.="" rather="" than="" using="" a="" condition,="" the="" forstatement="" uses="" range="" syntax:="" for="" x="" in="" range(0,="" 10):="" print(x)="" this="" indicates="" that="" the="" body="" of="" the="" loop="" (print(x))="" should="" repeatedly="" run="" some="" number="" of="" iterations.="" before="" each="" iteration,="" the="" loop="" variable="" (x)="" should="" be="" assigned="" a="" value="" from="" the="" range="" (0,="" 10).="" the="" first="" iteration,="" we="" should="" assign="" the="" value="" 0="" to="" x,="" and="" then="" run="" the="" statement="" in="" the="" loop’s="" body.="" the="" second="" iteration,="" we="" should="" assign="" the="" value="" 1="" to="" x,="" and="" then="" run="" the="" statement="" in="" the="" loop’s="" body.="" this="" continues="" until="" all="" values="" in="" the="" range="" have="" been="" assigned="" to="" x.="" a="" few="" important="" things="" to="" keep="" in="" mind="" when="" implementing="" the="" forstatement:="" ●="" the="" loop="" variable="" (x="" in="" the="" example="" above)="" can="" be="" any="" variable="" name.="" ●="" the="" specified="" range="" is="" inclusive="" at="" the="" start="" and="" exclusive="" at="" the="" end.="" that="" means="" that="" the="" loop="" variable="" should="" be="" assigned="" every="" integer="" value="" starting="" at="" the="" range="" start="" value,="" up="" to="" but="" not="" including="" the="" range="" end="" value.="" in="" the="" range(0,="" 10)="" example,="" that="" would="" include="" 0,="" 1,="" 2,="" 3,="" 4,="" 5,="" 6,="" 7,="" 8,="" and="" 9.="" ●="" the="" start="" and="" end="" values="" for="" the="" range="" might="" not="" be="" integer="" constants.="" they="" may="" be="" variables="" (e.g.="" range(0,="" x)).="" for="" that="" reason,="" we="" will="" express="" the="" range="" start="" and="" range="" end="" values="" as="" expressions="" instead,="" which="" can="" be="" evaluated="" to="" integers.="" ●="" the="" range="" start="" and="" range="" end="" expressions="" should="" each="" be="" evaluated="" only="" once="" when="" the="" loop="" runs,="" right="" at="" the="" beginning.="" this="" is="" important="" to="" keep="" in="" mind="" in="" case="" one="" of="" them="" is="" a="" variable="" whose="" value="" is="" changed="" inside="" the="" loop.="" ●="" the="" forstatement="" shares="" some="" similarities="" with="" ifstatement="" and="" whilestatement,="" and="" you="" will="" also="" want="" to="" find="" a="" way="" to="" avoid="" code="" duplication="" between="" them.="" see="" lecture="" 17="" for="" a="" more="" detailed="" description="" of="" how="" forstatement="" can="" be="" implemented.="" once="" finished,="" you="" should="" be="" able="" to="" run="" the="" “programs/simple-for-statement.txt”,="" “programs/complex-for-statement.txt”,="" and="" “programs/fibonacci-iterative.txt”="" programs.="" definefunctionstatement="" the="" definefunctionstatement="" is="" the="" first="" statement="" type="" we’ll="" be="" implementing="" to="" support="" our="" interpreter="" understanding="" functions.="" as="" with="" other="" statement="" classes,="" it="" must="" implement="" the="" statement="" interface.="" in="" python,="" functions="" are="" defined="" as="" follows:="" def="" squared(n):="" product="n" *="" n="" return="" product="" when="" defining="" a="" function,="" we="" provide="" the="" function="" name,="" a="" list="" of="" function="" parameter="" names,="" and="" a="" list="" of="" function="" statements.="" a="" definefunctionstatement="" should="" store="" all="" of="" this="" information.="" it’s="" important="" to="" note="" that="" when="" a="" definefunctionstatement="" is="" run,="" we="" won’t="" immediately="" run="" any="" of="" those="" statements="" inside="" the="" body="" --="" that="" happens="" when="" the="" function="" is="" actually="" called="" (see="" functioncallexpression="" and="" returnstatement="" for="" details="" on="" that).="" instead,="" we’ll="" register="" the="" function’s="" information="" in="" our="" programstate="" object.="" programstate="" has="" these="" new="" methods:="" public="" void="" registerfunction(="" string="" functionname,="">
parameterNames, List functionStatements) { // TODO: Implement. } public List getParameterNames(String functionName) { // TODO: Implement. } public List getFunctionStatements(String functionName) { // TODO: Implement. } The purpose of these methods is to offer our program a way to remember a function’s definition when we encounter it, and to recall that definition later on when we call the function, given only its name. You will need to add instance variables to the ProgramState class which can keep track of function information based on name, and implement these three methods accordingly. A DefineFunctionStatement object stores a function’s information, and when it is run, it will in turn invoke the registerFunction method in ProgramState. That function information is now available for any other part of the program to use. Later on, when we call the function, the ProgramState object will be able to tell us this information given just the function name. See Lecture 18 for a more detailed description of how DefineFunctionStatement can be implemented. Unfortunately, even after DefineFunctionStatement is finished, you will not be able to run any of the programs that use DefineFunctionStatement until ReturnStatement, FunctionCallExpression, and all of the ProgramState changes are implemented. FunctionCallExpression and ReturnStatement A function call is actually an Expression, even though it results in a series of Statements being executed, due to the fact that functions in our limited version of Python all return an integer value. We will represent the actual function call with the FunctionCallExpression class, which will implement the Expression interface. def squared(n): product = n * n return product print(squared(5)) Let’s expand on our previous function example. Here, we are first going to define the squared function using a DefineFunctionStatement as previously described. Then, on the next line, we have a PrintStatement, which is already implemented from Part 1. However, notice that the expression we’re printing out is something new -- it’s a call to the squared function we just defined, passing in a ConstantExpression (5) as a parameter. The entire expression -- squared(5) -- will be represented as a FunctionCallExpression. When we try to evaluate it to figure out what we should print, we go through the following steps: 1. Note the function name ("squared"). 2. Note the list of expressions passed in as parameters (one ConstantExpression, 5). 3. Look up the list of parameter names for this function. We can use getParameterNames, which we implemented in ProgramState earlier. 4. Match up the list of parameter expressions from step 2 to the list of parameter names from step 3. Each parameter expression should be evaluated to an integer, and assigned to the corresponding parameter name. In the above example, we would assign 5 to n. 5. Look up the list of statements for this function. We can use getFunctionStatements, which we implemented in ProgramState earlier. 6. Iterate through the list of statements from step 5, one after another, and call the run method on each of them. In our limited version of Python, every function must have a return statement that returns an Expression which can be evaluated to an integer. You can see an example of it above: return product We will represent these statements with the ReturnStatement type, another new class that will implement the Statement interface. On the surface, it seems simpler than other statement types we’ve implemented, because it only involves a single Expression. However, the side effect of running a ReturnStatement is a bit more complicated. ProgramState has the following new methods. You must add any new necessary instance variables and then implement the methods: public boolean hasReturnValue() { // TODO: Implement. return false; } public int getReturnValue() { // TODO: Implement. return 0; } public void setReturnValue(int returnValue) { // TODO: Implement. } public void clearReturnValue() { // TODO: Implement. } These methods will come in handy when we implement ReturnStatement and utilize return values in FunctionCallExpression. When a FunctionCallExpression is being evaluated, we expect that function’s statements to run until a ReturnStatement is encountered. Once it has been run, the FunctionCallExpression must recognize that a value has been returned, stop running the statements in the function, and use that returned value as the integer it evaluates to. Returning to our previous example: def squared(n): product = n * n return product print(squared(5)) When the FunctionCallExpression representing squared(5) is evaluated, it should look up the information about squared, set up the parameter n, and then run the two