Question: import lib 2 8 0 . list.LinkedList 2 8 0 ; import java.util.Iterator; import java.util.Random; public class CargoSimulator { protected LinkedList 2 8 0 fleet;

import lib280.list.LinkedList280;
import java.util.Iterator;
import java.util.Random;
public class CargoSimulator{
protected LinkedList280 fleet;
private static int randomSeed =42; // Don't modify this!
/**
* Construtor will generate a "fixed random" cargo for each ship using the random seed randomSeed.
*
* @param numberOfSacks Total number of sacks of grain to be carried by the fleet.
* These are automatically generated.
*
* @postcond The instance variable 'fleet' is a list of ships, each one of which contains a list of sacks of cargo.
*/
public CargoSimulator(int numberOfSacks){
// DO NOT MODIFY THE CONSTRUCTOR -- This is generating the data you need to complete the assignment.
// Seed the random number generator.
Random generator = new Random(randomSeed);
// Create the fleet from the ship names in Ship.ShipNames
this.fleet = new LinkedList280();
for(int i=0; i < Ship.ShipNames.length; i++){
this.fleet.insertFirst(new Ship(Ship.ShipNames[i], generator.nextInt(19500)+500));
}
// Load each ship with some sacks of various grains.
for(int i=0; i < numberOfSacks; i++){
// Generate a random grain type, sack weight, and create the sack object.
Grain type = Grain.values()[generator.nextInt(Grain.values().length)];
float weight = generator.nextFloat()*100;
Sack thisSack = new Sack(type, weight);
// Pick a random ship to load it onto.
String ship = Ship.ShipNames[generator.nextInt(Ship.ShipNames.length)];
// Find the ship in the list of ships and load our randomly generated sack of grain.
this.fleet.goFirst();
while(this.fleet.itemExists() && this.fleet.item().getName().compareTo(ship)!=0){
this.fleet.goForth();
}
try {
this.fleet.item().loadSack(thisSack);
}
catch(Exception e){
System.out.println("I didn't find a ship named "+ ship +". That shouldn't happen!");
}
}
}
/**
* @return A printable string describing the name of each ship in the fleet and it's contents.
*/
public String toString(){
// DO NOT MODIFY THIS METHOD
String out ="";
this.fleet.goFirst();
while(this.fleet.itemExists()){
out += this.fleet.item().toString();
this.fleet.goForth();
}
return out;
}
public static void main(String args[]){
// Create a new cargo simulator object.
CargoSimulator sim = new CargoSimulator(1000);
// for (int i =0; i < Ship.ShipNames.length; i++){
//}
// for (int i =0; i < Ship.ShipNames.length; i++){
// System.out.println(Ship.ShipNames[i]);
System.out.println(sim.fleet.);
}
}
Questions:
In Ship.java, complete the isOverloaded method. This method must return a boolean value
true if the ship is overloaded, and false otherwise. The ship is overloaded if the total weight of
all sacks of grain in its cargo exceeds the ships capacity.
2. In Ship.java, complete the sacksOfGrainType method. This method must return the number of
sacks of grain of the grain type indicated by its parameter that are in the ships cargo. That is, if
the parameter type is Grain.WHEAT and the ships cargo contains 42 sacks of wheat, the method
should return 42.
3. In the main() method of CargoSimulator.java, there is an instance of a CargoSimulator object
called sim. As described above, this object contains a list of ships, and each ship contains its list of
cargo. At the location indicated, print out how many sacks of wheat each ship in sim is carrying.
4. In the main() method of CargoSimulator.java, print out a message for each ship in the CargoSimulator
instance sim that is overloaded. If a ship is not overloaded, print nothing.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!