Note: has two additional questions added to the undergraduate midterm in ../m-1205-f96 /proj/asl/lieber/java/java-envs/jk-sys-simpler ======================= Adaptive Object-Oriented Software Fall 1996 COM 3360/NTU SE737 Karl Lieberherr --------------------------------------------------------------------------- Midterm Oct. 31, 1996 Question 1: 50 points Question 2: 80 points Question 3: 80 points Question 4: 120 points -------------- Total 330 points Open book and open notes. One hour. For all questions we use an extension of the class dictionary below. This class dictionary does not show some of the visitor classes. A visual form of the class dictionary is also available. ------------------------------------------------------------- Class Dictionary demjava.cd ------------------------------------------------------------- 1 // demjava.cd -- class dictionary for Demeter/Java 2 3 Program = [ JavaCode ] ClassGraph 4 [ Dictionary]. 5 6 ClassGraph = DList(ClassDef). 7 8 ClassDef = ParamClassName ClassParts [ ClassMethods ] "." 9 [ DFS_mark ] [ PP_mark ] . 10 11 ParamClassName = ClassName ["(" Commalist(ClassName) ")"]. 12 13 ClassParts : ConstOrAltClass | RepetitionClass. 14 15 ConstOrAltClass : ConstructionClass | AlternationClass 16 *common* + + + 17 List(PartOrSyntax) 18 ClassParents 19 - - -. 20 21 PartOrSyntax : Part | OptionalPart | Syntax. 22 23 Part = [ "<" PartName ">" ] ClassSpec. 24 25 OptionalPart = "[" Sandwich(Part) "]". 26 27 ClassParents = [ "extends" ClassSpec ] 28 [ "implements" Commalist(ClassSpec) ] . 29 30 ConstructionClass = "=". 31 32 AlternationClass = ":" + + + Barlist(Subclass) - - - 33 [ Common ]. 34 35 Subclass = ClassSpec. 36 37 Common = *l + + + "*" "common" "*" - - -. 38 39 RepetitionClass = "~" Sandwich(RepeatedPart). 40 41 RepeatedPart = [ ClassSpec ] 42 "{" Sandwich(ClassSpec) "}". 43 44 Sandwich(S) = List(Syntax) S List(Syntax) . 45 46 ClassSpec = ClassName 47 ["(" Commalist(ClassSpec) ")" ] 48 [ PP_mark ] 49 . 50 51 Syntax : PlainSyntax | PrintCommand. 52 53 PlainSyntax = String. 54 55 PrintCommand : PrintIndent | PrintUnindent | PrintSkip | PrintSpace. 56 57 PrintIndent = "+" . 58 59 PrintUnindent = "-" . 60 61 PrintSkip = "*l" . 62 63 PrintSpace = "*s" . 64 65 // Marks for traversal bookkeeping. 66 DFS_mark = "*dfs*". 67 PP_mark = "*pp*". 68 69 70 ////////////////////// 71 // Behavior (methods). 72 ////////////////////// 73 ProgramBehavior = [ DList(ClassBehavior) ] . 74 75 ClassBehavior = ClassName ClassMethods. 76 77 ClassMethods = "{" *l + [ SList(Method) ] - "}" . 78 79 Method : Traversal | Wrapper | Verbatim. 80 81 82 // Class graph traversal specifications. 83 Traversal = "traversal" TraversalName TraversalArgs "{" *l 84 + PathDirective ";" - *l 85 "}". 86 87 TraversalArgs = "(" [ Commalist(Visitor) ] ")" . 88 89 Visitor = ClassName VisitorName. 90 91 PathDirective = [ "bypassing" Bypassing ] Target. 92 93 Bypassing = Commalist(Glob). 94 Glob : ClassGlob | EdgeGlob. 95 EdgeGlob : PartGlob | SubclassGlob. 96 97 ClassGlob = ClassNameGlob. 98 PartGlob = "->" ClassNameGlob "," 99 PartNameGlob "," ClassNameGlob. 100 SubclassGlob = "=>" ClassNameGlob "," ClassNameGlob. 101 102 ClassNameGlob : ClassNameExact | AnyClass. 103 ClassNameExact = ClassName. 104 AnyClass = "*". 105 106 PartNameGlob : PartNameExact | AnyPart. 107 PartNameExact = PartName. 108 AnyPart = "*". 109 110 Target : To | ToStop *common* ClassName. 111 112 To = "to". 113 ToStop = "to-stop". 114 115 116 // Before and after wrappers. 117 Wrapper : Before | After *common* ClassName JavaCode. 118 119 Before = "before". 120 After = "after". 121 122 123 // Verbatim java code. 124 Verbatim = JavaCode. 125 126 127 // Terminal buffer classes. 128 ClassName = Ident. 129 PartName = Ident. 130 TraversalName = Ident. 131 VisitorName = Ident. 132 MethodName = Ident. 133 JavaCode = "(@" String "@)". 134 135 136 // Parameterized class definitions. 137 List(S) ~ {S}. 138 SList(S) ~ S { *l S } *l . 139 DList(S) ~ S { *l *l S } *l . 140 Commalist(S) ~ S {"," S}. 141 Barlist(S) ~ S { *l "|" S}. 142 143 // Extra utility parameterized classes. 144 Parameterized = Commalist(Part). 145 SyntaxTable = List(PlainSyntax). 146 147 148 // Visitors. 149 PrintVisitor = . ------------------------------------------------------------- Cross Reference List ------------------------------------------------------------- Program :3 ClassGraph :6 3 ClassDef :8 6 ParamClassName :11 8 ClassParts :13 8 ConstOrAltClass :15 13 PartOrSyntax :21 17 Part :23 21 25 144 OptionalPart :25 21 ClassParents :27 18 ConstructionClass :30 15 AlternationClass :32 15 Subclass :35 32 Common :37 33 RepetitionClass :39 13 RepeatedPart :41 39 Sandwich :44 25 39 42 ClassSpec :46 23 27 28 35 41 42 47 Syntax :51 21 44 44 PlainSyntax :53 51 145 PrintCommand :55 51 PrintIndent :57 55 PrintUnindent :59 55 PrintSkip :61 55 PrintSpace :63 55 DFS_mark :66 9 PP_mark :67 9 48 ProgramBehavior :73 ClassBehavior :75 73 ClassMethods :77 8 75 Method :79 77 Traversal :83 79 TraversalArgs :87 83 Visitor :89 87 PathDirective :91 84 Bypassing :93 91 Glob :94 93 EdgeGlob :95 94 ClassGlob :97 94 PartGlob :98 95 SubclassGlob :100 95 ClassNameGlob :102 97 98 99 100 100 ClassNameExact :103 102 AnyClass :104 102 PartNameGlob :106 99 PartNameExact :107 106 AnyPart :108 106 Target :110 91 To :112 110 ToStop :113 110 Wrapper :117 79 Before :119 117 After :120 117 Verbatim :124 79 ClassName :128 11 11 46 75 89 103 110 117 PartName :129 23 107 TraversalName :130 83 VisitorName :131 89 MethodName :132 JavaCode :133 3 117 124 List :137 17 44 44 145 SList :138 77 DList :139 6 73 Commalist :140 11 28 47 87 93 144 Barlist :141 32 Parameterized :144 SyntaxTable :145 PrintVisitor :149 ------------------------------------------------------------- Alphabetically Sorted Cross Reference List ------------------------------------------------------------- After :120 117 AlternationClass :32 15 AnyClass :104 102 AnyPart :108 106 Barlist :141 32 Before :119 117 Bypassing :93 91 ClassBehavior :75 73 ClassDef :8 6 ClassGlob :97 94 ClassGraph :6 3 ClassMethods :77 8 75 ClassName :128 11 11 46 75 89 103 110 117 ClassNameExact :103 102 ClassNameGlob :102 97 98 99 100 100 ClassParents :27 18 ClassParts :13 8 ClassSpec :46 23 27 28 35 41 42 47 Commalist :140 11 28 47 87 93 144 Common :37 33 ConstOrAltClass :15 13 ConstructionClass :30 15 DFS_mark :66 9 DList :139 6 73 EdgeGlob :95 94 Glob :94 93 JavaCode :133 3 117 124 List :137 17 44 44 145 Method :79 77 MethodName :132 OptionalPart :25 21 PP_mark :67 9 48 ParamClassName :11 8 Parameterized :144 Part :23 21 25 144 PartGlob :98 95 PartName :129 23 107 PartNameExact :107 106 PartNameGlob :106 99 PartOrSyntax :21 17 PathDirective :91 84 PlainSyntax :53 51 145 PrintCommand :55 51 PrintIndent :57 55 PrintSkip :61 55 PrintSpace :63 55 PrintUnindent :59 55 PrintVisitor :149 Program :3 ProgramBehavior :73 RepeatedPart :41 39 RepetitionClass :39 13 SList :138 77 Sandwich :44 25 39 42 Subclass :35 32 SubclassGlob :100 95 Syntax :51 21 44 44 SyntaxTable :145 Target :110 91 To :112 110 ToStop :113 110 Traversal :83 79 TraversalArgs :87 83 TraversalName :130 83 Verbatim :124 79 Visitor :89 87 VisitorName :131 89 Wrapper :117 79 Question 1: ========================================================================== ========================================================================== 50 points (see /proj/asl/lieber/java/java-envs/j-mine-syntax/c-plus-plus) For class dictionary demjava.cd, what is the sentence corresponding to the following object: : Program ( < classgraph > : ClassGraph ( < classes > : ClassDef_DList { : ClassDef ( < paramclassname > : ParamClassName ( < classname > : ClassName ( < name > : Ident "A" ) ) < classparts > : ConstructionClass ( < parts > : PartOrSyntax_List { : Part ( < classspec > : ClassSpec ( < classname > : ClassName ( < name > : Ident "B" ) ) ) } < parents > : ClassParents ( ) ) ) , : ClassDef ( < paramclassname > : ParamClassName ( < classname > : ClassName ( < name > : Ident "B" ) ) < classparts > : AlternationClass ( < subclasses > : Subclass_Barlist { : Subclass ( < classspec > : ClassSpec ( < classname > : ClassName ( < name > : Ident "C" ) ) ) , : Subclass ( < classspec > : ClassSpec ( < classname > : ClassName ( < name > : Ident "D" ) ) ) } < common > : Common ( ) < parts > : PartOrSyntax_List { : PlainSyntax ( < string > : String "." ) , : OptionalPart ( < part > : Part_Sandwich ( < first > : Syntax_List { } < inner > : Part ( < classspec > : ClassSpec ( < classname > : ClassName ( < name > : Ident "Y" ) ) ) < second > : Syntax_List { } ) ) , : PlainSyntax ( < string > : String ":" ) } < parents > : ClassParents ( ) ) ) , : ClassDef ( < paramclassname > : ParamClassName ( < classname > : ClassName ( < name > : Ident "X" ) ) < classparts > : RepetitionClass ( < sandwiched > : RepeatedPart_Sandwich ( < first > : Syntax_List { } < inner > : RepeatedPart ( < nonempty > : ClassSpec ( < classname > : ClassName ( < name > : Ident "R" ) ) < repeated > : ClassSpec_Sandwich ( < first > : Syntax_List { } < inner > : ClassSpec ( < classname > : ClassName ( < name > : Ident "R" ) ) < second > : Syntax_List { } ) ) < second > : Syntax_List { } ) ) ) } ) ) Question 2 ========================================================================== ========================================================================== 80 points (see /proj/asl/lieber/java/java-envs/j-mine) In this question you are given an adaptive program and you are asked to modify the behavior. The program below prints out the names of all construction classes and the total number of construction classes. The program uses the following visitor classes: CountingVisitor = Integer. ClassNameRetVisitor = ClassName. ClassNameTranspVisitor = ParamClassName. PrintConsClassesVisitor = ClassNameTranspVisitor. For example, for input (this input is also used in later questions) //================================================== // computer.cd -- class dictionary for Computer example Computer = List(Equipment). List(X) ~ { X }. Equipment : Card | Drive | FloppyDisk | CompositeEquipment *common* Currency "discount" Currency. Card = "card" . Drive = "drive" . FloppyDisk = "floppy" . CompositeEquipment : Cabinet | Chassis | Bus *common* "(" List(Equipment) ")". Cabinet = "cabinet" . Chassis = "chassis" . Bus = "bus" . PricingVisitor = Currency. Currency = "$" Integer. InventoryVisitor = Inventory. Inventory = List(Equipment). Main = . Test(S) = S. //================================================== the program produces the following output: construction class Computer construction class Card construction class Drive construction class FloppyDisk construction class Cabinet construction class Chassis construction class Bus construction class PricingVisitor construction class Currency construction class InventoryVisitor construction class Inventory construction class Main construction class Test 13 = number of construction classes ======================================== Describe how to change the program so that it produces the following output, i.e., the program needs to number the classes: 1. construction class Computer 2. construction class Card 3. construction class Drive 4. construction class FloppyDisk 5. construction class Cabinet 6. construction class Chassis 7. construction class Bus 8. construction class PricingVisitor 9. construction class Currency 10. construction class InventoryVisitor 11. construction class Inventory 12. construction class Main 13. construction class Test 13 = number of construction classes Describe how to modify demjava.beh (and demjava.cd if you need more visitors) or demjava.java. (it is preferrable that you modify demjava.beh since it is shorter and simpler.) Below we give demjava.beh and the relevant parts of demjava.java. demjava.beh Program { (@ /** Prints all construction class names in a given class graph. A program to show second order visitors. Requires a visitor to traverse a visitor. Also counts the total number of construction classes. */ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.print_and_count_cons_classes(); } @) (@ public void print_and_count_cons_classes() { ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv); CountingVisitor cv = new CountingVisitor( new Integer(0)); this.toAllConstructionClasses(pcv,cntv,cv); } @) traversal toAllConstructionClasses( PrintConsClassesVisitor pcv, ClassNameTranspVisitor cntv, CountingVisitor cv ) { to ConstructionClass; } } CountingVisitor { before ConstructionClass (@ this.set_total(new Integer(total.intValue() + 1)); @) after Program (@ System.out.println(this.get_total().intValue() + " = number of construction classes"); @) } ClassNameTranspVisitor { before ClassDef (@ this.set_pcn(host.get_paramclassname()); @)} ClassNameRetVisitor { before ClassName (@ this.set_cn(host); @)} PrintConsClassesVisitor { traversal toClassName( ClassNameRetVisitor cnrv) { bypassing -> *,parameters,* to ClassName;} (@ /** Retrieve the class name from the visitor. Requires a traversal from the visitor class which itself needs a visitor. */ public ClassName dig_out() { ClassNameRetVisitor cnrv = new ClassNameRetVisitor(); this.toClassName(cnrv); return cnrv.get_cn(); } @) before ConstructionClass (@ System.out.println("construction class " + this.dig_out().get_name()) ; @) } // this.get_cntv().get_pcn().get_classname().get_name()) ; @) } Relevant parts of demjava.java import demeter.*; class CountingVisitor { private Integer total; public Integer get_total() { return total; } public Integer set_total(Integer total) { ... } CountingVisitor() { } public CountingVisitor(Integer total ) { ... } ... public void before(ConstructionClass host) { this.set_total(new Integer(total.intValue() + 1)); } public void after(Program host) { System.out.println(this.get_total().intValue() + " = number of construction classes"); } } class ClassNameRetVisitor { private ClassName cn; public ClassName get_cn() { return cn; } public ClassName set_cn(ClassName cn) ... ClassNameRetVisitor() { } public ClassNameRetVisitor(ClassName cn ) ... public void before(ClassName host) { this.set_cn(host); } } class ClassNameTranspVisitor { private ParamClassName pcn; public ParamClassName get_pcn() { return pcn; } public ParamClassName set_pcn(ParamClassName pcn) ... ClassNameTranspVisitor() { } public ClassNameTranspVisitor(ParamClassName pcn ) ... public void before(ClassDef host) { this.set_pcn(host.get_paramclassname()); } public void toClassName_traverse ( ClassNameRetVisitor cnrv ) { pcn.toClassName_traverse(cnrv); } } class PrintConsClassesVisitor { private ClassNameTranspVisitor cntv; public ClassNameTranspVisitor get_cntv() { return cntv; } public ClassNameTranspVisitor set_cntv(ClassNameTranspVisitor cntv) ... PrintConsClassesVisitor() { } public PrintConsClassesVisitor(ClassNameTranspVisitor cntv ) ... /** Retrieve the class name from the visitor. Requires a traversal from the visitor class which itself needs a visitor. */ public ClassName dig_out() { ClassNameRetVisitor cnrv = new ClassNameRetVisitor(); this.toClassName(cnrv); return cnrv.get_cn(); } public void before(ConstructionClass host) { System.out.println("construction class " + this.dig_out().get_name()) ; } public void toClassName( ClassNameRetVisitor cnrv ) { this.toClassName_traverse(cnrv); } public void toClassName_traverse ( ClassNameRetVisitor cnrv ) { cntv.toClassName_traverse(cnrv); } } class Program { private JavaCode preamble; public JavaCode get_preamble() { return preamble; } public JavaCode set_preamble(JavaCode preamble) ... private ClassGraph classgraph; public ClassGraph get_classgraph() { return classgraph; } public ClassGraph set_classgraph(ClassGraph classgraph) ... private Dictionary d; public Dictionary get_d() { return d; } public Dictionary set_d(Dictionary d) ... Program() { } public Program(JavaCode preamble , ClassGraph classgraph , Dictionary d ) ... /** Prints all construction class names in a given class graph. A program to show second order visitors. Requires a visitor to traverse a visitor. Also counts the total number of construction classes. */ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.print_and_count_cons_classes(); } public void print_and_count_cons_classes() { ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv); CountingVisitor cv = new CountingVisitor( new Integer(0)); this.toAllConstructionClasses(pcv,cntv,cv); } public void toAllConstructionClasses( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { this.toAllConstructionClasses_traverse(pcv, cntv, cv); } public void toAllConstructionClasses_traverse ( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { classgraph.toAllConstructionClasses_traverse(pcv, cntv, cv); cv.after(this); } } class ClassGraph { private ClassDef_DList classes; public ClassDef_DList get_classes() { return classes; } public ClassDef_DList set_classes(ClassDef_DList classes) { ClassDef_DList old_classes = this.classes; this.classes = classes; return old_classes; } ClassGraph() { } public ClassGraph(ClassDef_DList classes ) ... public void toAllConstructionClasses_traverse ( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { classes.toAllConstructionClasses_traverse(pcv, cntv, cv); } } class ClassDef { private ParamClassName paramclassname; public ParamClassName get_paramclassname() { return paramclassname; } public ParamClassName set_paramclassname(ParamClassName paramclassname) ... private ClassParts classparts; public ClassParts get_classparts() { return classparts; } public ClassParts set_classparts(ClassParts classparts) ... private ClassMethods classmethods; public ClassMethods get_classmethods() { return classmethods; } public ClassMethods set_classmethods(ClassMethods classmethods) ... ClassDef() { } public ClassDef( ParamClassName paramclassname , ClassParts classparts , ClassMethods classmethods , DFS_mark dfs_mark , PP_mark pp_mark ) { super(); set_paramclassname(paramclassname); set_classparts(classparts); set_classmethods(classmethods); set_dfs_mark(dfs_mark); set_pp_mark(pp_mark); } public void toAllConstructionClasses_traverse ( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { cntv.before(this); classparts.toAllConstructionClasses_traverse(pcv, cntv, cv); } } class ParamClassName { private ClassName classname; public ClassName get_classname() { return classname; } public ClassName set_classname(ClassName classname) ... private ClassName_Commalist parameters; public ClassName_Commalist get_parameters() { return parameters; } public ClassName_Commalist set_parameters(ClassName_Commalist parameters) ... ParamClassName() { } public ParamClassName( ClassName classname , ClassName_Commalist parameters ) ... public void toClassName_traverse ( ClassNameRetVisitor cnrv ) { classname.toClassName_traverse(cnrv); } } abstract class ClassParts { public ClassParts() { super(); } public void toAllConstructionClasses_traverse ( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { } } abstract class ConstOrAltClass extends ClassParts { private PartOrSyntax_List parts; public PartOrSyntax_List get_parts() { return parts; } public PartOrSyntax_List set_parts(PartOrSyntax_List parts) ... private ClassParents parents; public ClassParents get_parents() { return parents; } public ClassParents set_parents(ClassParents parents) ... ConstOrAltClass() { } public ConstOrAltClass(PartOrSyntax_List parts , ClassParents parents ) { super(); set_parts(parts); set_parents(parents); } public void toAllConstructionClasses_traverse ( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { } } abstract class PartOrSyntax { ... class Part extends PartOrSyntax ... class OptionalPart extends PartOrSyntax ... class ClassParents { ... class ConstructionClass extends ConstOrAltClass { ConstructionClass() { } public ConstructionClass(PartOrSyntax_List parts , ClassParents parents ) ... public void toAllConstructionClasses_traverse ( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { pcv.before(this); cv.before(this); } } class ClassName { private Ident name; public Ident get_name() { return name; } public Ident set_name(Ident name) ... ClassName() { } public ClassName(Ident name ) ... public void toClassName_traverse ( ClassNameRetVisitor cnrv ) { cnrv.before(this); } } class ClassDef_DList { private Nonempty_ClassDef_DList first; public Nonempty_ClassDef_DList get_first() { return first; } public Nonempty_ClassDef_DList set_first(Nonempty_ClassDef_DList first) ... public void toAllConstructionClasses_traverse ( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { first.toAllConstructionClasses_traverse(pcv, cntv, cv); } } class Nonempty_ClassDef_DList { private ClassDef it; public ClassDef get_it() { return it; } public ClassDef set_it(ClassDef it) ... private Nonempty_ClassDef_DList next; public Nonempty_ClassDef_DList get_next() { return next; } public Nonempty_ClassDef_DList set_next(Nonempty_ClassDef_DList next) ... Nonempty_ClassDef_DList() { } public Nonempty_ClassDef_DList(ClassDef it , Nonempty_ClassDef_DList next ) ... public void toAllConstructionClasses_traverse ( PrintConsClassesVisitor pcv , ClassNameTranspVisitor cntv , CountingVisitor cv ) { it.toAllConstructionClasses_traverse(pcv, cntv, cv); if (next != null) next.toAllConstructionClasses_traverse(pcv, cntv, cv); } } Question 3: ========================================================================== ========================================================================== 80 points Modify the program of question 2, so that it prints both construction and alternation classes and the total number of classes: The output for the input computer.cd from question 2, after modification of the program, should be: construction class Computer alternation class Equipment construction class Card construction class Drive construction class FloppyDisk alternation class CompositeEquipment construction class Cabinet construction class Chassis construction class Bus construction class PricingVisitor construction class Currency construction class InventoryVisitor construction class Inventory construction class Main construction class Test 15 = number of classes For your convenience, the program from question 2 is reproduced so that you can write the changes into the program directly: (if you need more visitors indicate how you change demjava.cd) The program uses the following visitor classes: CountingVisitor = Integer. ClassNameRetVisitor = ClassName. ClassNameTranspVisitor = ParamClassName. PrintConsClassesVisitor = ClassNameTranspVisitor. demjava.beh Program { (@ /** Prints all construction class names in a given class graph. A program to show second order visitors. Requires a visitor to traverse a visitor. Also counts the total number of construction classes. */ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.print_and_count_cons_classes(); } @) (@ public void print_and_count_cons_classes() { ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv); CountingVisitor cv = new CountingVisitor( new Integer(0)); this.toAllConstructionClasses(pcv,cntv,cv); } @) traversal toAllConstructionClasses( PrintConsClassesVisitor pcv, ClassNameTranspVisitor cntv, CountingVisitor cv ) { to ConstructionClass; } } CountingVisitor { before ConstructionClass (@ this.set_total(new Integer(total.intValue() + 1)); @) after Program (@ System.out.println(this.get_total().intValue() + " = number of construction classes"); @) } ClassNameTranspVisitor { before ClassDef (@ this.set_pcn(host.get_paramclassname()); @)} ClassNameRetVisitor { before ClassName (@ this.set_cn(host); @)} PrintConsClassesVisitor { traversal toClassName( ClassNameRetVisitor cnrv) { bypassing -> *,parameters,* to ClassName;} (@ /** Retrieve the class name from the visitor. Requires a traversal from the visitor class which itself needs a visitor. */ public ClassName dig_out() { ClassNameRetVisitor cnrv = new ClassNameRetVisitor(); this.toClassName(cnrv); return cnrv.get_cn(); } @) before ConstructionClass (@ System.out.println("construction class " + this.dig_out().get_name()) ; @) } // this.get_cntv().get_pcn().get_classname().get_name()) ; @) } Alternatively you can say precisely how the Java program should be modified. See the Java program in question 2. Question 4: ========================================================================== ========================================================================== 80 points (from /proj/asl/lieber/java/java-envs/j-mine-syntax) There are two questions, one about output and the other about the classes which get a traversal function. Consider the following program: Program { (@ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.toAllPlainSyntax(new PrintVisitor()); } @) traversal toAllPlainSyntax(PrintVisitor v) { to PlainSyntax; } } PrintVisitor { before PlainSyntax (@ System.out.println(host.get_string() ); @) } Question 4.1: ============= 40 points What is the output produced for the following input? (This is the same compute.cd as in question 2.) // computer.cd -- class dictionary for Computer example Computer = List(Equipment). List(X) ~ { X }. Equipment : Card | Drive | FloppyDisk | CompositeEquipment *common* Currency "discount" Currency. Card = "card" . Drive = "drive" . FloppyDisk = "floppy" . CompositeEquipment : Cabinet | Chassis | Bus *common* "(" List(Equipment) ")". Cabinet = "cabinet" . Chassis = "chassis" . Bus = "bus" . PricingVisitor = Currency. Currency = "$" Integer. InventoryVisitor = Inventory. Inventory = List(Equipment). Main = . Test(S) = S. Question 4.2: ============= 40 points For which classes does the above program define a traversal function? In other words, where does traversal toAllPlainSyntax traverse through? Give the class names in alphabetically sorted order. Question 4.3: ============= 20 points Give a sentence which defines an object containing at least 10 subobjects and which is legal with respect to class dictionary computer.cd in 4.1. Question 4.4: ============= 20 points What is the relationship between the Law of Demeter and Adaptive Programming?