Okay so here's the assignment:
Problem Statement
You have been commissioned by the US Navy to develop a system for tracking the amount of fuel consumed by fleets of ships. Each ship has a name (ex: "Carrier"), fuel capacity (the maximum amount of fuel the ship can carry), and amount of fuel currently onboard. In this problem, fuel is measured in "units" and the capacity of each ship is an integer number (ex: The carrier's capacity is 125 fuel units). Each fleet has exactly four ships in it. When a fleet is deployed, each ship in the fleet is deployed. When a ship is deployed, it consumes half of the fuel it has onboard. When a fleet is refueled, each ship in the fleet is refueled. When a ship is refueled, it is totally filled up (its onboard amount equals its capacity).
Assignment
Your Fleet class need 4 methods:
A constructor that takes 4 Ships as parameters.
A method called deploy that will deploy each ship in the fleet.
A method called refuel that will refuel each ship in the fleet.
A method called printSummary that will print, for each ship, the ship's name and the number of fuel units that ship has consumed.
From reviewing the Driver, you can see that you will need a Ship class as well. The constructor of this class will take the ship's name and fuel capacity as parameters.
Infer from the Problem Statement what instance variable and methods you need in the Ship class.
Alright so what I have done so far:
public class Ship { private String name; private int fuelCapacity; private int fuelOnboard; /** * Constructor for objects of class ship */ public Ship(String inName, int inFuelCapacity) { name = inName; fuelCapacity = inFuelCapacity; } public int refuled() { fuelOnboard = fuelCapacity; } public int deploy() { fuelOnboard = fuelOnboard/2; } public String getName() { return name; } public int getFuelCapacity() { return fuelCapacity; } }
public class Fleet { private Ship ship1; private Ship ship2; private Ship ship3; private Ship ship4; /** * Constructor for objects of class Fleet */ public Fleet(Ship inShip1, Ship inShip2, Ship inShip3, Ship inShip4) { ship1 = inShip1; ship2 = inShip2; ship3 = inShip3; ship4 = inShip4; } public void deploy () { } public void reFuel() { } public void printSummary() { } }
and the Driver (which was provided):
/** * Driver for Outlab2. * * @author yaw * @version 22 Jan 2014 */ public class Driver { public static void main(String[] args) { //Creating 4 instances of Ship Ship ship1 = new Ship("Carrier", 150); Ship ship2 = new Ship("Anti-Submarine", 35); Ship ship3 = new Ship("Patrol", 22); Ship ship4 = new Ship("Destroyer", 83); //Creating instance of Fleet Fleet fleet1 = new Fleet(ship1, ship2, ship3, ship4); //Deploying the fleet twice fleet1.deploy(); fleet1.deploy(); //Refuel the fleet once fleet1.reFuel(); //Print summary fleet1.printSummary(); } }
As you can see I am having trouble with communicating between my Fleet class and my Ship class. Even if I am able to use my deploy method in my Ship class I would still be wondering how to use it on all the ships at once. Also, as you can see I made ship1, ship2... so forth of type Ship and I wasn't sure if this was correct or not.
Not sure if my Ship class is correct either. The reason I say that is when I look at the driver. The driver makes me think I should only have two return statements in my Ship class while the deploy method from Ship seems like it should be in Fleet. But the problem statement clearly states in my opinion that Ship should have a deploy and refuel method along with Fleet.
I just need some of your guy's advice on how to interpret the problem statement correctly. Is my ship class correct? If so, then what am I missing about calling the Ship method from the Fleet class?
--- Update ---
Will simply calling all four of them work like:
ship1.deploy();
ship2.deploy();
ship3.deploy();
ship4.deploy();