patternjavaModerate
Simulation of an ocean containing sharks and fish
Viewed 0 times
containingsimulationsharksfishandocean
Problem
As part of my Java learning, I tried solving Part I of problem description here. The only issue that I see now is that I could not close the button of
Please review and provide your comments on coding style/OOPS aspects.
Ocean.java
```
/ Ocean.java /
/**
* The Ocean class defines an object that models an ocean full of sharks and
* fish. Descriptions of the methods you must implements appear below. They
* include a constructor of the form
*
* public Ocean(int i, int j, int starveTime);
*
* that creates an empty ocean having width i and height j, in which sharks
* starve after starveTime timesteps.
*
* See the README file accompanying this project for additional details.
*
* @author mohet01
*
*/
public class Ocean {
/**
* Do not rename these constants. WARNING: if you change the numbers, you
* will need to recompile Test4.java. Failure to do so will give you a very
* hard-to-find bug.
*/
public final static int EMPTY = 1;
public final static int SHARK = 2;
public final static int FISH = 3;
/**
* Define any variables associated with an Ocean object here. These
* variables MUST be private.
*
*/
private final static int UNKNOWN = -1; // for unknown return type
private int width;
private int height;
private int[][] oceanMatrix;
private int[][] sharkHungerLevelMatrix; // need to think on optimization
private int starveTime;
/**
* The following methods are required for Part I.
*
*/
/**
* Ocean() is a constructor that creates an empty ocean having width i and
* height j, in which sharks starve after starveTime timesteps.
*
* @param i
* is the width of the ocean.
* @param j
* is the height of the ocean.
* @param starveTime
* is the number of timeSteps sharks survive without food.
*/
public Ocean(int i, int j, int starveTime) {
this.
Frame. Please review and provide your comments on coding style/OOPS aspects.
Ocean.java
```
/ Ocean.java /
/**
* The Ocean class defines an object that models an ocean full of sharks and
* fish. Descriptions of the methods you must implements appear below. They
* include a constructor of the form
*
* public Ocean(int i, int j, int starveTime);
*
* that creates an empty ocean having width i and height j, in which sharks
* starve after starveTime timesteps.
*
* See the README file accompanying this project for additional details.
*
* @author mohet01
*
*/
public class Ocean {
/**
* Do not rename these constants. WARNING: if you change the numbers, you
* will need to recompile Test4.java. Failure to do so will give you a very
* hard-to-find bug.
*/
public final static int EMPTY = 1;
public final static int SHARK = 2;
public final static int FISH = 3;
/**
* Define any variables associated with an Ocean object here. These
* variables MUST be private.
*
*/
private final static int UNKNOWN = -1; // for unknown return type
private int width;
private int height;
private int[][] oceanMatrix;
private int[][] sharkHungerLevelMatrix; // need to think on optimization
private int starveTime;
/**
* The following methods are required for Part I.
*
*/
/**
* Ocean() is a constructor that creates an empty ocean having width i and
* height j, in which sharks starve after starveTime timesteps.
*
* @param i
* is the width of the ocean.
* @param j
* is the height of the ocean.
* @param starveTime
* is the number of timeSteps sharks survive without food.
*/
public Ocean(int i, int j, int starveTime) {
this.
Solution
Just a quick comment: Your
I'd be expecting a
Methods like
Ocean class knows/does too many things.I'd be expecting a
Fish class and a Shark class. Creating an ocean requires a starveTime constructor parameter? That's a sign you've broken the single responsibility principle (SRP).Methods like
void addFish(int x, int y) would be void addFish(Fish fish) - let the fish know where he is in the ocean, and let the ocean know it has fish.Context
StackExchange Code Review Q#43476, answer score: 19
Revisions (0)
No revisions yet.