Encapsulation - Design Activity
Overview
Meet with your team and prepare a design for this week's programming assignment.
Regarding "Guidance from a Mentor"
This activity is designed to help you walk through the design process as a team. Because decisions in the first steps determine many steps that follow, we want to make sure you don't start down a path that will lead to problems later.
If we were in a conference room together at a company, the mentor would quietly observe the discussion and only interject if the conversation started to lead in a bad direction.
In the case of this activity, because the instructor cannot be in each team meeting, we have placed "Guidance from a mentor" sections throughout the design process. You should not expand and look at these sections until you have done your best as a team to answer the questions. Then, after you have answered the questions, you should check these Guidance sections to make sure you are headed in a good direction.
Important: There is more than one good way to design a project. We have been hesitant to include any guidance like this, because you shouldn't think this is the only way to solve the problem or the way you must do the project. If your design is following a similar approach, but uses slightly different words for the classes/variables/methods, that is great! You don't have to conform exactly to the suggestions here.
Agenda
Use the following as an agenda for your team meeting. Whoever is assigned to be the lead student for this gathering should help guide the group through these steps and ask the questions listed here.
Before the meeting: Verify the time, location, and lead student
This could be as simple as posting a message to your MS Teams channel that says something like, "Hi guys, are we still planning to meet tomorrow at 7pm Mountain Time? Let's use the MS Teams video feature again." Or, if someone else has already posted a message like this, it could be as simple as "liking" their message.
Make sure to identify who will be the lead student for this week. For example, "Emily, are you still good to be the lead student for this week?"
Begin with Prayer
Discuss the Preparation Learning Activity
Take a minute to talk about the learning activity from this week. Talk through any difficulties that people had understanding the material or completing the activity.
- What part of the learning activity was the hardest for you?
Guidance from a Mentor
Remember:
- Every member variable should be private. Getters and Setters should be used to access the data.
- Getters should not have any parameters and should not prompt the user for anything, but instead, simply return a value.
- Setters should accept a single parameter and have a
voidreturn type.
Review the Program Specification
Refer to the Scripture Memorizer program specification. As a team, review the program requirements and how it is supposed to work.
- What does the program do?
- What user inputs does it have?
- What output does it produce?
- How does the program end?
Guidance from a Mentor
- The program can end in one of two ways: Either the user types quit, or all of the words in the scripture have been hidden.
Determine the classes
The first step in designing a program like this is to think about the classes you will need. When thinking about classes, it is often helpful to consider the strong nouns in the program description.
- What are good candidates for classes in this program?
- What are the primary responsibilities of each class?
Guidance from a Mentor
Good choices for classes could include something like:
- Scripture - Keeps track of the reference and the text of the scripture. Can hide words and get the rendered display of the text.
- Reference - Keeps track of the book, chapter, and verse information.
- Word - Keeps track of a single word and whether it is shown or hidden.
Define class behaviors
Now that you have decided on the classes, you will need and their responsibilities, the next step is to define the behaviors of these classes. These will become methods for the class.
Go through each of your classes and ask:
- What are the behaviors this class will have in order to fulfill its responsibilities? (In other words, what things should this class do?)
Guidance from a Mentor
A class for a Scripture, which has the responsibility to keep track of the reference, the text, and to hide the words, might include some behaviors similar to these:
- Hide Words
- Get Rendered Text
- Is Completely Hidden
A class for a Word, which has the responsibility for storing the text of a word and whether it is hidden or not, might include some behaviors similar to these:
- Hide
- Show
- Is Hidden
- Get Rendered Text
A class for a Reference will have some behaviors as well. What might they be?
Define class attributes
Now that you have defined the classes, their responsibilities, and their behaviors, the next step is to determine what attributes the class should have, or what variables it needs to store.
Go through each of your classes and ask:
- What attributes does this class need to fulfill its behaviors? (In other words, what variables should this class store?)
- What are the data types of these member variables?
- What constructors should each class have?
Guidance from a Mentor
A class for a Scripture would likely need member variables for a reference and all of the words in the scripture. What data types would be used for these?
A class for a Reference should likely have attributes for the book, chapter, and verse, as well as a second "end verse" to handle the case of Proverbs 3:5-6. What data types would be appropriate for these?
A class for a Word will need attributes as well. What should they look like?
Define Constructors
Now that you have defined the classes, including their methods and attributes, the next step is to think about the constructors you will need to create new instances of these classes. Remember that you can create multiple constructors with different parameters to make it easy to work with your classes.
Remember, that constructors help set up the initial state of the object, so you should consider what data is necessary for that initial state.
What constructors should each class have?
- In other words, what parameters should you pass in when creating an object of that type.
What other work needs to be done to set up these objects?
- For example, does the constructor need to run code to perform set up tasks, like creating lists, iterating through variables, etc.
Guidance from a Mentor
A class for a Reference should likely have at least two different constructors to account for cases where there is a single verse or multiple verses. What should they look like?
A class for a Word will a constructor as well. This constructor should likely accept the text of the word to save it as an attribute. In addition, the constructor will need to set the initial visibility of the word (whether it is shown or hidden). Is this something that the user should pass in directly, or can the constructor just assign a value?
A class for a Scripture will need a constructor that accepts both a reference and the text of the scripture. If the Scripture class internally stores a List of Word objects, the first thought would be to pass a List<Word> variable to the constructor. While this could work, it may be easier to use the class by passing the text of the scripture as a single string with all of the words in it. Then, the constructor would have the responsibility of creating the list, and splitting up the words in the string to create Word objects for each one and put them in the list.
While putting the logic of creating the word list may seem like a lot of work for the constructor, it is helpful to encapsulate this logic in the Scripture class so that other code, does not have to worry about the internal storage of the Scripture. This would enable the program to be easily changed in the future, if a different implementation choice were made.
Conclude
At this point, you have the design of the classes you will need for this project. If your design is not "perfect," or it needs to change as you begin working on the project, that is just fine! As you learn more details, you will naturally need to adjust your planning. This is why the principles of programming with class are so valuable, because they allow your program to easily change.
At the end of your meeting:
- Make sure that each person in the meeting gets a copy of any resources you have developed as a team.
- Determine who will be the lead student for the next meeting.
Submission
After this team activity, each person needs to individually do the the following:
-
Complete a design based on the team discussion.
- Include a class diagram for each class you will need.
- Include some kind of description or chart showing how the program will run or how these methods relate to one another.
- You are welcome to use any drawings, diagrams, or other artifacts that you created as a team.
- You might also need to create a few more on your own, based on the discussion you had with your team.
- Each person should individually submit their design to I-Learn.