Homework3
● Conciseness, Formatting & Documentation: 3 points – Reserved for concise solution, good Javadoc, and formatting. ● Points: Phase #1: 2 points per class – 2×9=18 points total. Phase #2: 10 points.
Project Overview:
An object-oriented solution is being developed for a pet shop that has different types of pets and food for them.
Phase#1: Develop hierarchy of classes from UML
Analyzing the user’s requirements, a hierarchy of classes has been designed. In this part of the project, you are required to develop the Java source code (9 different classes) corresponding to the class hierarchy shown below:
Inside the Thing class: the getKind(), isAquatic(), and getPrice() methods are in italic which means they are abstract methods
Notes on specific of the methods
- The return value of getKind method should be as follows: ▪ For Pet objects (Cat, Fish, Octopus. Dog), the return value of the getKind must be the kind value but with class name as prefix. For e.g., “Cat: Maincoon”, “Dog: Poodle” etc. ▪ For Food objects (WormCan, ChowBag) , the return of the getKind method must be just the name of the class as in “WormCan” or “ChowBag”.
- Note the following toString output format: ▪ For Pets the toString representation is generated via (example for Cat class): String.format("%s\t%s\t%.2f\t%.2f", "Cat", kind, price, foodPerDay); ▪ For Foods the toString representation is generated via (example for WormCan class): String.format("%s\t%.2f\t%.2f", "WormCan", price, weight);
- Notes for the equals methods in Pet and Food classes: ▪ Two Pet objects are equal if they are of the same Java-class (use instanceof keyword for comparing classes), and have the same kind values. And, ignore price and foodPerDay in comparisons. ▪ Two Food objects are equal only if they are of the same Java-class (use instanceof keyword for comparing classes), and have the same price and weight.
- For the isAquatic methods, simply return false inside Cat and Dog classes, and return true for Octopus and Fish classes.
Testing for Phase #1:
You are expected to develop your own approach to test your classes • Test each class as you develop them • For each class test, ensure you are able to create an object from that class • Print the values returned by different methods to ensure they are consistent .
Phase #2: Complete methods in the PetShop source
In this phase of the project, you will be using the classes you developed in the previous phase to develop a simple pet store application. You are already given starter code with method stubs in PetShop.java for you to complete. Use the Javadoc for the methods and sample outputs shown below to implement the various methods in PetShop.java.
Testing for Phase #2:
For this phase of testing a simple PetShopUI.java program is already supplied to you. Review this program and use it for testing methods in PetShop. Sample outputs are shown below (user inputs are in green color)
Test #1: Loading things from a text file Welcome to the Pet Shop (0 pets & 0 food things). What would you like to do [0 for menu]: 0
- To add things from a text file
- Print summary of things.
- Print all things
- Print food status
- Show this menu -1. Quit Welcome to the Pet Shop (0 pets & What would you like to do [0 for Enter file name to add inventory: Welcome to the Pet Shop (7 pets & What would you like to do [0 for
Test #2: Load and print things Welcome to the Pet Shop (0 pets & What would you like to do
Welcome to the Pet Shop (7 pets &
What would you like to do [0 for
List of all items:
Fish Goldfish 4.75 0.02
Fish Guppy 1.25 0.01
Octopus Argonaut 99.99 0.03
ChowBag 17.50 2.50
WormCan 5.75 1.00
WormCan 5.75 1.00
Dog Poodle 725.00 0.30
Dog Bulldog 450.50 0.20
Cat Persian 1400.00 0.10
Cat Meincoon 325.00 0.15
Welcome to the Pet Shop (7 pets & What would you like to do
Test #3: Load and print summary of things Welcome to the Pet Shop (0 pets & What would you like to do [0 for Enter file name to add inventory: Welcome to the Pet Shop (7 pets & What would you like to do [0 for Summary of items in Pet Shop Aquatic pets & food summary Number of pets : 3 Total price pets : $105.99 Number of food items: 2 Total price of food : $11.50 Non-aquatic pets & food summary Number of pets : 4 Total price pets : $2900.50 Number of food items: 1 Total price of food : $17.50 Welcome to the Pet Shop (7 pets & What would you like to do [0 for
0 food things). menu]: 1 few_things.txt 3 food things). menu]: -1 0 food things). menu]: 1 few_things.txt 3 food things). menu]: 3
3 food things). menu]: -1
0 food things). menu]: 1 few_things.txt 3 food things). menu]: 2
3 food things). menu]: -1
Test #4: Load and print food status
Welcome to the Pet Shop (0 pets & 0 food things). What would you like to do [0 for menu]: 1 Enter file name to add inventory: few_things.txt Welcome to the Pet Shop (7 pets & 3 food things). What would you like to do [0 for menu]: 4 Pet Shop food status: Daily aquatic food needed : 0.06 lb Daily non-aquatic food needed : 0.75 lb Aquatic food stock in store : 2.00 lb Non-aquatic food stock in store: 2.50 lb Welcome to the Pet Shop (7 pets & 3 food things). What would you like to do [0 for menu]: -1
USE DEBUGGER:
Submission:
This homework assignment must be turned-in electronically via Canvas CODE plug-in. Ensure your program compiles successfully, without any warnings or style errors. Ensure you have documented the methods. Ensure you have tested operations of your program as indicated. Upload the following to Canvas via the CODE plug-in:
- Phase #1: The 9 Java source files developed for this phase of the project.
- Phase #2: The 9 classes and PetShop.java
Frequently asked questions:
Are any of these 9 classes supposed to be abstract classes? Yes, the Thing class.
Why the Pet and Food classes are also in italic. Are they also supposed to be abstract?? If you are not implementing some of the abstract methods from the Thing class inside the Pet or Food class (which is exactly what you should do based on the instructions), then you also need to define the Pet and Food classes as abstract. That way you are telling java that those abstract methods will be implemented later by children of Food or Pet.
*** The plugin is saying my .equals() methods aren't working correctly but I made a test class and both methods seem to be working fine (as seen in picture). I am really not sure where to go from here and how to approach fixing my methods. The Code checks the classes to make sure objects are also from the same family. Make sure to check objects using instanceof instead of getClass() method. Also, ensure that you are overriding the .equals method inside the Pet and Food classes.
Also, should we use standard javadoc for abstract methods? Yes. Same rules.
I was wondering how we are supposed to use the Thing class/ constructor. Are we supposed to be using it as an abstract constructor and use the pet and food classes to actually create the Thing? You can create the Thing class as an abstract class. Thing() is a default constructor, you don't really have to do anything inside the default constructor, you can just leave the body of the Thing() constructor empty and you'll be fine. Constructors can't be abstract.
Should I choose "abstract" at the modifier part when creating a class? Because when I chose that option, Eclipse created an abstract class in a package named "homework3" Yes, if you want to create an abstract class then you have to add the keyword abstract to the class header.
What do you do for the isAquatic method? Don’t overthink it. The isAquatic is just a method that returns a boolean value either true or false. If you are writing this method inside a dog class, then it’s not aquatic so this method inside this class should return false, that’s it.
Can I call getKind() methods from either Food or Pet? You can always use methods/variables available inside the class or from higher classes like a parent or grandparent class. But, you can't use things from other classes that haven’t been implemented yet either in the current class or in higher classes. For example: the getKind method hasn't been implemented yet in Pet class so you should not use it. Also, when you are using instance variables that are inherited from another class, don't forget using the keyword super, not this.