View this PageEdit this PageUploads to this PageVersions of this PageHomeRecent ChangesSearchHelp Guide

MP4

Military Rank

Homework 4: Military rank

Magnitude is the abstract superclass of everything that can be compared.  Most of the standard Smalltalk classes that are magnitudes are numbers, but most of the subclasses of Magnitude that you are likely to make, such as loudness, brightness, and grades, are not numbers. How can you make a new kind of magnitude?

The purpose of this exercise is two-fold:
- learn how to make a concrete subclass of an abstract class like Magnitude.  The goal is to make a class to represent military ranks.
- continue to learn to write code in a test driven manner. We provide you with a set of test cases and when your code passes all those tests you should be in good shape (we reserve the right to add more test inputs).  Get the source code for the test cases at the end of this page.

Military ranks can be compared with each other, so they are magnitudes, but they are not numbers.  The simplest kind of magnitudes only have to define one method, and all the other comparison methods are derived automatically from it.  (What is this method? If you didn't know from the notes, how would you find out?)

Each MilitaryRank has a name.  You can use the names to decide which is the highest rank.  Suppose you had a literal array #( 'private' 'corporal' 'sergeant' 'second lieutenant' 'first lieutenant' 'captain' 'major' 'lieutenant colonel' 'colonel' 'general' ) called ranks. This is much too rough to be useful, but it gets the idea across.  Then you could find out whether one rank came before another by

(ranks indexOf: rank1 name) < (ranks indexOf: rank2 name)

Give MilitaryRank a method that returns that array.  Then you should be able to easily define the < method for ranks.

You have to be able to create a new object. Create class and instance methods so that you can say

MilitaryRank named: 'private'
and get the right kind of instance.  Write methods so that you could say
MilitaryRank private
What are the advantages of each way of creating objects?

Try 3 < MilitaryRank private and MilitaryRank private < 3.  What error messages should they return?  Figure out how to make good error messages.

self error: 'Big Mistake!'
will generate an error message.

The army and the navy have different systems of rank. You can't compare the rank of someone from the army and the navy, though a Seaman knows better than to get in the way of a General.  One solution is to make two classes, ArmyRank and NavyRank.  If you do it right, you can make them almost identical, with most code in their common superclass.  The only difference between them should be the method that returns the literal array and the instance creation methods. Implement this approach. Pay attention to what happens when you try to compare ranks that are not comparable.  Note that we don't really care what the navy ranks are, though the test cases assume you have 'seaman' and 'captain' at least.  Your solution should work for any new ranking system.

Run our test suite for ArmyRank and NavyRank. It uses the SUnit testing framework. You can run the test suite by executing "TestRunner open", then selecting CS497rej.RankTestCase from the dropdown menu. You can run the tests by clicking the "Run" button (careful, do not use the "RunAll" button!).  If any tests fail or produce errors, you can go directly to a debugger by selecting the testing method from the dropdown list of failed tests at the bottom of the window.

Eliminate duplication as much as possible. If you aren't careful, you will duplicate a lot of code between the different ranks.  You can also duplicate code between the comparison methods.  The less duplication, the easier your programs are to understand and to change. For your convenience we provided some test cases that check to see whether you duplicated code (to be found in the RankTestCase under CodeDuplicationTesting).



Advice


While it is certainly possible to generate all the different methods by hand, you can also experiment with the reflective features of Squeak.

In particular, take a look at the "doesNotUnderstand" method and see what you can with it. You can read more about it from this book. Search for "doesNotUnderstand".

"doesNotUnderstand" is a common idiom in Smalltalk and you should understand how it works. Ruby has a similar mechanism called "method_missing" which forms the basis for the Ruby on Rails framework.

Deliverables.


For this exercise, you can just fileOut the class and send me the text contents directly as e-mail i.e. do not send as an attachment but just paste the text in.

To do a fileOut, open a browser, find the class category, select it with the middle mouse button and select fileOut.

I would appreciate it if you created proper categories to store all the methods that you implemented. That way I can quickly search through the list of categories.

CS598rej-Testing HW4.cs


Link to this Page

  • Machine Problems last edited on 14 May 2008 at 4:25:53 pm by c-98-212-224-168.hsd1.il.comcast.net