-------------------------------------------------------------------------- Adaptive Object-Oriented Development Fall 1999 COM 3360 --------------------------------------------------------------------------- Assignment 2 Due date: Thursday, Oct. 14 --------------------------------------------------------------------------- This assignment is stored in file $AOO/hw/2 in file assign.txt -------------------------------------------------------------------------- You may turn in your hw for full credit up to two working days late. This means, if it is due on Thursday, you can turn it in the following Monday. If it is due on day x, it means midnight of day x. Doug Orleans (dougo) is the grader for this class. He is the key developer for Demeter/Java and he also developed a good part of DJ. He maintains both systems. For late homework we will deduct 10% per day late, unless you present a very good reason to Doug. Although we have such a rule, we are flexible especially if you study part-time. ========= On CCS Northeastern machines, D=/proj/adaptive/www/ OO=$D/course/ AOO=/proj/adaptive/www/course/f99/ On the WWW, D = http://www.ccs.neu.edu/research/demeter/ On the WWW, OO = http://www.ccs.neu.edu/research/demeter/course/ On the WWW, AOO = http://www.ccs.neu.edu/research/demeter/course/f99 Further useful URLs are on page 589 of the AP book. ========= THEMES: Evolution of Java programs change design, translate to Java READING: Read chapters 5, 6, 7 of the AP book. Read Java book as necessary to understand enough of the Java programs which you need to modify in this homework. This homework requires use of Demeter/Java. Browse the resource guide at: $D/DemeterJava/ Read about related tools which promote a traversal/visitor style to programming at: http://www.ccs.neu.edu/home/lieber/suntest.html http://www.suntest.com/JavaCC/DOC/JJTree.html A quote from the above URL: ==================== Visitor Support JJTree provides some basic support for the visitor design pattern. If the VISITOR option is set to true JJTree will insert an jjtAccept() method into all of the node classes it generates, and also generate a visitor interface that can be implemented and passed to the nodes to accept. ==================== Read about Java packages in your favorite Java book. ================= TO TURN IN YOUR HW, please use the submit3360 command (or equivalent on your PC) which packages your directories and mails them to: com3360-grader@ccs.neu.edu Question 1: ----------- PART 1.A Motivation: Learn to modify simple adaptive programs. Note: This part can be solved two ways: By going directly after the file $AOO/hw/2/capacity-check/statistics.beh and finding out what is wrong with it or by looking at the generated Java code and find out what is wrong. You can solve it either way. The goal is to learn to edit the behavior files directly without even looking at the generated code. Consider the program gen/*.java in $AOO/hw/2/capacity-check NOTE: Please regenerate the gen directory using Demeter/Java. I use the jikes compiler for compilation. If you use another one, you have to modify file program.prj accordingly // The Java compiler executable. COMPILER = jikes // Java compiler arguments. COMPILE_ARGS = -g -depend +E would need to be changed for your compiler of choice. See http://www.ccs.neu.edu/research/demeter/course/f99/runningjava.html for set-up information. The purpose of this program is to read in a description of nested containers and to build a Java object. The program then checks whether each container satisfies its capacity restriction. Example: Consider the following container description in $AOO/hw/2/capacity-check/statistics.input ( //begin container 1 apple 1 //item ( //begin container 2 pencil 1 //item ( //begin container 3 apple 1 //item 1) //capacity orange 1 1) //capacity orange 1 kiwi 1 5) //capacity ================= Container 2 is over capacity since it contains three items with a total weight of 3, but the capacity is one. Container 1 is also over capacity. It contains 6 items with a total weight of 6, but the capacity is 5. Container 3 is within capacity. For each container which is over capacity, the program should print a message of the form: total weight 2 but limit is = 1 OVER CAPACITY The program capacity-check/gen/*.java contains a wrinkle: The wrinkle is conceptual in that the program gives wrong results: it does not detect all containers which are over capacity. Your task is to make the Java program capacity-check/gen/*.java work and to figure out what is wrong. However, you don't turn in the corrected Java program, but instead the corrected program statistics.beh. In other words, you have to "lift" the correction you did to the Java program to the adaptive Java program in statistics.beh. Demeter/Java has a project file facility which works pretty much the same on any platform (the file paths need to be adjusted from platform to platform). Typically, the project file is called program.prj A default program.prj file is generated by "demjava new". I changed three lines in the default file: CDFILE = statistics.cd BEHFILES = statistics.beh TEST_INPUT = statistics.input You can do this much easier by just saying "demjava new statistics". This creates a project named statistics, i.e., a project file named statistics.prj with these three variables set as shown above. If you use Demeter/Java on a PC with Windows and you copy my program.prj, make sure you change the / to \ in the file paths. Also, you need to replace javacc by javacc.bat. demjava test allows you to test the statistics.beh file after modification. Turn in the corrected program statistics.beh and the output produced for file statistics.input. If you are unable to perform the reverse enginering (going from capacity-check/gen/*.java to statistics.beh), you may hand in the capacity-check/gen/*.java files for partial credit. PART 1.B Motivation: Learn to modify simple adaptive programs, learn about Java wrapper classes and class java.util.Stack. Learn about benefits of Selective Visitor pattern. Consider the Java program in directory capacity-with-stack. This program solves the capacity checking problem correctly using a Java stack and the program is a Java package called RecursiveContainer (see class dictionary; note that in the program.prj file the package name also has to be set to RecursiveContainer. PACKAGE = RecursiveContainer ). The program is written in terms of the Java wrapper class Integer. Read in your favorite Java book about wrapper classes. You need them, for example, to work with Java hash tables or Java collection classes. This program uses a wrapper class since earlier versions of Demeter/Java required this. Now you can use type int instead of type Integer. Replace throughout the program, as much as possible, all occurences of Integer by int and adapt the Java program. Why cannot you eliminate all occurrences of Integer in the behavior file? You should update the statistics.cd and statistics.beh files and regenerate the Java code using Demeter/Java. Turn in your modified files statistics.cd and statistics.beh. Question 2: ----------- Motivation: Learn about how an adaptive program defines a family of object-oriented programs. Learn how to modify one adaptive program instead of two object-oriented programs. Consider the Java programs gen/*.java in average-default-list/ average-other-list/ They both do the same "thing" which is described in the design statistics.beh which is the same in both directories. Both Java programs print out the running average as Weight-objects are encountered during the traversal. Change the programs so that they print the average for each subcontainer. Example: For input: ( apple 10 ( apple 10 kiwi 6 10) pear 9 ( apple 7 10) kiwi 6 50) the program should print three partial results: 8 // average for first sub-container 7 // average for second sub-container 8 // (10 + 8 + 9 + 7 + 6)/5 = average for total container However, you don't turn in the two corrected Java programs but you lift the corrections in the two changed programs average-default-list/gen/*.java average-other-list/gen/*.java to the file: statistics.beh which is the same for both programs. Turn in the modified file statistics.beh. If you are unable to perform the reverse enginering (going from *.java to *.beh), you may hand in the *.java files for partial credit. ====================== System help: http://www.ccs.neu.edu/research/demeter/course/f99/runningjava.html http://www.ccs.neu.edu/research/demeter/DemeterJava/setup.html We are using Java 1.2 (= Java 2). When you think the system behaves unexpectedly, use demjava clean A general, shell-independent solution using the .software file is described below: From dougo@ccs.neu.edu Tue Oct 6 02:13:36 1998 From: Doug Orleans Run 'software -v' to see how exactly your .software file expands into your path. Here's the relevant portion of my .software, for reference: # ~/bin stuff: home # Demeter stuff: PATH=/proj/demsys/demjava/bin PATH=/proj/demsys/bin # jikes stuff JIKESPATH=/usr/java1.2/jre/lib/rt.jar # Prefer Gnu tools to system ones # and to get JDK 1.1.5 @gnu-all # Put the current directory last. dot --Doug Use "resoft" and "rehash" after you changed your .software file.