Question

Consider AddE class listed here. a. Identify the stack-dynamic variables in this class. b. Identify the...

Consider AddE class listed here.

a. Identify the stack-dynamic variables in this class.
b. Identify the heap-dynamic variables in this class.
c. Identify all occurrences of function names in this class that are bound to body codes during program execution time by means of inheritance polymorphism.
d. Give the types of the functions defined in this class.

import java.util.*;

class AddE extends E
{
   // Term term; inherited from E

   E e;

   AddE(Term t, E e_)
   {
       term = t;
       e = e_;
   }

   void printParseTree(String indent)
   {
       String indent1 = indent + " ";

       IO.displayln(indent + indent.length() + " <E>");
       term.printParseTree(indent1);
       IO.displayln(indent1 + indent1.length() + " +");
       e.printParseTree(indent1);
   }

   Val Eval(HashMap<String,Val> state)
   {
       Val termVal = term.Eval(state);
       Val eVal = e.Eval(state);
       if ( termVal == null || eVal == null )
           return null;
      
       // The result will be float if one or both of the arguments are float.
      
       Class termClass = termVal.getClass();
       Class eClass = eVal.getClass();

       if ( termClass == IntVal.class && eClass == IntVal.class )
       {
           ((IntVal)termVal).val = ((IntVal)termVal).val + ((IntVal)eVal).val;
           return termVal;
       }
       else if ( termClass == IntVal.class ) // eClass == FloatVal.class
       {
           ((FloatVal)eVal).val = ((IntVal)termVal).val + ((FloatVal)eVal).val;
           return eVal;
       }
       else // termClass == FloatVal.class
       {
           ((FloatVal)termVal).val = termVal.floatVal() + eVal.floatVal();
           return termVal;
       }      
   }
  
   void emitInstructions()
   {
       term.emitInstructions();
       e.emitInstructions();
       IO.displayln("add");
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a. Stack dynamic variables : indent,indent1 ,state => as they are statically allocated memory as long as it is in the defined scope of the function.

b. Heap dynamic variables : term, e, termVal , eVal, evaltermClass , eClass => as they are all instances created for which memory will be allocated in the heap

c.  function names in this class that are bound to body codes during program execution time by means of inheritance polymorphism.=>

Inheritance we can easily see in the current => class AddE extends E

E is the parent/base class where AddE is inherted.

AddE is the ocntructor for the class.

ValEval

Polymorphism is havinf =g many forms.. SO the same method being called in differnt forms by the means of paramters being passed/ type of parameters.

d. Types of methods:

User defined methods=> void printParseTree,Eval,emitInstructions

These are the ones which user has created in order to perfoirm some operations.

predefined mehods => the one which are defined from the std library.

getClass(),

IO.displayIn()

Add a comment
Know the answer?
Add Answer to:
Consider AddE class listed here. a. Identify the stack-dynamic variables in this class. b. Identify the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT