Cargo Ships This question involves cargo ships that carry a specific number of cargo containers. Each ship has a maximum number of cargo containers that it can hold and a current number of cargo containers that it is holding. You can see more details in the Ship class that is provided for you. Part A For part A, you will be completing the nextToUnload() method. This takes no input and returns a Ship from the ships instance variable that has the highest percentage of its cargo hold filled (as measured by current hold / capacity). If two ships have the same percentage, then it should return the first ship with that percentage. If all of the ships in the ships array are empty (ie. no cargo), the method should return null. Part B For part B, you will complete the two unloadShip methods. The first method takes a Ship object and unloads all of the cargo from that ship. The second method takes a Ship and an int and unloads the specified number of containers from the ship. If the number of containers is more than what is currently on the ship, it should only unload the number that are on the ship. In both cases, the methods should update the Ship object to set the currentHold to reflect the containers unloaded, then return the number of containers that were unloaded from the ship. public class Yard { private Ship[] ships; public Yard(Ship[] ships){ this.ships = ships; } /* * precondition: ships array * length is greater than 0 */ public Ship nextToUnload(){ /* Paste your code from Part A */ } public int unloadShip(Ship ship){ /* Paste your code from part B */ } public int unloadShip(Ship ship, int amount){ /* Paste your code from part B */ } }       public class Ship { private int currentHold; private int capacity; public Ship(int currentHold, int capacity){ this.currentHold = currentHold; this.capacity = capacity; } // Returns the number of containers currently // loaded on the ship public int getCurrentHold() { return currentHold; } /* * precondition: all ships will have a * currentHold >= 0 */ // Sets the number of containers currently // loaded on the ship public void setCurrentHold(int currentHold) { this.currentHold = currentHold; } /* * precondition: all ships will have a * capacity > 0 */ // Returns the total number of containers the // ship can hold public int getCapacity() { return capacity; } }     public class ShipTester { public static void main(String[] args) { // Add code if you would like to test your methods } }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter14: Introduction To Swing Components
Section: Chapter Questions
Problem 18RQ
icon
Related questions
Question

Cargo Ships

This question involves cargo ships that carry a specific number of cargo containers. Each ship has a maximum number of cargo containers that it can hold and a current number of cargo containers that it is holding. You can see more details in the Ship class that is provided for you.

Part A

For part A, you will be completing the nextToUnload() method. This takes no input and returns a Ship from the ships instance variable that has the highest percentage of its cargo hold filled (as measured by current hold / capacity). If two ships have the same percentage, then it should return the first ship with that percentage.

If all of the ships in the ships array are empty (ie. no cargo), the method should return null.

Part B

For part B, you will complete the two unloadShip methods. The first method takes a Ship object and unloads all of the cargo from that ship. The second method takes a Ship and an int and unloads the specified number of containers from the ship. If the number of containers is more than what is currently on the ship, it should only unload the number that are on the ship.

In both cases, the methods should update the Ship object to set the currentHold to reflect the containers unloaded, then return the number of containers that were unloaded from the ship.

public class Yard {

private Ship[] ships;

public Yard(Ship[] ships){
this.ships = ships;
}

/*
* precondition: ships array
* length is greater than 0
*/
public Ship nextToUnload(){
/* Paste your code from Part A */
}

public int unloadShip(Ship ship){
/* Paste your code from part B */
}

public int unloadShip(Ship ship, int amount){
/* Paste your code from part B */
}
}

 

 

 

public class Ship {

private int currentHold;
private int capacity;

public Ship(int currentHold, int capacity){
this.currentHold = currentHold;
this.capacity = capacity;
}

// Returns the number of containers currently
// loaded on the ship
public int getCurrentHold() {
return currentHold;
}

/*
* precondition: all ships will have a
* currentHold >= 0
*/
// Sets the number of containers currently
// loaded on the ship
public void setCurrentHold(int currentHold) {
this.currentHold = currentHold;
}

/*
* precondition: all ships will have a
* capacity > 0
*/
// Returns the total number of containers the
// ship can hold
public int getCapacity() {
return capacity;
}
}

 

 

public class ShipTester
{
public static void main(String[] args)
{

// Add code if you would like to test your methods

}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,