Skip to main content

Section 19.10 Group Work with Multiple Classes

It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home 1 .

Note 19.10.1.

If you work in a group, have only one member of the group fill in the answers on this page. That person will be able to share answers with the group at the bottom of the page.
Learning Objectives
Students will know and be able to do the following.
Content Objectives:
  • Explain how objects of one class can be used in another
  • Identify object attributes and methods
  • Identify class attributes
  • Define the parts of a UML class diagram
  • Explain how to use a description of a system to find classes, attributes, and methods.
Process Objectives:
  • Modify code that involves multiple classes
  • Predict output from code with multiple classes

Subsection 19.10.1 Multiple Classes

Object-oriented programs typically have many classes. Each class describes the data (attributes) that objects of that class have and their behaviors (methods). One class can use objects from another class.
Let's use the Point class that we created earlier to define a Rectangle class. A rectangle can be defined by two points if we assume that one point is the top left and the other is the bottom right. That means the width of a rectangle is the difference between the x values and the height is the difference between the y values. The area of a rectangle is the width times the height.

Checkpoint 19.10.2.

Run the code below to see what it prints. You can also use the “Show in CodeLens” button to step through the code.

Checkpoint 19.10.3.

    Q-2: Look at the code above. What are the object methods in the Rectangle class? Pick all that apply.
  • self.p1
  • This is an object attribute in the Rectangle class.
  • self.__init__
  • This is an object method in the Rectangle class.
  • self.p2
  • This is an object attribute in the Rectangle class.
  • self.area
  • This is an object method in the Rectangle class.

Checkpoint 19.10.4.

    Q-3: Look at the code above. What type of thing (class) is p1 in the __init__ method in the Rectangle class?
  • Point
  • It was created by the Point class and passed to the __init__ method in the Rectangle class
  • Rectangle
  • It is set in the __init__ method in the Rectangle class, but it is not a rectangle
  • Tuple
  • It is not a tuple. Point(0,0) creates an object of the Point class and calls the __init__ method in that class.

Checkpoint 19.10.5.

    Q-4: Look at the code above. What are the object attributes in the Point class? Pick all that apply.
  • self.x
  • This stores the x position for the point. It is an object attribute.
  • self.distanceFromOrigin
  • This is an object method in the Point class.
  • self.y
  • This stores the y position for the point. It is an object attribute.
  • self.halfway
  • This is an object method in the Point class.

Subsection 19.10.2 Object-Oriented Analysis and Design

How do you determine the classes that you need and the attributes and methods that objects of the class should have? In object-oriented analysis and design you often walk through a scenario of how the system will work. Write down all the nouns and verbs that are mentioned. Nouns are potential classes or attributes. Verbs are potential methods. If a noun represents simple data like a number or string it is usually just an attribute of a class. If a noun has data associated with it or behavior then it is likely a class.

Note 19.10.6.

One approach to object-oriented analysis and design is to use index cards to represent classes. Write the class name at the top of the card. Put the data or attributes that each object needs to keep track of below that and the methods or behaviors that objects need to be able to do below that.
How would you create software for a simple card game? Play the Aces and Twos card game at https://www.mathsisfun.com/games/card-match-game.html 2 
When the game starts all of the cards are all face down in rows and columns. You click on a card to select it and it turns over to show the suit and rank (value). Then you click another card to select it and it also turns face up. If the two cards match (have the same rank and value) then the cards stay facing up. Otherwise they both turn down and you take another turn. You win when you have found all of the matches. The game keeps track of the total time it took you to find all of the matches.

Checkpoint 19.10.7.

    Q-5: Look at explanation of the game above. Which of the following nouns could be classes?
  • Game
  • Game is a noun and you can play a game so it has behavior so it is a good candidate class.
  • Card
  • Card is a noun and you can turn a card so it has behavior. A card has a suit and rank so it has attributes.
  • Row
  • Row is a noun, but here it just describes how to layout the cards when you start the game. It doesn't need to be a class.
  • Suit
  • Suit is a noun, but it can be an integer representing one of the four suits. It will be an attribute of a Card object.

Checkpoint 19.10.8.

    Q-6: Look at explanation of the game above. Which of the following verbs could be methods (behaviors)?
  • select
  • You select a card so it could be a method on the Card class.
  • turn
  • You turn a card over so it could be a method on the Card class.
  • show
  • While this is a verb, it happens when you turn over a card. It probably doesn't need to be another method.
  • match
  • In the game you need to determine if two cards match, so you do need a method to support this.

Subsection 19.10.3 UML Diagrams

UML means Unified Modeling Language. It is a standard way to visualize the design of an object-oriented program. See https://en.wikipedia.org/wiki/Unified_Modeling_Language 3  for more information.
The figure below shows a possible class diagram for a Card class.
Figure 19.10.9.
In UML each class is shown in a box with the class name on top optionally followed by a line then the attributes and then optionally followed by a line and then the behaviors (methods).
To play the card game you need two of each type of card. So you will need to two decks. A deck has 52 cards in the four suits from Ace to King.
Figure 19.10.10.

Checkpoint 19.10.11.

Run the code below to see what it prints. You can also use the “Show in CodeLens” button to step through the code.
Notice that we have defined a list of suit_names and rank_names in the Card class. These will be created in the class Card and not in each object of the class Card. They are called class attributes. Each object of the Card class doesn't need to have its own copy of these lists. That would be a waste of space. Instead they are created in the class and all objects of the class have access to it.

Checkpoint 19.10.12.

Q-8: Use .rank_names to access the class attribute rank_names in the Card class.

Checkpoint 19.10.13.

    Q-9: Look at class Card above. Which of the following are object attributes? Select all that apply.
  • suit_names
  • This is defined in the class not on an object. It is a class attribute.
  • rank_names
  • This is defined in the class not on an object. It is a class attribute.
  • self.suit
  • This is an object attribute. It is set in every card object.
  • self.rank
  • This is an object attribute. It is set in every card object.

Checkpoint 19.10.14.

Q-10: How can you tell if an attribute is a class or object attribute?

Checkpoint 19.10.15.

Q-11: The function in class creates all of the card objects in the deck object.
Use the following UML class diagram to answer the next few questions.
Figure 19.10.16.

Checkpoint 19.10.17.

    Q-12: Look at class diagram for Flight and Airport above. How many object attributes does a Flight object have?
  • 3
  • How many items are listed after the first line in the class diagram for Flight?
  • 5
  • A Flight object has 5 object attributes: (number, departure_date, departure_time, departure_airport, and arrival_airport)
  • 7
  • This would be true if Flight inherited from Airport, but it does not.

Checkpoint 19.10.18.

Given the Airport class shown below and the class diagram shown above, write the Flight class with an __init__ method that takes the object attributes in order from top to bottom in the class diagram (number, departure_date, departure_time, departure_airport, and arrival_airport).

Checkpoint 19.10.19.

Given the Item and Order classes shown below, write the get_total method in the Order class that returns a total of all of the prices of the items in the order.

Checkpoint 19.10.20.

Q-15: What is the relationship between the example classes on this page, such as Point and Rectangle or Item and Order? How do they relate to each other?
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=mult_classes_groupsub data-size_limit=4> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.</p> </div> </div>
https://cspogil.org/Home
https://www.mathsisfun.com/games/card-match-game.html
https://en.wikipedia.org/wiki/Unified_Modeling_Language