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.

Note

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:

Process Objectives:

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.

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

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

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

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.

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 for more information.

The figure below shows a possible class diagram for a Card class.

Class diagram for a Card class

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.

Class diagram for Card and Deck

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.

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

Use the following UML class diagram to answer the next few questions.

class diagram of a Flight and Airport class

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).

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.

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.

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.

You have attempted of activities on this page