程序求助,朋友们能帮帮忙吗?
程序要求是这样的:有一组重量大于0而小于等于1的物品,把它们装入箱子中,每个箱子能容纳的重量不能超过1,求用最少的箱子装这些物品。(程序已经给了BinaryHeap.java/PriorityQueue.java /UnderFlowException.java 这三个Class, 要求写的是Item.java/Box.java/Strategy.java/Test.java)
以下是英文原文:
You will have to build several classes which will put several items with different weights into the MINIMUM number of boxes. Each item will have 2 data - an identifier and a weight, W (0<W<=1).
You are required to implement TWO (2) related strategies, and to compare the number of boxes used by each strategy for given set of items.
Strategy 1:
Arrange items into boxes.
E.g. weights 0.4, 0.4, 0.6, 0.6 --> 2 boxes
Strategy 2:
Sort the items into decreasing order by weight first, then use Strategy 1.
CLASSES GIVEN:
--------------
BinaryHeap.java PriorityQueue.java UnderFlowException.java
CLASSES YOU NEED TO BUILD:
--------------------------
1. Item class
- a constructor of Item containing an integer identifier and a double weight
- this constructor only allows weight W (0<W<=1)
- throw an exception if constructor passes a bad weight (e.g. W>1 or W<0)
- methods to return items identifier and weight
- a toString method to return a left bracket, identifier, a comma, a space, a weight and
a right bracket (e.g. "(2, 0.359)")
2. Box class
- start counting at 1.0
- list of item object in the box
- a method to return the weight can be added, already added, number of items in the box,
the i-th item in the box (throw exception if i is invalid), list of all items in the box
- toString method contains weight available, weight of items in box, number of items in the
box, list of items in the box
- implement this class with Comparable interface - compare two boxes, compare the weight of
items to pack into boxes, and should return the list of packed boxes
3. Strategy class
- contains Strategy 1 method and Strategy 2 method
- first implement Strategy 1, then implement Strategy 2
- each method accept only a parameter of list of items to pack into boxes and return the
list of packed boxes
4. Test class
- contains the main method
- opens a text file containing a single line with a list of items weights
(i.e. 0.3, 0.249, 0.95, 0.34, 0.21, 0.13, 0.79, 0.2, 0.86)
- creates a list of items
- calls each methods for Strategy 1 and 2
- prints out the resulting list of boxes of each call