Question: Testing Implement and test the following functions in MiniMac: Tri ( n ) = 1 + 2 + . . . + n / /

Testing
Implement and test the following functions in MiniMac:
Tri(n)=1+2+...+ n // stored in a file called tri
Fib(n)= nth Fibonacci number // stored in a file called fib
Less(n, m)=(n m)?1:0//1= true and 0= false
Log(n)= m where 2m = n and n 2m+1
In each case the output, f(n), should be stored in memory[0] and the inputs should be stored beginning in memory[1].
Hints
1. The MiniMac is a publisher and the view panel is its subscriber. Each time memory is updated or a new program is set, MiniMac notifies its subscribers.
2. The control panel is the action listener for its buttons. It navigates to the MiniMac and calls the appropriate procedure (execute and clear).
3. Parsing is more complicated. The control panel must prompt the user for a file name, read the file (as a string), then pass the string to MiniMaxParser.parse(program). It then sets the MiniMac program to the parsers output, which causes a subscriber notification. Suggestion: ask ChatGPT how to read a text file in Java.
4. Ask ChatGPT for a simple example of how to use a JList.
MiniMacParser.java:
package minimac;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
class ParserUtils {
protected static Integer parseInteger(Iterator it) throws Exception {
if (it.hasNext()){
return Integer.parseInt(it.next());
} else {
throw new Exception("Parsing error, integer expected");
}
}
}
public class MiniMacParser extends ParserUtils {
static public List parse(String program) throws Exception {
List result = new ArrayList();
// split program into a list of tokens (i.e. numbers and operations):
List tokens = Arrays.asList(program.split("\\s*;\\s*|\\s+"));
Iterator it = tokens.iterator();
while (it.hasNext()){
result.add(parseNext(it));
}
return result;
}
// dispatcher method:
static public Instruction parseNext(Iterator it) throws Exception {
String opcode = it.next();
if (opcode.equalsIgnoreCase("halt")) return new Halt();
if (opcode.equalsIgnoreCase("add")) return parseAdd(it);
if (opcode.equalsIgnoreCase("mul")) return parseMul(it);
if (opcode.equalsIgnoreCase("load")) return parseLoad(it);
if (opcode.equalsIgnoreCase("loop")) return parseLoop(it);
if (opcode.equalsIgnoreCase("Bgt")) return parseBgt(it);
if (opcode.equalsIgnoreCase("Blt")) return parseBlt(it);
if (opcode.equalsIgnoreCase("{")) return parseBlock(it);
if (opcode.equalsIgnoreCase("}")) return null; // used to indicate the end of a block
throw new Exception("Parsing error, unrecognized operator: "+ opcode);
}
static protected Add parseAdd(Iterator it) throws Exception {
int src1= parseInteger(it);
int src2= parseInteger(it);
int dest = parseInteger(it);
return new Add(src1, src2, dest);
}
static protected Mul parseMul(Iterator it) throws Exception {
int src1= parseInteger(it);
int src2= parseInteger(it);
int dest = parseInteger(it);
return new Mul(src1, src2, dest);
}
static protected Load parseLoad(Iterator it) throws Exception {
int loc = parseInteger(it);
int value = parseInteger(it);
return new Load(loc, value);
}
static protected Loop parseLoop(Iterator it) throws Exception {
int count = parseInteger(it);
List blockInstructions = new ArrayList>();
if (!it.next().equals("{")){
throw new Exception("Loop must be followed by '{'");
}
Instruction instruction;
while (!(instruction = parseNext(it)).equals("}")){
blockInstructions.add(instruction);
}
return new Loop(condition, blockInstructions);
}
static protected Bgt parseBgt(Iterator it) throws Exception {
int loc = parseInteger(it);
int offset = parseInteger(it);
return new Bgt(loc, offset);
}
static protected Blt parseBlt(Iterator it) throws Exception {
int loc = parseInteger(it);
int offset = parseInteger(it);
return new Blt(loc, offset);
}
static protected Block parseBlock(Iterator it) throws Exception {
List blockInstructions = new ArrayList>();
while (it.hasNext()){
Instruction nextInstruction = parseNext(it);
if (nextInstruction == null){
break;
}
blockInstructions.add(nextInstruction);
}
return new Block(blockInstructions);
}
}
 Testing Implement and test the following functions in MiniMac: Tri(n)=1+2+...+ n

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!