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");
}
}
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()
Consider AddE class listed here. a. Identify the stack-dynamic variables in this class. b. Identify the...
Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix) { LinkedStack stack2 = new LinkedStack(); int val1; int val2; int result; for(int i = 0; i < postFix.length(); i++) { char m = postFix.charAt(i); if(Character.isDigit(m)) { stack2.push(m); } else { ...
1) Consider the following Java program: 1 public class HelloWorld { 2 // My first program! 3 public static void main(String[] args) { 4 System.out.println("Hello, World!"); 5 } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...
Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide. Note: AmusementRide.java contains two classes (class FerrisWheel and class RollerCoaster) that provide examples for the class you must create. Your class must include the following. Implementations for all of the abstract methods defined in abstract class AmusementRide. At least one static class variable and at least one instance variable that are not defined in abstract class AmusementRide. Override the inherited repair() method following the...
Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...
The names of the two input files as well as the output file are supposed to be provided as arguments. Could you please fix it? Thanks. DPriorityQueue.java: // Import the required classes import java.io.*; import java.util.*; //Create the class public class DPriorityQueue { //Declare the private members variables. private int type1,type2; private String CostInTime[][], SVertex, DVertex; private List<String> listOfTheNodes; private Set<String> List; private List<Root> ListOfVisitedNode; private HashMap<String, Integer> minimalDistance; private HashMap<String, Integer> distOfVertices; private PriorityQueue<City> priorityQueue; // prove the definition...
Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...
Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...
Multiple Choice Multiple Choice Section 4.1 Pointers and Dynamic Memory Consider the following statements: int *p; int i; int k; i = 42; k = i; p = &i; After these statements, which of the following statements will change the value of i to 75? A. k = 75; B. *k = 75; C. p = 75; D. *p = 75; E. Two or more of the answers will change i to 75. Consider the following statements: int i =...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...