|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java Teaching Basic Object-Oriented Concepts Using HTML
Teaching Basic Object-Oriented Concepts Using HTML
Apr. 1, 2000 12:00 AM
In this article we're going to describe a tool that we've created to help OO newcomers understand the class/instance relationship, inheritance between classes and linking between objects...by automatically converting an object graph into HTML. The tool we've created is based on the "circlegram" idea used by almost every object-oriented teacher during conventional "chalk and blackboard" lessons. Our work at the computer science department of Milano-Bicocca University in Italy is a mix of software design, programming and teaching. We use Unified Modeling Language (UML) for design, Java for programming and blackboards for teaching. When we teach, we also sometimes use overheads/slides. "So what?" you may say, "every teacher in the world does the same...." Right, but we have an added problem: we have to teach Java and general OO (object-oriented) concepts to students at their very first course at the very start of their computer science career. So these students are often a complete tabula rasa they've had no prior programming experience whatsoever. This happens because in Italy we don't have screening or filtering at entrance: every high school student can enter almost any university, and major in whatever he or she likes. There are no formal restrictions or requirements, so a student from a high school where they specialize in Greek and Latin can enroll to study software engineering or physics. (Students can then abandon or change studies as and when they realize their mistake, along the lines of the DOR army mechanism "Dismissed On Request".) Given this situation, you can just imagine how our Java programming course attracts an eclectic group of students: we have students with no prior experience in using a PC, let alone programming! The business world isn't that different. We dare say this because we also teach short Java courses for programmers from a wide variety of companies. They often have prior experience in languages such as COBOL, RPG, C, Visual Basic, and so on. Sometimes (very rarely) we also meet a C++ programmer. But prior knowledge isn't always an advantage: it can be misleading when you try to map a procedural language into an object-oriented one. In both cases (university and business), we face a major problem when teaching Java and OO concepts: we call it the "class/instance problem." Newcomers and experienced procedural programmers alike find it difficult to fully grasp the meaning and the difference between a class and its instances (except of course in the case of a good C++ programmer). Now of course we haven't discovered anything that wasn't already known; it's a problem that's been addressed before see for example Top-Down Teaching: Object-Oriented Programming in CSI by R. Decker and S. Hirsfield (ACM SIGCSE 1993, pp. 270-73). Indeed it's one of the reasons people debate whether or not choosing an OO language as the first language is the right thing to do. Nonetheless, the class/instance problem remains the first and probably the biggest hurdle you face when teaching (and, seen from the student's point of view, when learning) your first OO language. We've seen students struggle with "can't make static reference to a nonstatic attribute" errors. We've seen Java programs with classes full of static methods and no instantiation at all. And we've seen many techniques described in the famous "How to write unmaintainable code" Web site (http://mindprod.com/unmain.html) actually being used! Alas, the class/instance problem isn't the only one. When you code in Java you're forced to divide your product into many files. This policy is good for experienced programmers they can organize code better, in separate files and directories. But while the experienced programmer can keep track of the whole product, the Java newcomer isn't always able to see a bunch of classes as a complete program. When novices are editing a piece of code they have to remember every relationship existing between the current Java file and every other one. They must learn (and, we might add, learn very fast) that the class they're creating has attributes and methods not present in the current opened file. And we must help them as best we can. To do so (and to help ourselves in teaching), we tried to address these problems by "porting" a teaching technique taken from traditional classroom lessons to a software tool. What We Needed, What We Produced
The example shown in Figure 1 is a common form of circlegram: it represents an instance of a hypothetical class C with its attributes, both native and inherited, correctly placed (every attribute has an example value). To mimic the classroom situation we created a package, called HTMLStream, to generate an HTML representation of a circlegram. Remember that we want to map inheritance with a circlegram, but we also want to show the linking between objects (reference attributes). In HTML there are hyperlinks a perfect solution for mapping object links. So we had to create only the inheritance representation. We chose the HTML TABLE to represent an object instance, with a recursive table-in-table to show inherited attributes. The current format is shown in Table 1. This representation should also be familiar to someone accustomed to UML class symbols, except that in our case inherited attributes are embedded inside the object instead of having an arrow pointing to the extended class. The table-in-table format converts an object like the one shown in Figure 2 into the HTML visualization shown in Figure 3. When converting a complex object graph, our tool will generate an HTML page (it may be a long page) with links and internal anchors. An object graph (a bunch of interlinked instances in memory) is "linearized" in one single HTML page. Every reference between instances is mapped with an internal link to an anchor on the same page; in fact, every single object is converted into a piece of HTML text that contains links to other "objects," and is also a target (HTML anchor) to be pointed to. Graphically, every object having reference attributes will look like the one shown in Figure 4. At runtime, in memory, the same object will be allocated as a graph (see Figure 5). Our generator will create an HTML page similar to the one rendered in Figure 6. The HTMLStream tool can be used during a lesson to explain inheritance and reference attributes, but of course it can also (and should also) be used by all students to experiment with their own source code. Students feel at home browsing an HTML page; they'll explore objects the same way they explore the Internet. The whole process of generating an HTML page from instances in memory can be described with this checklist:
Other Products Already Available
Source visualization is used by developers to keep track of what they (or other developers) have done. With source visualizers, you can produce, for example, call graphs, statistical reports and tree representations. Runtime visualization is subcategorized into pre- and postmortem visualization, "pre" meaning "running" and "post" meaning "after completion." Information about software visualization is readily available starting atwww.cc.gtech.edu (the research area of professor Stasko and colleagues). There's also a journal article to be aware of, "An Effective Web-based Software Visualization Learning Environment" by J. Domingue and P. Mulholland, in the Journal of Visual Languages and Computing, 9 (5), 1998, pp. 485-508. And there's a book chapter too, "Using Software to Teach Computer Programming: Past, Present and Future" by P. Mulholland and M. Eisenstadt, in Software Visualization: Programming as a Multimedia Experience (Cambridge, MA: MIT Press, 1998). In its present version, HTMLStream is mainly a postmortem visualizer, even though it produces output throughout the execution of the analyzed program. At the end of execution, the user can browse through the HTML page and view what's just happened. After our resource search we decided not to use any of the preexisting tools for the following reasons:
How to Use HTMLStream
// MUST be HtmlizableI The HTMLStream is also useful if you want to add (or "queue") other objects at a later time - assuming that we still have hStream in scope:
// adding new objects This last example is one reason for the existence of the HTMLStream class: it's useful when you need to queue more than one reference for printing. It's very similar in usage to the ObjectOutputStream; in fact, the HTMLStream works more or less the same way (i.e., conceptually). A mental aid when using HTMLStream is the "printing queue" metaphor: you submit objects to the stream when you're satisfied you can start the actual printing of the stream itself.
How Can I Make My Objects Printable?
How We Did It Implementation Notes
To implement this htmlization mechanism, we took inspiration from the serialization feature already present in Java. This "pattern" is very similar to the Externalizable mechanism: the serializable object must "do something" to be serialized; it's not completely passive. Remember that we had to render the three aspects mentioned above: inheritance, attributes and links (references to other objects). Inheritance is represented by the recursive table-in-table (every attribute is printed coupled with its value). Any link (attribute that is a reference) will be a hyperlink to an anchor somewhere on the same page. The main problem in performing this conversion is, for example, when an object points to another object, which in turn points to another one, which in turn points to the first one that is to say, a circular list. This is a common situation in a running program. In fact, we call them object "graphs," not trees. If we want to convert everything automatically, meaning that every reference inside the root object (initial object) is automatically followed, we need a way to avoid infinite loops when converting circular graphs or even the simple multiple referentiation. Loop avoidance is the primary rationale for the existence of the HTMLStream class. An HTMLStream is a "container" that can be "grown" by adding HtmlizableI objects. An "add" operation wonÕt succeed if an object is already present in the stream. We must solve another problem: automatic reference following. If we want to "print" an object through this stream, we want to avoid passing every single reference to be printed; we just want to pass the graph "root" (quoted since it is a graph and there is no formal root) and let the stream do the rest. This is the role of any HtmlizableI object (implemented in the DefaultHtmlizable class): instances from this class can scan their attributes and treat them "correctly," automatically adding to the stream every referenced object (have a look at the method grow() in DefaultHtmlizable.java). The pseudocode for the scan&grow algorithm is:
for every attribute of this instance Actually, this procedure is executed at each class "level." We use reflection to extract attribute details, and we do this for each class level up to but excluding Object, the top class in Java. For every class level scanned we open a new TABLE tag. At the end we close everything, thus drawing our desired "html circlegram." Ideas for Past, Present and Future Development
There are two categories of ideas for development: minor (e.g., aesthetic or "easy" to implement) and major (e.g., semantic) improvements.
Conclusion
A tool like HTMLStream and not only HTMLStream, but any tool must be introduced with care. Novice users need to be introduced first to general programming principles, then to the joys of editing we had students editing source files who didn't know where they'd saved them! and to the problems associated with using a PC (for example, if they're very young they often ignore basic concepts such as command prompt and redirection). We'd be very happy to receive some feedback from JDJ readers. The complete source code for this article can be downloaded from the JDJ Web site, JavaDevelopersJournal.com. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||