Answered step by step
Verified Expert Solution
Question
1 Approved Answer
code starter: Ship.java > > > > > > > > > > > > > import lib 2 8 0 . list.LinkedList 2 8
code starter: Ship.java import liblist.LinkedList; import java.util.Iterator; public class Ship extends LinkedList These are the name of the ships in Tractor Jack's fleet. This is used ot initialize data for the assignemnt. Note that it is a static variable, and not an instance variable. public static String ShipNames "The Prairie Onion", "The Blackstrap", "The Bunnyhug", "The Salty Farmer", "The Icebreaker" ; List of sacks of grain to be hauled by this ship. protected static LinkedList cargo; The name of this ship. protected String name; The capacity in pounds of this ship. A ship cannot carry more than this much grain. protected float capacity; Create a new ship. @param name Name of ths hip. @param capacity Capacity of the ship in pounds. The ship can carry sacks of grain with total weight up to this value. public ShipString name, float capacity super; this.cargo new LinkedList; this.name name; this.capacity capacity; Load a sack of grain onto this ship. @param s The sack of grain to load. public void loadSackSack s Add the sack to the list of sacks this ship is carrying. Since this is only a simulation, we don't care if this causes the ship to be overloaded at the time of loading. cargo.insertFirsts; Sack.java import liblist.LinkedList; enum Grain WHEAT, BARLEY, OATS, RYE, OTHER public class Sack extends LinkedList protected Grain type; protected double weight; @param type the type of grain @param weight how much grain in pounds public SackGrain type, double weight super; this.type type; this.weight weight; @return the type of grain public Grain getType return type; @param type the type of grain for this plunder public void setTypeGrain type this.type type; @return the weight of grain for this plunder public double getWeight return weight; @param weight the weight of grain for this plunder public void setWeightdouble weight this.weight weight; public String toString return type.toString weight ; CargoSimulator.java 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!"; Qeustions: 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 type indicated by its parameter that are in the ship's cargo. That is if the parameter type is Grain. Wheat and the ship's cargo contains sacks of wheat, the method should return in the main method of CargoSimulator.java, there is an instance if 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.
code starter:
Ship.java
import liblist.LinkedList;
import java.util.Iterator;
public class Ship extends LinkedList
These are the name of the ships in Tractor Jack's fleet. This is used ot initialize data for the assignemnt.
Note that it is a static variable, and not an instance variable.
public static String ShipNames "The Prairie Onion", "The Blackstrap", "The Bunnyhug", "The Salty Farmer", "The Icebreaker" ;
List of sacks of grain to be hauled by this ship.
protected static LinkedList cargo;
The name of this ship.
protected String name;
The capacity in pounds of this ship. A ship cannot carry more than this much grain.
protected float capacity;
Create a new ship.
@param name Name of ths hip.
@param capacity Capacity of the ship in pounds. The ship can carry sacks of grain with total weight up to this value.
public ShipString name, float capacity
super;
this.cargo new LinkedList;
this.name name;
this.capacity capacity;
Load a sack of grain onto this ship.
@param s The sack of grain to load.
public void loadSackSack s
Add the sack to the list of sacks this ship is carrying. Since this is only a simulation, we don't
care if this causes the ship to be overloaded at the time of loading.
cargo.insertFirsts;
Sack.java
import liblist.LinkedList;
enum Grain
WHEAT, BARLEY, OATS, RYE, OTHER
public class Sack extends LinkedList
protected Grain type;
protected double weight;
@param type the type of grain
@param weight how much grain in pounds
public SackGrain type, double weight
super;
this.type type;
this.weight weight;
@return the type of grain
public Grain getType
return type;
@param type the type of grain for this plunder
public void setTypeGrain type
this.type type;
@return the weight of grain for this plunder
public double getWeight
return weight;
@param weight the weight of grain for this plunder
public void setWeightdouble weight
this.weight weight;
public String toString
return type.toString weight ;
CargoSimulator.java
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!";
Qeustions:
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 type indicated by its parameter that are in the ship's cargo. That is if the parameter type is Grain. Wheat and the ship's cargo contains sacks of wheat, the method should return
in the main method of CargoSimulator.java, there is an instance if 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started