File is attached
Assignment 1 COMP 250 Winter 2021 posted: Wednesday, Jan. 27, 2021 due: Friday, Feb. 12, 2021 at 23:59 Learning Objectives This assignment is meant for you to practice what we have learned in class in the past few weeks. A lot of the design decision have been taken for you, but it is important for you to ask yourself why each choice has been made and whether there could be a better way of doing it. You’ll soon realize that the classes you have to write are all closely related to one another. We hope that the assignment will help you appreciate the importance of class design. This is of course just a taste, you will learn much more about it in COMP 303. As mentioned in class, we suggest you take the time to draw out a class diagram. This should help you develop a clear picture of the relationship between all these classes. General Instructions • Submission instructions – Late assignments will be accepted up to 2 days late and will be penalized by 10 points per day. Note that submitting one minute late is the same as submitting 23 hours late. We will deduct 10 points for any student who has to resubmit after the due date (i.e. late) irrespective of the reason, be it wrong file submitted, wrong file format was submitted or any other reason. This policy will hold regardless of whether or not the student can provide proof that the assignment was indeed “done” on time. – Don’t worry if you realize that you made a mistake after you submitted : you can submit multiple times but only the latest submission will be kept. We encourage you to submit a first version a few days before the deadline (computer crashes do happen and codePost may be overloaded during rush hours). – These are the files you should be submitting on codePost: ∗ MarketProduct.java ∗ Egg.java ∗ Jam.java ∗ Fruit.java ∗ SeasonalFruit.java ∗ Basket.java 1 https://i.imgur.com/9ksTICA.gif https://en.wikipedia.org/wiki/Class_diagram ∗ Customer.java Do not submit any other files, especially .class files. Any deviation from these requirements may lead to lost marks • Please note that the classes you submit should be part of a package named assignment1. • You will have to create all the above classes from scratch. The assignment shall be graded automatically. Requests to evaluate the assignment manually shall not be entertained, so please make sure that you follow the instruction closely or your code may fail to pass the automatic tests. Note that for this assignment, you are NOT allowed to import any other class (including for example ArrayList or LinkedList). Any failure to comply with these rules will give you an automatic 0. • Whenever you submit your files to codepost, you will see the results of some exposed tests. These tests are a mini version of the tests we will be using to grade your work. If your code fails those tests, it means that there is a mistake somewhere. Even if your code passes those tests, it may still contain some errors. We will test your code on a much more challenging set of examples. We highly encourage you to test your code thoroughly before submitting your final version. • You will automatically get 0 if your code does not compile. • Failure to comply with any of these rules will be penalized. If anything is unclear, it is up to you to clarify it by asking either directly a TA during office hours, or on the discussion board on Piazza. 2 Market Place For this assignment you will write several classes to simulate an online Market place. Make sure to follow the instructions below very closely. Note that in addition to the required methods, you are free to add as many other private methods as you want (no other additional method is allowed). [10 points] Write an abstract class MarketProduct which has the following private field: • A String name The class must also have the following public methods: • A constructor that takes a String as input indicating the name of product and uses it to initialize the corresponding attribute. • A final getName() method to retrieve the name of this MarketProduct. • An abstract method getCost() which takes no input and returns an int. This method should be abstract (thus, not implemented) because how to determine the cost depends on the type of product. • An abstract method equals() which takes an Object as an input and returns a boolean. This method should be abstract as well, since depending on the type of product different conditions should be met in order for two products to be considered equal. [25 points] All of the followings must be subclasses of the MarketProduct: • Write a class Egg derived from the MarketProduct class. The Egg class has the following private fields: – An int indicating the number of eggs. – An int indicating the price per dozen of these eggs. Note that the all the prices (throughout the assignment) are indicated in cents. For instance, 450 represents the amount $4.50. The Egg class has also the following public methods: – A constructor that takes as input a String with the name of the product, an int indicating the number required, and an int indicating the price of such product by the dozen. The constructor uses the inputs to create a MarketProduct and initialize the corresponding fields. – A getCost() method that takes no input and returns the cost of the product in cents. The cost is computed base on the number required and the cost per dozen. For instance, 4 large brown eggs at 380 cents/dozen cost 126 cents (the cost should be rounded down to the nearest cent). You may assume that cost of all MarketProducts fits within an int and therefore doesn’t cause overflow. – An equals() method which takes as input an Object and return true if input matches this in type, name, cost and number of eggs. Otherwise the method returns false. 3 • Write a class Fruit derived from the MarketProduct class. The Fruit class has the following private fields: – A double indicating the weight in kg. – An int indicating the price per kg in cents. The Fruit class has also the following public methods: – A constructor that takes as input a String with the name of the product, a double indicating the weight in kg, and an int indicating the price per kg of such prod- uct. The constructor uses the inputs to create a MarketProduct and initialize the corresponding fields. – A getCost() method that takes no input and returns the cost of the product in cents. The cost is computed based on the weight and the price per kilogram. For instance, 1.25 kgs of asian pears at 530 cents per kg cost 662 cents. – An equals() method just like the Egg class, which matches type, name, weight and cost. • Write a class Jam derived from the MarketProduct class. The Jam class has the following private fields: – An int indicating the number of jars. – An int indicating the price per jar in cents. The Jam class has also the following public methods: – A constructor that takes as input a String with the name of the product, an int indicating the number of jars, and an int indicating the price per jar of such prod- uct. The constructor uses the inputs to create a MarketProduct and initialize the corresponding fields. – A getCost() method that takes no input and returns the cost of the product in cents. The cost is computed based on the number of jars and their price. For instance, 2 jars of Strawberry jam at 475 cents per jar cost 950 cents. – An equals() method like in the previous classes. • Write a class SeasonalFruit derived from the Fruit class. The SeasonalFruit class has no fields, but it has the following public methods: – A constructor that takes as input a String with the name of the product, a double indicating the weight in kg, and an int indicating the price per kg of such product. The constructor uses the inputs to create a Fruit. – A getCost() method that takes no input and returns the cost of the product in cents. Since this type of Fruit is in season, its original cost should receive a 15% discount. For instance, 0.5 kgs of McIntosh apples at 480 cents per kg cost 204 cents. 4 [40 points] Write a class Basket representing a list of market products. Note that the instructions on how to implement this class are not always very specific. This is intentional, since your assignment will not be tested on the missing details of the implementation. Note though, that your choices will make a difference in terms of how efficient your code will be. We will not be deducting point for inefficient code in Assignment 1. Note once again that, you are NOT allowed to import any other class (including ArrayList or LinkedList). The class has (at least) the following private field: • An array of MarketProducts. The class must also have the following public methods: • A constructor that takes no inputs and initialize the field with an empty array. • A getProducts() method which takes no inputs and returns a shallow copy of the array (NOT a copy of the reference!) of MarketProducts of this basket (with the elements in the same order). • An add() method which takes as input a MarketProduct and does not return any value. The method adds the MarketProduct at the end of the list of products of this basket. • A remove() method which takes as input a MarketProduct and returns a boolean. The method removes the first occurrence of the specified element from the array of products of this basket. If no such product exists, then the method returns false, otherwise, after removing it, the method returns true. Note that this method removes a product from the list if and only if such product is equal to the input received. For example, it is not possible to remove 0.25 Kg of McIntosh apples for a 0.5 Kg McIntosh apples MarketProduct. After the product has been remove from the array, the subsequent elements should be shifted down by one position, leaving no unutilized slots in the middle array. • A clear() method which takes no inputs, returns no values, and empties the array of products of this basket. • A getNumOfProducts() method that takes no inputs and returns the number of products present in this basket. • A getSubTotal() method that takes no inputs and returns the cost (in cents) of all the products in this basket. • A getTotalTax() method that takes no inputs and returns the tax amount (in cents) to be paid based on the product in this basket. Since we are in Quebec, you can use a tax