Answered step by step
Verified Expert Solution
Link Copied!

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

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 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.
Code starter:
Ship.java>>>>
import lib280.list.LinkedList280;
import java.util.Iterator;
public class Ship extends LinkedList280{
// 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 LinkedList280 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 Ship(String name, float capacity){
super();
this.cargo = new LinkedList280();
this.name = name;
this.capacity = capacity;
}
/**
* Load a sack of grain onto this ship.
* @param s The sack of grain to load.
*/
public void loadSack(Sack 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.insertFirst(s);
}
/**
* 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 =0;
Iterator iterator =(Iterator) cargo.iterator();
while (iterator.hasNext()){
totalW += iterator.next().getWeight();
}
return totalW > capacity;
}
public static int sacksOfGrainType(Grain type){
int count =0;
Iterator iterator =(Iterator) cargo.iterator();
while (iterator.hasNext()){
Sack sack = iterator.next();
if (sack.getType()== type){
count++;
}
}
return count;
}
// Accessor methods ...
public LinkedList280 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 lib280.list.LinkedList280;
enum Grain {
WHEAT, BARLEY, OATS, RYE, OTHER
}
public class Sack extends LinkedList280{
protected Grain type;
protected double weight;
/**
* @param type - the type of grain
* @param weight - how much grain in pounds
*/
public Sack(Grain 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 setType(Grain 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 setWeight(double weight){
this.weight = weight;
}
public String toString(){
return "("+ type.toString()+","+ weight +")";
}
}
CargoSimulator.java >>>>
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

3. Outline the four major approaches to informative speeches

Answered: 1 week ago

Question

4. Employ strategies to make your audience hungry for information

Answered: 1 week ago