Q1) public class Product { private String name; private double weight; private String manufacturer; public Product(String name, double weight, String manufacturer) { this.name = name; this.weight = weight; this.manufacturer = manufacturer; } public String getName() { return this.name; } public void setName(String newName) { this.name = newName; } ÃÂ public double getWeight() { return this.weight; } public void setWeight(double weight) { this.weight = weight; } public String getManufacturer() { return this.manufacturer; } public void setManufacturer(String newManufacturer) { this.manufacturer = newManufacturer; } } Q2) public class Merchant { private String name; private String location; private boolean isOnline; public Merchant(String name, String location, boolean isOnline) { this.name = name; this.location = location; this.isOnline = isOnline; } public String getName() { return this.name; } public void setName(String newName) { this.name = newName; } public String getLocation() { return this.location; } public void setLocation(String location) { this.location = location; } public boolean getIsOnline() { return this.isOnline; } public void setIsOnline(boolean isOnline) { this.isOnline = isOnline; } } Q3) public class Offer{ private Merchant merchant; private Product product; private double currentPrice; private double normalPrice; private boolean limitedSupply; public Offer(Merchant m, Product p, double cp, double np, boolean limit) { this.merchant = m; this.product = p; this.currentPrice = cp; this.normalPrice = np; this.limitedSupply = limit; } public Merchant getMerchant() { return this.merchant; } public Product getProduct() { return this.product; } public double getNormalPrice() { return this.normalPrice; } public double getCurrentPrice() { return this.currentPrice; } public boolean getIsLimitedSupply() { return this.limitedSupply; } } q4) import java.util.ArrayList; public class Store { private ArrayList offers; public Store() { this.offers = new ArrayList(); } //returns an arraylist of every item which has a cheaper //current price than normal price public ArrayList getAllOnSale() { ArrayList onSaleItems = new ArrayList(); for (int i=0; i < this.offers.size(); i++) { Offer current = this.offers.get(i); if (current.getCurrentPrice() < current.getNormalPrice()) { onSaleItems.add(current); } } return onSaleItems; } public Offer getMostDollarSavings() { // this problem is a variation on the problem //"find the maximum value of an array" Offer best = null; double savingsOfBest = 0; for (int i=0; i < this.offers.size(); i++) { Offer current = this.offers.get(i); double savingsOfCurrent = current.getCurrentPrice() - current.getNormalPrice(); if (best == null || savingsOfCurrent < savingsOfBest) { best = current; savingsOfBest = savingsOfCurrent; } } return best; } //this problem is identical to the above except we have //a different way to calculate the "best" public Offer getMostPercentSavings() { // this problem is a variation on the problem //"find the maximum value of an array" Offer best = null; double savingsOfBest = 0; for (int i=0; i < this.offers.size(); i++) { Offer current = this.offers.get(i); //the following few lines are the only changes! double savingsOfCurrent = current.getCurrentPrice() / current.getNormalPrice(); //this will now be > instead of < because //we want to see which offer's current price //is the smallest of the original price //You could use a slightly different formula //to take the offer which saved you the most //percent, but it's the same result either way. if (best == null || savingsOfCurrent > savingsOfBest) { best = current; savingsOfBest = savingsOfCurrent; } } return best; } //this is largely the same as getAllOnSale() except with a different condition public ArrayList getProduct(String name) { ArrayList matchingProducts = new ArrayList(); for (int i=0; i < this.offers.size(); i++) { Offer current = this.offers.get(i); if (current.getProduct().getName().equals(name)) { matchingProducts.add(current.getProduct()); } } return matchingProducts; } public ArrayList getAllLessThan(double price) { ArrayList lessItems = new ArrayList(); for (int i=0; i < this.offers.size(); i++) { Offer current = this.offers.get(i); if (current.getCurrentPrice() < price) { lessItems.add(current); } } return lessItems; } public ArrayList getAllOffers() { return new ArrayList(this.offers); } } public class Shopper { public static ArrayList getAllOffers(ArrayList stores) { ArrayList offers = new ArrayList(); for (int i=0; i < stores.size(); i++) { offers.addAll(stores.get(i).getAllOffers()); //note that you could use a loop instead of the //above statement if you didn't know of the addAll //method } return offers; } public static double getValue(ArrayList offers) { if (offers == null) { return 0; } double value = 0; for (int i=0; i < offers.size(); i++) { value = value + PracticeLongAnswer.getUtility(offers.get(i).getProduct()); } return value; } /*note that question omitted a step to convert the stores to an arraylist of offers. Sorry!*/ public static ArrayList getMostUseful(ArrayList stores, double availableMoney) { return getMostUseful(getAllOffers(stores), availableMoney, new ArrayList()); } private static ArrayList getMostUseful(ArrayList offers, double availableMoney, ArrayList chosen) { if (offers.size() == 0) { return chosen; } if (availableMoney < 0) { return null; } ArrayList duplicateOffers = new ArrayList(offers); ArrayList duplicateChosen = new ArrayList(chosen); Offer first = duplicateOffers.get(0); duplicateOffers.remove(0); duplicateChosen.add(first); ArrayList withFirst = getMostUseful(duplicateOffers, availableMoney - first.getCurrentPrice(), duplicateChosen); duplicateOffers = new ArrayList(offers); duplicateChosen = new ArrayList(chosen); duplicateOffers.remove(0); ArrayList withoutFirst = getMostUseful(duplicateOffers, availableMoney, duplicateChosen); if (getValue(withFirst) < getValue(withoutFirst)) { return withoutFirst; } else { return withFirst; } } }