As Apple announced April 4th ,2010 as the new released day of the mighty iPad(late April for the 3G model with a separate plan), Apple's fan finally find something they have always looking for at a cost of a netbook. A fully touchable multimedia device with the height of 9.56 inches(242.8 mm), Width: 7.47 inches(about 189.7 mm) and it weights around 1.6 pound for the wifi with 3G model, and 1.5 pound for the standard integrated wifi model. Overall, the price seem quite reasonable with a start of $499.00 USD for the base model. This device will give Apple a fresh start as well as a new toy within the entertainment world. As nobody have seen the device yet, more update will be coming soon...
Search This Blog
Wednesday, March 31, 2010
April 4th... iPad finally coming to the wild world
As Apple announced April 4th ,2010 as the new released day of the mighty iPad(late April for the 3G model with a separate plan), Apple's fan finally find something they have always looking for at a cost of a netbook. A fully touchable multimedia device with the height of 9.56 inches(242.8 mm), Width: 7.47 inches(about 189.7 mm) and it weights around 1.6 pound for the wifi with 3G model, and 1.5 pound for the standard integrated wifi model. Overall, the price seem quite reasonable with a start of $499.00 USD for the base model. This device will give Apple a fresh start as well as a new toy within the entertainment world. As nobody have seen the device yet, more update will be coming soon...
Last Assignment of the year
COMP1005 - Assignment #4
Due:
April 1at 11:55 pm
In this assignment you will simulate a
Customer that goes to a grocery store and buys items. The Customerwill
select
GroceryItemsand place them into an ArrayList representing a grocery basket and then when buying the
items, he will pack them into
GroceryBagswhich will also use ArrayLists. Implement the classes and methods as
described below.
(1) The
GroceryItemClass:
Here is a class that represents a
GroceryItemthat you buy at the grocery store:
public class
GroceryItem {
private
String name;
private
float price;
private
float weight;
private
boolean perishable;
public
GroceryItem() { this("", 0, 0, false); }
public
GroceryItem(String n, float p, float w, booleanper) {
name = n;
price = p;
weight = w;
perishable = per;
}
public
String getName() { returnname; }
public float
getWeight() { returnweight; }
public float
getPrice() { returnprice; }
public boolean
isPerishable() { returnperishable; }
public
String toString () {
return
name + " weighing " + weight + " kg with price $"+ price;
}
}
Complete this class by implementing an
equals() method that takes an Object as a parameter and returns aboolean
indicating whether or not two GroceryItems are equal. Note that GroceryItems are only equal if they have the same
name and weight (note that price doesn't matter). Test your method with the following test program:
public class
GroceryItemTestProgram {
public static void
main(String args[]) {
GroceryItem g1 =
new GroceryItem("Ocean Spray Cranberry Cocktail", 2.99f, 2.26f, false);
GroceryItem g2 =
new GroceryItem("Heinz Beans Original", 0.79f, 0.477f, false);
GroceryItem g3 =
new GroceryItem("Ocean Spray Cranberry Cocktail", 1.77f, 2.26f, false);
System.out.println(g1.equals(g1));
// true
System.out.println(g1.equals(g2));
// false
System.out.println(g1.equals(g3));
// true
}
}
(2) The
GroceryBagClass:
Implement a class called
GroceryBag that represents a bag which will holdGroceryItem
objects. The class should have the following
private staticvariable:
MAXWEIGHT = 5;
of type floatwhich indicates the maximum weight that the bag
can hold (i.e., set to 5 kg)
The class must have the following
privateinstance variables:
items which is an
ArrayListobjects.
weight of type
floatwhich stores the weight of the bag with its current items.
Don't forget to
import java.util.ArrayList. For the GroceryBagclass, create the following methods:
Appropriate
publicget methods
A zero-parameter constructor.
A
toString()method that displays a string showing the number of items in the bag and the total weight as
follows: "
4.3 kg GroceryBag with 8 items". However, if the bag is empty, this method should return "An
empty GroceryBag
".
A method called
addItem(GroceryItem g) which adds the given GroceryItemto the bag, provided that the
weight of the item will not cause the bag to exceed the MAXWEIGHT. Make sure to update the total weight
variable.
A method called
removeLastItem() which removes the LASTitem that was added to the bag. Make sure to
update the total weight again. If there are no items in the bag, do nothing.
Test your class with the following test program:
public class
GroceryBagTestProgram {
public static void
main(String args[]) {
GroceryItem g1 =
new GroceryItem("Jumbo Cherries", 6.59f, 1.0f, false);
GroceryItem g2 =
new GroceryItem("Smart-Ones Frozen Entrees", 1.99f, 0.311f, true);
GroceryItem g3 =
new GroceryItem("SnackPack Pudding", 0.99f, 0.396f, false);
GroceryItem g4 =
new GroceryItem("Nabob Coffee", 3.99f, 0.326f, false);
GroceryItem g5 =
new GroceryItem("Gold Seal Salmon", 1.99f, 0.213f, false);
GroceryBag b1 =
newGroceryBag();
GroceryBag b2 =
newGroceryBag();
b1.
addItem(g1);
b1.
addItem(g2);
b1.
addItem(g3);
b1.
addItem(g4);
b1.
addItem(g5);
b1.
addItem(g5);
b1.
addItem(g5);
System.out.println(b1);
System.out.println(b2);
System.out.println(b1.getItems());
b1.
removeLastItem();
b1.
removeLastItem();
b1.
removeLastItem();
System.out.println(b1);
System.out.println(b1.getItems());
b1.
addItem(new GroceryItem("Coca-Cola 12-pack", 3.49f, 5.112f, false));
b2.
addItem(new GroceryItem("Coca-Cola 12-pack", 3.49f, 5.112f, false));
System.out.println(b1);
System.out.println(b2);
}
}
Here is the output that you should see (the array lists of items will appear on one line though):
A 2.6720002 kg GroceryBag with 7 items
An empty GroceryBag
[Jumbo Cherries weighing 1.0 kg with price $6.59,
Smart-Ones Frozen Entrees weighing 0.311 kg with price $1.99,
SnackPack Pudding weighing 0.396 kg with price $0.99,
Nabob Coffee weighing 0.326 kg with price $3.99,
Gold Seal Salmon weighing 0.213 kg with price $1.99,
Gold Seal Salmon weighing 0.213 kg with price $1.99,
Gold Seal Salmon weighing 0.213 kg with price $1.99]
A 2.033 kg GroceryBag with 4 items
[Jumbo Cherries weighing 1.0 kg with price $6.59,
Smart-Ones Frozen Entrees weighing 0.311 kg with price $1.99,
SnackPack Pudding weighing 0.396 kg with price $0.99,
Nabob Coffee weighing 0.326 kg with price $3.99]
A 2.033 kg GroceryBag with 4 items
An empty GroceryBag
(3) The
CustomerClass:
Implement a class called
Customerwhich represents a person that buys
grocery items. The
Customershould keep the following instance variable:
shoppingCart which is an
ArrayListof all the
GroceryItem
objects that the customer is planning to purchase.
Create the following methods.
public
get method for the shopping cart.
A zero-argument constructor.
A
toString() method that returns a string representation of the customer in the form: "Customer with
shopping cart of 10 items
".
An
addItem(GroceryItem g) method that adds a given GroceryItemto the shopping cart.
A
removeItem(GroceryItem g) method that removes a given GroceryItemfrom the shopping cart.
Create the following 5 interesting
Customermethods. A thorough test case is given at the end of this section. Use
it to test your program as you go by commenting out the parts that you did not complete yet:
A
heaviestItem()method that returns the heaviest item in the shopping cart.
A
alreadyHave(GrocerytItem g) method that takes a GroceryItem object and returns a booleanindicating
whether or not the item is currently in the shopping cart.
A private
printReceipt()method that prints out a receipt for all items in the shopping cart when they are
purchased. The receipt must be nicely displayed with proper formatting (i.e., everything lined up perfectly).
It should show the subtotal of all costs, the GST and PST amounts, and finally the Amount Due. Below is
what it should look like. Note that to align the items as you loop through them, make use of the
String.format()
method which displays strings and floats nicely. For example, the following command
String.format("%15s %10.2f", myString, myFloat)
returns a String object in which the characters from
myString
are displayed (taking up 15 spaces) followed by a space character and then finishing withmyFloat
value (displayed with 2 decimal places and taking up 10 characters in total). You just need to play around
with the numbers here to get what you want. Note that the
String.format()method simply creates a new
String...it does not display anything.
Smart-Ones Frozen Entrees 1.99
SnackPack Pudding 0.99
Breyers Chocolate Icecream 2.99
Nabob Coffee 3.99
Gold Seal Salmon 1.99
Ocean Spray Cranberry Cocktail 2.99
Heinz Beans Original 0.79
Lean Ground Beef 4.94
5-Alive Frozen Juice 0.75
Coca-Cola 12-pack 3.49
Smart-Ones Frozen Entrees 1.99
Ocean Spray Cranberry Cocktail 2.99
SnackPack Pudding 0.99
SnackPack Pudding 0.99
Breyers Chocolate Icecream 2.99
Breyers Chocolate Icecream 2.99
Breyers Chocolate Icecream 2.99
Breyers Chocolate Icecream 2.99
Breyers Chocolate Icecream 2.99
Coca-Cola 12-pack 3.49
Toilet Paper - 48 pack 40.96
5-Alive Frozen Juice 0.75
Gold Seal Salmon 1.99
Ocean Spray Cranberry Cocktail 2.99
Heinz Beans Original 0.79
Lean Ground Beef 4.94
Lean Ground Beef 4.94
Lean Ground Beef 4.94
Gold Seal Salmon 1.99
---------------------------------------
Subtotal $114.60
GST $ 5.73
PST $ 9.17
---------------------------------------
Amount Due $129.50
A private
packBags() method which returns a mixed ArrayList of packed GroceryBagobjects and possibly
some unpacked
GroceryItems(e.g., case of coke). The method should use the following packing algorithm:
Fill as many bags as are necessary one at a time, each to just below its weight limit. That is take items in any
order from the shopping cart and place them into an initially empty bag and stop when the next item will cause
the bag to exceed its weight limit. Then make a new bag and pack that one until it is full. The items are taken
sequentially from the basket in any order (although the test code given assumes that you take them in the order
that they were added to the cart). As each item is packed it should be removed from the shopping cart (use the
removeItem()
method). If an item is too heavy (i.e., exceeds the MAXWEIGHT) to be placed in a bag (e.g., a
case of coke), then it should be added separately to the
ArrayListthat is being returned. Hence, the returned
ArrayList
may contain both GroceryBag and GroceryItemobjects.
A private
displayPackedBags(ArrayList bags) method that takes the result of the packBags()method (i.e., an
ArrayList of
GroceryBag and unpacked GroceryItemobjects) and displays them nicely as follows. Note the
2 space indentation for easy readability. There is a minor bug in JCreator that sometimes leaves a blank line
in the output at weird locations ... do not lose any sleep over this as there is nothing you can do about it ;)
GROCERY BAG
Smart-Ones Frozen Entrees weighing 0.311 kg with price $1.99
SnackPack Pudding weighing 0.396 kg with price $0.99
Breyers Chocolate Icecream weighing 2.27 kg with price $2.99
Nabob Coffee weighing 0.326 kg with price $3.99
Gold Seal Salmon weighing 0.213 kg with price $1.99
UNPACKED ITEM
Coca-Cola 12-pack weighing 5.112 kg with price $3.49
GROCERY BAG
Ocean Spray Cranberry Cocktail weighing 2.26 kg with price $2.99
Heinz Beans Original weighing 0.477 kg with price $0.79
Lean Ground Beef weighing 0.75 kg with price $4.94
5-Alive Frozen Juice weighing 0.426 kg with price $0.75
Smart-Ones Frozen Entrees weighing 0.311 kg with price $1.99
GROCERY BAG
Ocean Spray Cranberry Cocktail weighing 2.26 kg with price $2.99
SnackPack Pudding weighing 0.396 kg with price $0.99
SnackPack Pudding weighing 0.396 kg with price $0.99
GROCERY BAG
Breyers Chocolate Icecream weighing 2.27 kg with price $2.99
Breyers Chocolate Icecream weighing 2.27 kg with price $2.99
GROCERY BAG
Breyers Chocolate Icecream weighing 2.27 kg with price $2.99
Breyers Chocolate Icecream weighing 2.27 kg with price $2.99
UNPACKED ITEM
Coca-Cola 12-pack weighing 5.112 kg with price $3.49
UNPACKED ITEM
Toilet Paper - 48 pack weighing 10.89 kg with price $40.96
GROCERY BAG
Breyers Chocolate Icecream weighing 2.27 kg with price $2.99
5-Alive Frozen Juice weighing 0.426 kg with price $0.75
Gold Seal Salmon weighing 0.213 kg with price $1.99
GROCERY BAG
Ocean Spray Cranberry Cocktail weighing 2.26 kg with price $2.99
Heinz Beans Original weighing 0.477 kg with price $0.79
Lean Ground Beef weighing 0.75 kg with price $4.94
Lean Ground Beef weighing 0.75 kg with price $4.94
Lean Ground Beef weighing 0.75 kg with price $4.94
GROCERY BAG
Gold Seal Salmon weighing 0.213 kg with price $1.99
A public
makePurchase()method that takes the shopping cart and then prints a receipt (using your method
above), packs the bags (using your method above) and displays the result (using your method above).
A public
unpackPerishables(ArrayListmethod that takes an ArrayList of packed
GroceryBag
objects (from your packBags()method) and removes all the perishable items from the bags.
Note that the
GroceryBagsshould be modified (changed) by this method in that they will have less in them
afterwards. Do not discard any bags. At the end of the method, call
displayPackedBags()again to see if it
worked. Here is what you should see:
GROCERY BAG
SnackPack Pudding weighing 0.396 kg with price $0.99
Nabob Coffee weighing 0.326 kg with price $3.99
Gold Seal Salmon weighing 0.213 kg with price $1.99
UNPACKED ITEM
Coca-Cola 12-pack weighing 5.112 kg with price $3.49
GROCERY BAG
Ocean Spray Cranberry Cocktail weighing 2.26 kg with price $2.99
Heinz Beans Original weighing 0.477 kg with price $0.79
GROCERY BAG
Ocean Spray Cranberry Cocktail weighing 2.26 kg with price $2.99
SnackPack Pudding weighing 0.396 kg with price $0.99
SnackPack Pudding weighing 0.396 kg with price $0.99
GROCERY BAG
GROCERY BAG
UNPACKED ITEM
Coca-Cola 12-pack weighing 5.112 kg with price $3.49
UNPACKED ITEM
Toilet Paper - 48 pack weighing 10.89 kg with price $40.96
GROCERY BAG
Gold Seal Salmon weighing 0.213 kg with price $1.99
GROCERY BAG
Ocean Spray Cranberry Cocktail weighing 2.26 kg with price $2.99
Heinz Beans Original weighing 0.477 kg with price $0.79
GROCERY BAG
Gold Seal Salmon weighing 0.213 kg with price $1.99
As promised, here is the testing code:
public class
CustomerTestProgram {
public static void
main(String args[]) {
GroceryItem g1 =
new GroceryItem("Smart-Ones Frozen Entrees", 1.99f, 0.311f, true);
GroceryItem g2 =
new GroceryItem("SnackPack Pudding", 0.99f, 0.396f, false);
GroceryItem g3 =
new GroceryItem("Breyers Chocolate Icecream", 2.99f, 2.27f, true);
GroceryItem g4 =
new GroceryItem("Nabob Coffee", 3.99f, 0.326f, false);
GroceryItem g5 =
new GroceryItem("Gold Seal Salmon", 1.99f, 0.213f, false);
GroceryItem g6 =
new GroceryItem("Ocean Spray Cranberry Cocktail", 2.99f, 2.26f, false);
GroceryItem g7 =
new GroceryItem("Heinz Beans Original", 0.79f, 0.477f, false);
GroceryItem g8 =
new GroceryItem("Lean Ground Beef", 4.94f, 0.75f, true);
GroceryItem g9 =
new GroceryItem("5-Alive Frozen Juice", 0.75f, 0.426f, true);
GroceryItem g10 =
new GroceryItem("Coca-Cola 12-pack", 3.49f, 5.112f, false);
GroceryItem g11 =
new GroceryItem("Toilet Paper - 48 pack", 40.96f, 10.89f, false);
// Make a new customer and add some items to his/her shopping cart
Customer c =
newCustomer();
c.addItem(g1);
c.addItem(g2);
c.addItem(g3);
c.addItem(g4);
c.addItem(g5);
// Check the heaviest item and whether some particular items are in there
System.out.println(c.
heaviestItem());//Breyers Chocolate Icecream
System.out.println(c.
alreadyHave(g1));//true
System.out.println(c.
alreadyHave(g2));//true
System.out.println(c.
alreadyHave(g8));//false
System.out.println(c.
alreadyHave(g9));//false
// Now add some more items
c.addItem(g6);
c.addItem(g7);
c.addItem(g8);
c.addItem(g9);
c.addItem(g10);
// Re-check the heaviest item and others
System.out.println(c.
heaviestItem());//Coca-Cola 12-pack
System.out.println(c.
alreadyHave(g8));//true
System.out.println(c.
alreadyHave(g9));//true
// Now add a lot more items, somewhat randomly
c.addItem(g1); c.addItem(g6); c.addItem(g2); c.addItem(g2);
c.addItem(g3); c.addItem(g3); c.addItem(g3); c.addItem(g3);
c.addItem(g3); c.addItem(g10); c.addItem(g11); c.addItem(g9);
c.addItem(g5); c.addItem(g6); c.addItem(g7); c.addItem(g8);
c.addItem(g8); c.addItem(g8); c.addItem(g5);
// Now make the purchase showing the receipt and packed bags
System.out.println(
"\n\nMaking the purchase, showing receipt and packed bags:");
c.
makePurchase();
// Finally unpack the perishables and show the packed bags again
System.out.println(
"\n\nUnpacking perishables, showing packed bags:");
c.
unpackPerishables();
}
}
(4) The
PerishableItemsubclass:
Copy all of your code so far into a folder called
Solutions1to3. Make sure not to simply movethe code but to
actually make a
copyof it. The copy that you put in this folder will be handed in as is, do not make changes to it.
This code will be a backup of your previous work to this point. We will now modify the code that you were
working on.
Open the
GroceryItem.javafile in JCreator and go through it, removing any code and variables that have to do with
the item being perishable. Check all methods. Save and compile the code.
Create, save and compile the following class definition in a file called
PerishableItem.java:
public class
PerishableItem extendsGroceryItem {
public
PerishableItem(String n, float p, floatw) {
super
(n, p, w);
}
}
Open the
GroceryBagTestProgram.javafile in JCreator and go through the code. For all GroceryItems that were
perishable, change the class name to
PerishableItem. Lastly, remove the last parameter to the all theGroceryItem
and
PerishableItem constructor calls, because we discarded the perishableinstance variable. Run this test program
and make sure that it works as before. You have now made a subclass of GroceryItem and are using it. Notice that
we did not really add much code at all, we just removed some useless code. However, we will now see that we need
to make changes in the
Customerclass.
Try re-compiling your
Customer class. You should notice that your unpackPerishables()method no longer
compiles. Modify the code so that it works again. To test it, you will have to modify the
CustomerTestProgram
by changing the perishable
GroceryItem objects to PerishableItemobjects and remove the last constructor
parameter as you did before in the
GroceryBagTestProgram. The test code should give the same solution as
before. Do you know why we went through all this trouble ? The code is actually simpler overall.
(5) The
Carryableinterface:
Create, save and compile the following interface definition in a file called
Carryable.java:
import
java.util.ArrayList;
public interface
Carryable {
public
ArrayList
public
String getDescription();
}
Change the
GroceryItem and GroceryBag classes so that they implement the Carryableinterface. Note that you
will have to change the first line of the class definition as well as implement the
getContents() andget
Description()
methods in both these classes. For GroceryItem objects, the getContents()method should return an
ArrayList
method should
return the bag's
items. For GroceryItem objects, the getDescription()method should return the following string:
"
UNPACKED ITEM" whereas for GroceryBag objects it should return "GROCERY BAG".
Go back to your
Customer class and modify your packBags() and displayPackedItems()methods so that the type
ArrayList
of backed bags is now ArrayListmethod, remove all
code that checks instanceof. Make use of the
getDescription() and getContents()methods in the interface to
simplify your code. Run the
CustomerTestProgramto make sure that it still works the same way with the same
results.
Go through your
unpackPerishables()method as well and simplify it making use of the fact that the items are all
Carryable
. Remove any usage of instanceof, simplify your forloop and allow unpacked items to be removed as
well if necessary (although in our test case, none of our unpacked items are perishable). Run the
CustomerTestProgram
to make sure that it still works the same way with the same results.
Hopefully, you noticed how much simpler your methods became and that you understand how code can be
simplified using interfaces.
NOTE: For each coding question, submit your
.java and your compiled .class files. Describe your testsclearly in a readMe file. Submit early!!!!