Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 . In Ship.java, complete the isOverloaded method. This method must return a boolean value true if the ship is overloaded, and false otherwise. The
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.
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;
Check whether this ship is overloaded.
@return true if the total weight of all sacks of grain of any type on this ship exceeds this.capacity,
false otherwise.
public boolean isOverloaded
int totalW ;
Iterator iterator Iterator cargo.iterator;
while iteratorhasNext
totalW iterator.nextgetWeight;
return totalW capacity;
public static int sacksOfGrainTypeGrain type
int count ;
Iterator iterator Iterator cargo.iterator;
while iteratorhasNext
Sack sack iterator.next;
if sackgetType type
count;
return count;
Accessor methods
public LinkedList getCargo
return cargo;
public String getName
return name;
public float getCapacity
return capacity;
@return a printable string describing the ship's name and contents.
public String toString
return this.name has a this.capacity pound capacity and carries: cargo.toString
;
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
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