Hello!
It is necessary to implement a mechanism that would recreate the class BattleField, if in the class Ship the cycle was executed N times.
Please suggest the best and correct solution for this.
I expect from the program:
IF in Ship.java the loop was executed N times, THEN in GameInitialization.java to recreate an object of class BattleField and Fleet
//GameInitialization.java GameInitialization(){ BattleField humanBattleField = new BattleField(); Fleet humanFleet = new Fleet(humanBattleField); } //Fleet.java Fleet(BattleField battleField) { Ship[] fleet = new Ship[10]; this.battleField = battleField; for (int i = 0; i < fleet.length; i++) { if (0 == i) { fleet[i] = new Ship(4, i, battleField); //do actions } } } //Ship.java Ship(int shipLength, int shipNumber, BattleField battleField){ while (false == isShipPlacementCorrect) { while (false == isInitialCoordinatesCorrect) { //do actions } } }
Fleet, and checking the possibility of the installation will move as a method toBattleFieldor toFleet. Otherwise, the only option to inform the caller that the constructor was unable to throw an exception. But it is ugly, as well as serious work in the designer in general. It would be a little better to have a factory method that returned the ship if successful and threw an exception on failure, but still not very much. - zRrr