CS 4604 Homework #2 Solution Sketches

  1. (20 points) Here's "a" solution: If your answer does not match this, don't panic! This is just one of several possible solutions! For example, notice that we show a player as being able to play in only one team. If your players can play in more than one team, that is fine!

    The "color" is also shown as a "set of strings" attribute; this is possible in ODL because you have constructors such as Set, List etc. Notice that this question was featured in the E/R chapter, where it was not possible to make a balloon to be a set (hence the statutory statement that "a set of colors is not a suitable attribute type for teams"). If you took this statement to heart, you might have made color to be a separate class and expressed the uniform colors as a relationship; that is fine! Or you could have a color as an enum or as a struct of three reals (with proportions for the red, blue and green percentages!). There are many posible solutions.

    Finally, we have chosen a List to denote the players of a team; the reason for this is two-fold: (i) in several sports, the players are ordered and (ii) the captain can be easily indicated by his position on the list, (by just an integer, get it? :-)). You could have instead chosen a Set, in which case, captain would have been a string or something (realize that you cannot have an attribute of type Player; so, you would have to figure out how to enforce the requirement that the captain is one of the players themselves) But that looks shabby, don't you agree? Or you could make captain be a one-one relationship from Team to Player, that too would serve the purpose, though that still doesn't prevent us from naming a non-player as a captain.

    Obviously, we cannot enumerate every possible answer! All we look for is to see if your solution satisfies the question's constraints; if so, you are fine! 15 points for every correct interface declaration with 5 more points for identifying any relevant notes.
    
    interface Team
    {
    	attribute string name;
            attribute Set<string> colors_of_uniform;
    	relationship List<Player> members
    		inverse Player::playsfor;
    	attribute int captain;
    	relationship Set<Fan> favorite_team_of
    		inverse Fan::favorite_teams;
    };
    
    interface Player
    {
    	attribute string name;
    	relationship Team playsfor
    		inverse Team::members;
    	relationship Set<Fan> favorite_player_of
    		inverse Fan::favorite_players;
    };
    
    interface Fan
    {
    	attribute string name;
    	relationship Set<Team> favorite_teams
    		inverse Team::favorite_team_of;
    	relationship Set<Player> favorite_players
    		inverse Player::favorite_player_of;
    	attribute string favorite_color;
    };
    


  2. Here's a possible solution. Notice that we have two separate relationships from Felonies to the two punishments. A many-many relationship from Felonies to Punishments is overly general (for instance, it allows for the possibility that there could be 5 fines and 7 jail sentences, whereas the question is more specific).

    Some notes that we still have to write are that a crime can be a felony or a misdemeanor but not both. Similarly, a punishment can be a fine or a jail sentence but not both (this is different from the issue of a fine and a jail sentence being applied to a given felony; a given punishment can be only one of the two). Since the punishment for a misdemeanor is always a fine, you might want to make the arrow entering Fines (from Misdemeanors) a curved one. Similarly, there has to be some punishment for a felony (at least a fine or a jail sentence). There is no way to say "at least" in E/R, so this has to be a note. Notice that two curved arrows entering Fines and Jail Sentences (from Felonies) will not work, since then you are insisting that both of them have to be applied toward the felony.



    I added a key for Punishments since it seems useful to have one (even though the question doesn't declare what the key should be). It is wrong to make the dollar amount to be a key for Fines, since the key for a subclass cannot be different from the key for the main class. This means that the dollar amount is also the key for Punishments in general, but this is obviously wrong. Some Punishments do not have dollar amounts, only Fines do.

  3. Here's a solution. It is good to make all the arrows curved ones. There is at least one note, namely that a city has to have at least one airport.



    Notice that you can feel free to move two of the "time" balloons down to Flights since they are part of many-one relationships. We cannot move the "time" ballloon of stopover at to either Flights or Airports because it is an attribute of a many-many relationship.

  4. Here's a solution. Notice that it is more elegant to have one direct relationship from Composite Parts to Parts than two separate ones (to Monolithic Parts and Composite Parts). Why? This way, a composite part is made of any number of parts. Each of these parts can in turn be composite or monolithic. It also works the other way. If you read the relationship in the other direction, you get "a part is used for making composite parts". Since this relationship is inherited by both Monolithic Parts and Composite Parts, they get to be involved in the relationship too. Thus, "a monolithic part is used for making composite parts" and "a composite part is used for making composite parts", both of which are correct. Be careful not to read the inherited relationship as "A monolithic part is made up of composite parts" because what is inherited is the relationship in the other direction!



    One of the notes you have to write is that a part can be either composite or monolithic but not both.

Return Home