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 } }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
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
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images