JFormattedTextField | JTable | JSlider |
JRadioButton | JCheckBox | JButton |
JFrame | JPanel | JComponent |
JMenu | JDialog | JContainer |
package extends import virtual method interface type namespaces generic class finally final subtype public Scanner void void * protected subclasses static generational collector composition inheritance iterator stop-and-copy collector Error Throwable Exception RuntimeException IOException event interrupt non-virtual method implements classpath include hotspot compilation import try throw reference counting catch friends polymorphism mark-and-sweep collector Object union vtable setjmp/longjmp
Interior nodes are operator nodes, such as +, -, cos, and unary -, and their children may be either operator or operand nodes. Leaf nodes are operand nodes. All nodes contain a variable named value of type double, that is the result of their last evaluation. All nodes also contain a parent pointer to their parent (a parent node is always an Operator node). Operator nodes contain pointers to their children and a boolean variable named upToDate which indicates if their value is up-to-date. There are three types of operator nodes: single operand operators, such as cos and unary -, binary operand operators, such as +, -, *, and /, and n-ary operand operator nodes that can have arbitrarily many operands (remember that operands may be either operator or operand nodes). All nodes have an Evaluate() method that evaluates its operands and returns a double. For example, the Evaluate() method for plus might look like:
double Evaluate() { value = child1.getValue() + child2.getValue(); return value; }The Evaluate() method for an operand returns the operand's value. Operand nodes have GetValue()/SetValue() methods that allow their value to be retrieved and set. When an Operand's SetValue() method is called, it changes the operand's value and calls the parent's markCacheInvalid method, which recursively chases parent pointers to the root, marking the value of each node it visits as out-of-date. The code for markCacheInvalid might be:
void markCacheInvalid() { upToDate = false; if (parent != null) { parent.markCacheInvalid(); } }Operator nodes have a GetValue() method that allows their value to be retrieved. GetValue() checks to see if the cached value is up-to-date and returns it if it is. Otherwise GetValue() calls Evaluate() to update the cached value and then returns the updated value. The code for GetValue() might be:
double getValue() { if (!upToDate) { Evaluate(); upToDate = true; } return value; }Operator nodes do not have a SetValue method as their value is determined by their Evaluate method.
Finally an n-ary operator has two methods named addParameter and removeParameter that add/remove a child from its children list.
Write the Java code required to create this set of classes. You will need to create one or more additional superclasses that factor out common variables/methods. I want to see:
voterName candidate1 candidate2 ... candidatenEach name is a single word. There must be at least one candidate and there may be as many candidates as the voter wants to list. Do not worry about repeated candidate names.
brad yifan joe sarah nels yifan frank sam sandy yifan joe brad smiley nels bradthen the output should be:
ballot sarah: must have at least one voter and one candidate voter nels has already voted frank 1 brad 1 sam 1 smiley 1 joe 2 yifan 3
Efficiency matters!!! If you use inefficient data structures or inefficient methods for sorting you will lose points.
Here is the beginning of the class. Please note that I have written main and the RepeatVoterException classes for you:
import java.util.*; class Vote { public class RepeatVoterException extends Exception { public String voterName; public RepeatVoterException(String name) { voterName = name; } } public static void main(String args[]) { new Vote(); }