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 liblist.LinkedList;
import java.util.Iterator;
import java.util.Random;
public class CargoSimulator
protected LinkedList fleet;
private static int randomSeed ; 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 CargoSimulatorint 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 RandomrandomSeed;
Create the fleet from the ship names in Ship.ShipNames
this.fleet new LinkedList;
forint i; i Ship.ShipNames.length; i
this.fleet.insertFirstnew ShipShipShipNamesi generator.nextInt;
Load each ship with some sacks of various grains.
forint i; i numberOfSacks; i
Generate a random grain type, sack weight, and create the sack object.
Grain type Grain.valuesgeneratornextIntGrainvalueslength;
float weight generator.nextFloat;
Sack thisSack new Sacktype weight;
Pick a random ship to load it onto.
String ship Ship.ShipNamesgeneratornextIntShipShipNames.length;
Find the ship in the list of ships and load our randomly generated sack of grain.
this.fleet.goFirst;
whilethisfleet.itemExists && this.fleet.itemgetNamecompareToship
this.fleet.goForth;
try
this.fleet.itemloadSackthisSack;
catchException e
System.out.printlnI 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;
whilethisfleet.itemExists
out this.fleet.itemtoString;
this.fleet.goForth;
return out;
public static void mainString args
Create a new cargo simulator object.
CargoSimulator sim new CargoSimulator;
for int i ; i Ship.ShipNames.length; i
for int i ; i Ship.ShipNames.length; i
System.out.printlnShipShipNamesi;
System.out.printlnsimfleet.;
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.
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 sacks of wheat, the method
should return
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.
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
