# Polymorphism Section Mini-Assignment :::danger Due Date: Before your section (Tuesday-Thursday October 1-3) ::: ## Introduction: This mini assignment focuses on polymorphism and making class diagrams. We highly recommend fully answering these questions, as they will greatly help you prepare for Fruit Ninja. If you need help we recommend the resources listed below, asking on Ed, or going to conceptual hours. ### Helpful Resources: - Lecture slides: [Inheritance](https://cs.brown.edu/courses/cs015/lecture/pdf/CS15.Lecture_7_Inheritance_and_Polymorphism.9.26.24.pdf) and [Interfaces](https://cs.brown.edu/courses/cs015/lecture/pdf/CS15.Lecture_6_Interfaces_and_Polymorphism.9.24.24.pdf) - [How To: Class & Interfaces/Inheritance Diagrams](https://docs.google.com/document/d/1LZV1M75gozJuU7jouTmusjw2B4fjLPOCSh9feQ3sDXM/edit#heading=h.n64vzvb7650b) ### Problem 1: Tic Tac Toe Interface/Inheritance Diagram You have already created a class diagram to turn in along with your Tic Tac Toe. Now, we’d like you to create your first interface/inheritance diagram! Take a look at the support code interfaces that you implement, and make the diagram using [this document](https://docs.google.com/document/d/1LZV1M75gozJuU7jouTmusjw2B4fjLPOCSh9feQ3sDXM/edit#heading=h.n64vzvb7650b) as a guide! Your diagram should not contain all your classes. Just the ones that model a polymorphic relationship. **We will be providing feedback on your diagram so please put some thought into it.** ### Problem 2: Andy’s New Job Andy is tired of teaching students how to code and now wants to open a restaurant, but he needs your help in designing his program! Andy needs waiters, cooks, and a bartender for his trendy spot, but all of these people are his employees, so they must get paid and know how to work. Make sure that Andy is also at the restaurant, since he is the boss. Think of all the classes you need to model a Restaurant and what your top level class will be. Also remember that you will need to create an <span style="font-family: 'Consolas', monospace;">App</span> class. Refer to the [How To: Draw a Class Diagram guide](https://docs.google.com/document/d/1LZV1M75gozJuU7jouTmusjw2B4fjLPOCSh9feQ3sDXM/edit#heading=h.n64vzvb7650b). Please create 2 diagrams for Andy’s restaurant: 1. Illustrating the class relationships among your classes. 2. Illustrating the inheritance and interface relationships among your classes. a) Make sure to include which methods are shared between classes. For example, if you decide an interface is necessary, add which methods will be defined in the interface in your diagram. ### Problem 3: Polymorphism In a few sentences, describe the pros and cons of polymorphism. ### Problem 4: Actual vs. Declared Type Take a look at the short program below: <span style="font-family: 'Consolas', monospace;"> public class Store { &nbsp;&nbsp;&nbsp;public Store() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//code elided &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;public void restock() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.getFoodFromBack(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.putOnShelf(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.organize(); &nbsp;&nbsp;&nbsp;} } </span> <span style="font-family: 'Consolas', monospace;"> public class GroceryStore extends Store { &nbsp;&nbsp;&nbsp;public GroceryStore() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//other code elided &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;public void checkExpirationDates() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.takeOffShelf(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.throwOutOld(); &nbsp;&nbsp;&nbsp;} } </span> For each of the four code segments below, answer the following questions: - What is the actual and declared type of the variable myStore? - Will the code run without error? Why? A. <span style="font-family: 'Consolas', monospace;">**`Store myStore = new GroceryStore();`**</span> B. <span style="font-family: 'Consolas', monospace;">**`GroceryStore myStore = new Store();`**</span> C. <span style="font-family: 'Consolas', monospace;">**`GroceryStore myStore = new GroceryStore();`\ &nbsp;&nbsp;`myStore.restock();`**</span> D. <span style="font-family: 'Consolas', monospace;">**`myStore = new Store();` &nbsp;&nbsp;`myStore.checkExpirationDates();`**</span> ## To Handin: Submit your answers on [this form](https://docs.google.com/forms/d/e/1FAIpQLSd8zTLb81Kju_uNUGdn_e7VY6O7VuZxRZfGoSNd5i4qROs9rg/viewform) before the due date. Any handins after the due date will not be accepted.