Midterm, pt 2 – PAC I

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (5 votes)

1. (25 points)
Implement a class ‘ShoppingCart’ that models the shopping cart of an e-commerce site. This shopping
cart will manage a list of ‘Item’ objects. (You do not need to implement ‘Item’, only use it.)
ShoppingCart has the following instance variables:
→ items An array of Item objects representing the cart’s contents.
The cart can hold a maximum of 100 items.
→ numItems An int that tracks the number of items in the user’s cart.
It should have a getter.
→ taxRate A double representing the tax rate to be applied, defaulted to .05. The value is
the same for all instances of ShoppingCart. It could change, so it should have a setter.
ShoppingCart has the following methods (in addition to accessors and mutators listed above):
→ addItem(Item i) Adds an item to the cart. Returns void. If the cart is full, print an error
message and do not add the item to the cart.
→ subtotal() Returns the combined cost of all the items in the cart not including tax in pennies. Item
has a getCost() method that returns a long.
→ total() Returns the cost of the cart including tax in pennies. Use Math.round().
ShoppingCart has the following constructors:
→ A no-arg constructor.
→ A single argument constructor that takes an item.
Use the programming practices we discussed when designing this class.
(do not repeat code, principle of least privilege, etc.)
2. (10 points)
Part of Item’s implementation is here (there are pieces missing):
public class Item {
private long cost;
public Item (long cost) {

}
public long getCost() {
return cost;
}
}
Extend Item and implement a class called SaleItem that has an additional property ‘discount’. Discount is a double
representing the percent of the discount. Provide a getter for ‘discount’ (no setter necessary).
SaleItem has one constructor that takes two arguments, the super class property ‘cost’ and ‘discount’. Utilize the ‘this’
keyword.
Override any methods necessary in order for our SaleItem class to work with our ShoppingCar class from question 1.
(You’ll use Math.round again.)
3. (15 points)
a. (5 points) Given your implementations above, what would the result of attempting to execute this code be? Why?
public static void main(String[] args) {
Item i1 = new SaleItem(1, 0.5);
Item i2 = new SaleItem(1, 0.5);
System.out.println(i1.equals(i2));
}
b. (5 points) Given your implementations above, what would the result of attempting to execute this code be? Why?
public static void main(String[] args) {
Item i = new SaleItem(1, 0 .5);
System.out.println(i.getDiscount());
}
c. (5 points) Say that we have a class Vehicle from which the class Car is extended. Will the following operation
succeed or fail? In either case, why?
Vehicle v = new Car();