The "food visitor" Assignment Due one week from date assigned ****************** In Adams 204 Linux Lab, do /opt/mono/bin/gmcs program.cs (to compile) /opt/mono/bin/gmcs mono program.exe (to run) add /t:library for .dll target, and /r:blah.dll for .dll source. ****************** For this assignment you need to download: foods.cs : contains food item classes In addition, the file foodvisitors.cs and aboutfood.cs contain examples of how to define visitors and how to use them. You are to: 0. in a separate file (but with namespace foodstuff declaration), write another visitor that adds up all the calories in a list of food items. You must return the sum when you reach "nofood". 1.Add another fooditem class - such as beverage, with unique elements compared to fruit, vegetable and meat. Now, if you add the new "visit" declaration to the foodvisitor interface, you would have to change all existing visitors (in foodvisitors.cs) to also account for the new subclass. You may do it this way at first. HOWEVER, you must also do it the following way (look at the "weirdstudent example" for hints) create a new interface that extend the existing interfaces public interface extendeditem // think about whether this should // extend the fooditem interface public interface extendedvisitor : foodvisitor You'll need to specify the new class you're going to visit in the extendedvisitor interface, and your extended visitor classes will visit all fooditems as well as all new fooditems. However, to make this mechanism work, you'll have to really be aware of all the subtleties of dynamic v.s. static types and inheritance. Try to define an adaptor class that will allow you to use an extendeditem as a regular food item. Doing the excercise this way will help you see what happens to OOP when it's pushed to the limit. 2. (OPTIONAL) Write a visitor class patterned after the foodvisistors I gave you, which visits each food item in the list and output the data in xml format. For brief intro to xml goto www.w3schools.com/xml/default.asp and read the first few sections of the tutorial. It's mostly self-explanatory. The xml file you generate should conform to the format shown in the sample file (fff.xml on homepage) that I gave you. You need to make up a valid xml format for your new fooditem (, etc ...). Your output must also have reasonable indentations (similar to mine). To open a file and write to it in C#, do the following: using System.IO; // at the top, along with using System; ... FileStream fs = new FileStream(@"filename",FileMode.OpenOrCreate,FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.Write("hello\n"); // use sw just like Console sw.WriteLine("world"); ... // when done: sw.Flush(); sw.Close(); To write the " character, use sw.Write("\""); In the aboutfood.cs example I used the xmlwriter class I created myself, which takes a StreamWriter object as a constructor parameter. That's because I may not want to write to just a local file, but also to a network socket or some other IO channel. The word "stream" is another layer of abstraction, like most everything else in computer science. 3. A food list is "ordered" if all fruits come before vegetables and all vegetables come before meats. Write a visitor class that determines if a food list is ordered, that is, (bool)myFoodList.accept(new OrderCheckerVisitor()) should be true or false. Hint: Here's one possible algorithm: you should have an instance variable that represents the "mode" you're in. That is, whether you are looking for more fruits, vegetables, or meat items. For example, if the current mode is vegetables and you find a meat item, then the mode changes to looking for more meat items. But if the current mode is vegetables and you find a fruit, you should return false immediately. Be aware that a list may also not have any fruits, or vegetables or meats, and can still be ordered. nofood by itself is ordered. Demonstrate the visitor on both ordered and unordered lists. For this problem, you may optionally ignore your new food class, or extend the program so that all instances of the new class must come after meat. 4. Make your visitor pattern parametrically (generically) typed. Consult the fourth expression tree program. The return type of visit methods (and the accept method) should be changed from "object" to a type variable. Make sure you understand what the potential advantages of this approach are. See the program "exptree5.cs" (Parametrized VP version of the expression tree program) if you're stuck. Remember that it is possible to parameterize an interface, a class, or just a single function within a class. You'll probably need the latter form.