1. (13 points) Think about the full object-oriented version of the CORE interpre
ID: 3805267 • Letter: 1
Question
1. (13 points) Think about the full object-oriented version of the CORE interpreter that we've discussed in class. Suppose we wanted to use polymorphism in the implementation of the Stmt class. That is nstead of having a Stmt class that is a container for another node type (Assign, In, Out, If or Loop) we want to have Stmt be an abstract class that defines the methods parse, execute and print. Assign, In, Out, lf and Loop will be derived classes of the Stmt abstract class. (In Java terminology, Stmt will be an interface and the others will be classes that implement the Stmt interface, in C++ terminology Stmt will be a purely abstract class containing only virtual methods) Provide in pseudocode or in Java or C++ a definition of the abstract class/interface Stmt mplementations of the classes derived from Stmt (including their parse, print and execute methods) and the implementation of the StmtSeq class that Stmt stra bars for eg ons od willExplanation / Answer
import java.util.*;
//Below we define the interface named stmt.
public interface stmt
{
//Here we declare all the methods i.e. parse, execute, print.
void parse();
void execute();
void print();
}
//Here we define derived class Assign
class Assign implements stmt
{
/* Here you can call methods declared in the interface stmt, to show you how to call methods in interface I have called parse method in Assign derived class. You can call all the three methods here or as per requirement by you.*/
void parse()
{
//Here goes the code for parse method as per requirement.
}
}
//Here I define derived class In
class In implements stmt
{
/* Same as above only a execute method has been called here. As per requirement you can call any method here.*/
void execute()
{
//Here code for the execute method goes, as per required by you.
}
}
//Same as above, Out derived class has been defined
class Out implements stmt
{
// Here I have called a print method just for example. You can call other methods also.
void print()
{
// Code for the print method will go here.
}
}
//Here we defined derived class If
class If implements stmt
{
// Code for the If class can be written here.
}
//Same as above, Loop derived class has been defined
class Loop implements stmt
{
// Code for the Loop method goes here. As seen above derived methods you can call any method from the stmt
}
/*Below is main function class, from this class we can call all other class methods.
Here you can call class methods in interface class. I will show you how to call a methods which are defined in a stmt interface.*/
public class stmtSeq
{
public static void main(String args[])
{
// Below a object has been created for the Assign class, the name of an object is "a".
// You can use this object to call methods from the interface stmt
Assign a=new Assign();
// Below, using object 'a', a methd defined in a interface stmt has been called
a.parse();
// Below a object has been created for the In class, the name of an object is "i".
// You can use this object to call methods from the interface stmt
In i=new In();
// Below, using object 'i', a methd defined in a interface stmt has been called
i.execute();
// Below a object has been created for the Out class, the name of an object is "o".
// You can use this object to call methods from the interface stmt
Out o=new Out();
// Below, using object 'o', a methd defined in a interface stmt has been called
o.print();
// Below a object has been created for the If class, the name of an object is "ii".
// You can use this object to call methods from the interface stmt
If ii=new If();
// Below, object has been created for the Loop class, the name of an object is "l".
// You can use this object to call methods from the interface stmt
Loop l=new Loop();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.