Write a program that uses heuristics (that a bagger at a grocery store might use
ID: 3571644 • Letter: W
Question
Write a program that uses heuristics (that a bagger at a grocery store might use) to fill a shopping bag (an instance of a Bag class) from a whole bunch of grocery items. How you store the initial "bunch" of grocery items is up to you Grocery items' have properties such as weight, breakability, squishability, etc. As the bag is filled items are selected so that roughly (that's why it's heuristic and not an algorithm): The heaviest items (bag of potatoes) are near the bottom (added earlier) The items that are the most squishable (bread) and breakable (eggs) arc near the top (added later). Alternatively, place different categories of items into different bags (preferred). You may place a limit of the number, total size, and/or total weight in each bag.Explanation / Answer
//below is the code to implement your program please revert in case of any query
import java.util.ArrayList;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.HashBag;
class test extends HashBag{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String args[]){
Bag br, sq, nm;
br = new HashBag();//breakable item bag
sq = new HashBag();//squashable item bag
nm = new HashBag();//normal item bag
GroceryItem potatoes = new GroceryItem("potato", 500, false, true);
GroceryItem eggs = new GroceryItem("Eggs", 100, true, false);
GroceryItem breads = new GroceryItem("Bread", 300, false, true);
ArrayList<GroceryItem> ll = new ArrayList<GroceryItem>();
ll.add(breads);
ll.add(eggs);
ll.add(potatoes);
for(GroceryItem ss: ll){
if(ss.breakable==true){
br.add(ss);
}
if(ss.squashable==true){
sq.add(ss);
}
nm.add(ss);
}
}
}
class GroceryItem{
String name;
float weight;
boolean breakable, squashable;
GroceryItem(String name, float weight, boolean breakable, boolean squashable){
this.name = name;
this.weight = weight;
this.breakable = breakable;
this.squashable = squashable;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.