Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This review is in continuation with Part I mentioned in query query.

This review is in continuation with Part I mentioned in query.

This review is in continuation with Part I mentioned in query.

added 2 characters in body
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
/* RunLengthEncoding.java */
package Project1;
/**
 * The RunLengthEncoding class defines an object that run-length encodes an
 * Ocean object. Descriptions of the methods you must implement appear below.
 * They include constructors of the form
 *
 * public RunLengthEncoding(int i, int j, int starveTime);
 * public RunLengthEncoding(int i, int j, int starveTime,
 * int[] runTypes, int[] runLengths) {
 * public RunLengthEncoding(Ocean ocean) {
 *
 * that create a run-length encoding of an Ocean having width i and height j,
 * in which sharks starve after starveTime timesteps.
 *
 * The first constructor creates a run-length encoding of an Ocean in which
 * every cell is empty. The second constructor creates a run-length encoding
 * for which the runs are provided as parameters. The third constructor
 * converts an Ocean object into a run-length encoding of that object.
 *
 * See the README file accompanying this project for additional details.
 */
 class RunLengthEncoding {
 
 /**
 * Define any variables associated with a RunLengthEncoding object here.
 * These variables MUST be private.
 */
 
 private DList2 list;
 private long sizeOfRun;
 private int width;
 private int height;
 private int starveTime;
 /**
 * The following methods are required for Part II.
 */
 
 /**
 * RunLengthEncoding() (with three parameters) is a constructor that creates
 * a run-length encoding of 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 RunLengthEncoding(int i, int j, int starveTime) {
 this.list = new DList2();
 this.list.insertFront(TypeAndSize.Species.EMPTY, i*j);
 this.sizeOfRun = 1;
 this.width = i;
 this.height = j;
 this.starveTime = starveTime;
 }
 
 /**
 * RunLengthEncoding() (with five parameters) is a constructor that creates
 * a run-length encoding of an ocean having width i and height j, in which
 * sharks starve after starveTime timesteps. The runs of the run-length
 * encoding are taken from two input arrays. Run i has length runLengths[i]
 * and species runTypes[i].
 * @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.
 * @param runTypes is an array that represents the species represented by
 * each run. Each element of runTypes is Ocean.EMPTY, Ocean.FISH,
 * or Ocean.SHARK. Any run of sharks is treated as a run of newborn
 * sharks (which are equivalent to sharks that have just eaten).
 * @param runLengths is an array that represents the length of each run.
 * The sum of all elements of the runLengths array should be i * j.
 */
 
 public RunLengthEncoding(int i, int j, int starveTime,
 TypeAndSize.Species[] runTypes, int[] runLengths) {
 this.list = new DList2();
 this.width = i;
 this.height = j;
 this.starveTime = starveTime;
 if(runTypes.length != runLengths.length){
 System.out.println("lengths are unequal");
 }else{
 for(int index=0; index < runTypes.length; index++){
 this.list.insertFront(runTypes[index], runLengths[index]);
 this.sizeOfRun++;
 }
 }
 }
 
 /**
 * restartRuns() and nextRun() are two methods that work together to return
 * all the runs in the run-length encoding, one by one. Each time
 * nextRun() is invoked, it returns a different run (represented as a
 * TypeAndSize object), until every run has been returned. The first time
 * nextRun() is invoked, it returns the first run in the encoding, which
 * contains cell (0, 0). After every run has been returned, nextRun()
 * returns null, which lets the calling program know that there are no more
 * runs in the encoding.
 *
 * The restartRuns() method resets the enumeration, so that nextRun() will
 * once again enumerate all the runs as if nextRun() were being invoked for
 * the first time.
 *
 * (Note: Don't worry about what might happen if nextRun() is interleaved
 * with addFish() or addShark(); it won't happen.)
 */
 
 /**
 * restartRuns() resets the enumeration as described above, so that
 * nextRun() will enumerate all the runs from the beginning.
 */
 
 publicprivate void restartRuns() {
 this.sizeOfRun = this.list.size;
 }
 
 /**
 * nextRun() returns the next run in the enumeration, as described above.
 * If the runs have been exhausted, it returns null. The return value is
 * a TypeAndSize object, which is nothing more than a way to return two
 * integers at once.
 * @return the next run in the enumeration, represented by a TypeAndSize
 * object.
 */
 
 publicprivate TypeAndSize nextRun() {
 TypeAndSize obj = null;
 if(this.sizeOfRun > 0){
 obj = this.list.nTh(this.sizeOfRun);
 this.sizeOfRun--;
 }
 return obj;
 }
 
 /**
 * toOcean() converts a run-length encoding of an ocean into an Ocean
 * object. You will need to implement the three-parameter addShark method
 * in the Ocean class for this method's use.
 * @return the Ocean represented by a run-length encoding.
 */
 
public Ocean toOcean() {
 Ocean sea = new Ocean(this.width, this.height);
 TypeAndSize obj = null;
 TypeAndSize singleArray[] = new TypeAndSize[this.width*this.height];
 int savedIndex = 0;
 
 /*Convert Doubly linked ist to 1d array */
 while((obj = nextRun()) != null){
 for(int index = savedIndex; index < (savedIndex + obj.runLength); index++){
 /* TypeAndSize class is just data storage but not an abstraction */
 singleArray[index].runLength = obj.runLength;
 singleArray[index].type = obj.type;
 }
 savedIndex += obj.runLength;
 }
 
 /* Convert 1d array to 2d array Ocean */
 for(int index =0; index < singleArray.length; index++){
 if(singleArray[index].type == TypeAndSize.Species.EMPTY){
 // Do nothing because ocean is created with empty objects.
 }else if(singleArray[index].type == TypeAndSize.Species.FISH){
 sea.addFish(index/this.width, Utility.mod(index, this.width));
 }else if(singleArray[index].type == TypeAndSize.Species.SHARK){
 sea.addShark(index/this.width, Utility.mod(index, this.width), 0);
 }
 }
 
 this.restartRuns();
 return sea;
 }
}
/* RunLengthEncoding.java */
package Project1;
/**
 * The RunLengthEncoding class defines an object that run-length encodes an
 * Ocean object. Descriptions of the methods you must implement appear below.
 * They include constructors of the form
 *
 * public RunLengthEncoding(int i, int j, int starveTime);
 * public RunLengthEncoding(int i, int j, int starveTime,
 * int[] runTypes, int[] runLengths) {
 * public RunLengthEncoding(Ocean ocean) {
 *
 * that create a run-length encoding of an Ocean having width i and height j,
 * in which sharks starve after starveTime timesteps.
 *
 * The first constructor creates a run-length encoding of an Ocean in which
 * every cell is empty. The second constructor creates a run-length encoding
 * for which the runs are provided as parameters. The third constructor
 * converts an Ocean object into a run-length encoding of that object.
 *
 * See the README file accompanying this project for additional details.
 */
 class RunLengthEncoding {
 
 /**
 * Define any variables associated with a RunLengthEncoding object here.
 * These variables MUST be private.
 */
 
 private DList2 list;
 private long sizeOfRun;
 private int width;
 private int height;
 private int starveTime;
 /**
 * The following methods are required for Part II.
 */
 
 /**
 * RunLengthEncoding() (with three parameters) is a constructor that creates
 * a run-length encoding of 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 RunLengthEncoding(int i, int j, int starveTime) {
 this.list = new DList2();
 this.list.insertFront(TypeAndSize.Species.EMPTY, i*j);
 this.sizeOfRun = 1;
 this.width = i;
 this.height = j;
 this.starveTime = starveTime;
 }
 
 /**
 * RunLengthEncoding() (with five parameters) is a constructor that creates
 * a run-length encoding of an ocean having width i and height j, in which
 * sharks starve after starveTime timesteps. The runs of the run-length
 * encoding are taken from two input arrays. Run i has length runLengths[i]
 * and species runTypes[i].
 * @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.
 * @param runTypes is an array that represents the species represented by
 * each run. Each element of runTypes is Ocean.EMPTY, Ocean.FISH,
 * or Ocean.SHARK. Any run of sharks is treated as a run of newborn
 * sharks (which are equivalent to sharks that have just eaten).
 * @param runLengths is an array that represents the length of each run.
 * The sum of all elements of the runLengths array should be i * j.
 */
 
 public RunLengthEncoding(int i, int j, int starveTime,
 TypeAndSize.Species[] runTypes, int[] runLengths) {
 this.list = new DList2();
 this.width = i;
 this.height = j;
 this.starveTime = starveTime;
 if(runTypes.length != runLengths.length){
 System.out.println("lengths are unequal");
 }else{
 for(int index=0; index < runTypes.length; index++){
 this.list.insertFront(runTypes[index], runLengths[index]);
 this.sizeOfRun++;
 }
 }
 }
 
 /**
 * restartRuns() and nextRun() are two methods that work together to return
 * all the runs in the run-length encoding, one by one. Each time
 * nextRun() is invoked, it returns a different run (represented as a
 * TypeAndSize object), until every run has been returned. The first time
 * nextRun() is invoked, it returns the first run in the encoding, which
 * contains cell (0, 0). After every run has been returned, nextRun()
 * returns null, which lets the calling program know that there are no more
 * runs in the encoding.
 *
 * The restartRuns() method resets the enumeration, so that nextRun() will
 * once again enumerate all the runs as if nextRun() were being invoked for
 * the first time.
 *
 * (Note: Don't worry about what might happen if nextRun() is interleaved
 * with addFish() or addShark(); it won't happen.)
 */
 
 /**
 * restartRuns() resets the enumeration as described above, so that
 * nextRun() will enumerate all the runs from the beginning.
 */
 
 public void restartRuns() {
 this.sizeOfRun = this.list.size;
 }
 
 /**
 * nextRun() returns the next run in the enumeration, as described above.
 * If the runs have been exhausted, it returns null. The return value is
 * a TypeAndSize object, which is nothing more than a way to return two
 * integers at once.
 * @return the next run in the enumeration, represented by a TypeAndSize
 * object.
 */
 
 public TypeAndSize nextRun() {
 TypeAndSize obj = null;
 if(this.sizeOfRun > 0){
 obj = this.list.nTh(this.sizeOfRun);
 this.sizeOfRun--;
 }
 return obj;
 }
 
 /**
 * toOcean() converts a run-length encoding of an ocean into an Ocean
 * object. You will need to implement the three-parameter addShark method
 * in the Ocean class for this method's use.
 * @return the Ocean represented by a run-length encoding.
 */
 
public Ocean toOcean() {
 Ocean sea = new Ocean(this.width, this.height);
 TypeAndSize obj = null;
 TypeAndSize singleArray[] = new TypeAndSize[this.width*this.height];
 int savedIndex = 0;
 
 /*Convert Doubly linked ist to 1d array */
 while((obj = nextRun()) != null){
 for(int index = savedIndex; index < (savedIndex + obj.runLength); index++){
 /* TypeAndSize class is just data storage but not an abstraction */
 singleArray[index].runLength = obj.runLength;
 singleArray[index].type = obj.type;
 }
 savedIndex += obj.runLength;
 }
 
 /* Convert 1d array to 2d array Ocean */
 for(int index =0; index < singleArray.length; index++){
 if(singleArray[index].type == TypeAndSize.Species.EMPTY){
 // Do nothing because ocean is created with empty objects.
 }else if(singleArray[index].type == TypeAndSize.Species.FISH){
 sea.addFish(index/this.width, Utility.mod(index, this.width));
 }else if(singleArray[index].type == TypeAndSize.Species.SHARK){
 sea.addShark(index/this.width, Utility.mod(index, this.width), 0);
 }
 }
 
 this.restartRuns();
 return sea;
 }
}
/* RunLengthEncoding.java */
package Project1;
/**
 * The RunLengthEncoding class defines an object that run-length encodes an
 * Ocean object. Descriptions of the methods you must implement appear below.
 * They include constructors of the form
 *
 * public RunLengthEncoding(int i, int j, int starveTime);
 * public RunLengthEncoding(int i, int j, int starveTime,
 * int[] runTypes, int[] runLengths) {
 * public RunLengthEncoding(Ocean ocean) {
 *
 * that create a run-length encoding of an Ocean having width i and height j,
 * in which sharks starve after starveTime timesteps.
 *
 * The first constructor creates a run-length encoding of an Ocean in which
 * every cell is empty. The second constructor creates a run-length encoding
 * for which the runs are provided as parameters. The third constructor
 * converts an Ocean object into a run-length encoding of that object.
 *
 * See the README file accompanying this project for additional details.
 */
 class RunLengthEncoding {
 
 /**
 * Define any variables associated with a RunLengthEncoding object here.
 * These variables MUST be private.
 */
 
 private DList2 list;
 private long sizeOfRun;
 private int width;
 private int height;
 private int starveTime;
 /**
 * The following methods are required for Part II.
 */
 
 /**
 * RunLengthEncoding() (with three parameters) is a constructor that creates
 * a run-length encoding of 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 RunLengthEncoding(int i, int j, int starveTime) {
 this.list = new DList2();
 this.list.insertFront(TypeAndSize.Species.EMPTY, i*j);
 this.sizeOfRun = 1;
 this.width = i;
 this.height = j;
 this.starveTime = starveTime;
 }
 
 /**
 * RunLengthEncoding() (with five parameters) is a constructor that creates
 * a run-length encoding of an ocean having width i and height j, in which
 * sharks starve after starveTime timesteps. The runs of the run-length
 * encoding are taken from two input arrays. Run i has length runLengths[i]
 * and species runTypes[i].
 * @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.
 * @param runTypes is an array that represents the species represented by
 * each run. Each element of runTypes is Ocean.EMPTY, Ocean.FISH,
 * or Ocean.SHARK. Any run of sharks is treated as a run of newborn
 * sharks (which are equivalent to sharks that have just eaten).
 * @param runLengths is an array that represents the length of each run.
 * The sum of all elements of the runLengths array should be i * j.
 */
 
 public RunLengthEncoding(int i, int j, int starveTime,
 TypeAndSize.Species[] runTypes, int[] runLengths) {
 this.list = new DList2();
 this.width = i;
 this.height = j;
 this.starveTime = starveTime;
 if(runTypes.length != runLengths.length){
 System.out.println("lengths are unequal");
 }else{
 for(int index=0; index < runTypes.length; index++){
 this.list.insertFront(runTypes[index], runLengths[index]);
 this.sizeOfRun++;
 }
 }
 }
 
 /**
 * restartRuns() and nextRun() are two methods that work together to return
 * all the runs in the run-length encoding, one by one. Each time
 * nextRun() is invoked, it returns a different run (represented as a
 * TypeAndSize object), until every run has been returned. The first time
 * nextRun() is invoked, it returns the first run in the encoding, which
 * contains cell (0, 0). After every run has been returned, nextRun()
 * returns null, which lets the calling program know that there are no more
 * runs in the encoding.
 *
 * The restartRuns() method resets the enumeration, so that nextRun() will
 * once again enumerate all the runs as if nextRun() were being invoked for
 * the first time.
 *
 * (Note: Don't worry about what might happen if nextRun() is interleaved
 * with addFish() or addShark(); it won't happen.)
 */
 
 /**
 * restartRuns() resets the enumeration as described above, so that
 * nextRun() will enumerate all the runs from the beginning.
 */
 
 private void restartRuns() {
 this.sizeOfRun = this.list.size;
 }
 
 /**
 * nextRun() returns the next run in the enumeration, as described above.
 * If the runs have been exhausted, it returns null. The return value is
 * a TypeAndSize object, which is nothing more than a way to return two
 * integers at once.
 * @return the next run in the enumeration, represented by a TypeAndSize
 * object.
 */
 
 private TypeAndSize nextRun() {
 TypeAndSize obj = null;
 if(this.sizeOfRun > 0){
 obj = this.list.nTh(this.sizeOfRun);
 this.sizeOfRun--;
 }
 return obj;
 }
 
 /**
 * toOcean() converts a run-length encoding of an ocean into an Ocean
 * object. You will need to implement the three-parameter addShark method
 * in the Ocean class for this method's use.
 * @return the Ocean represented by a run-length encoding.
 */
 
public Ocean toOcean() {
 Ocean sea = new Ocean(this.width, this.height);
 TypeAndSize obj = null;
 TypeAndSize singleArray[] = new TypeAndSize[this.width*this.height];
 int savedIndex = 0;
 
 /*Convert Doubly linked ist to 1d array */
 while((obj = nextRun()) != null){
 for(int index = savedIndex; index < (savedIndex + obj.runLength); index++){
 /* TypeAndSize class is just data storage but not an abstraction */
 singleArray[index].runLength = obj.runLength;
 singleArray[index].type = obj.type;
 }
 savedIndex += obj.runLength;
 }
 
 /* Convert 1d array to 2d array Ocean */
 for(int index =0; index < singleArray.length; index++){
 if(singleArray[index].type == TypeAndSize.Species.EMPTY){
 // Do nothing because ocean is created with empty objects.
 }else if(singleArray[index].type == TypeAndSize.Species.FISH){
 sea.addFish(index/this.width, Utility.mod(index, this.width));
 }else if(singleArray[index].type == TypeAndSize.Species.SHARK){
 sea.addShark(index/this.width, Utility.mod(index, this.width), 0);
 }
 }
 
 this.restartRuns();
 return sea;
 }
}
added 5372 characters in body
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
/* Ocean.java */
package Project1;
/**
 * The Ocean class defines an object that models an ocean full of sharks and
 * fish.
 * @author mohet01
 *
 */
class Ocean {
 /**
 * Define any variables associated with an Ocean object here. These
 * variables MUST be private.
 * 
 */
 //width of an Ocean
 private final int width;
 //height of an Ocean
 private final int height;
 /**
 * Below data member is the number of simulation time steps that a Shark
 * can live through without eating.
 */
 private static int starveTime;
 /**
 * Do not rename these constants. WARNING: if you change the numbers, you
 * will need to recompile Test4.java.
 * 
 */
 public final static int EMPTY = 0;
 public final static int SHARK = 1;
 public final static int FISH = 2;
 
 /*
 * This method provides the starvation time in the Ocean for sharks
 */
 public static int getStarvationTime(){
 return starveTime;
 }
 /*
 * This method provides the starvation time in the Ocean for sharks
 */
 public static void setStarvationTime(int starveTime){
 Ocean.starveTime = starveTime; 
 }
 
 
 /*
 * I preferred, 2d array of references to Critter objects
 * rather than List. Reasons(correct me),
 * 1) To display an array of ocean, it adds more logic in paint() method.
 * 2) Checking 8 nearest neighbors of each Critter looks inefficient,
 * For example: for an ocean of SEEFE 
 * FEEFE a 2x2 ocean, If i maintain
 * a list of Critter for this 2x2 ocean, i need to traverse 
 * S->E->E->F->E->F to get my first nearest neighbor of Shark,
 * In contrast, With 2d array, I would just use modulo operation as
 * mentioned in update() method. Let us see what happens!!!
 * 
 */
 private Critter[][] oceanMatrix;
 /**
 * Constructor that creates an empty ocean with below dimension
 * 
 * @param width
 * is the width of the ocean.
 * @param height
 * is the height of the ocean.
 * 
 */
 public Ocean(int width, int height){
 this.oceanMatrix = new Critter[height][width];
 this.width = width;
 this.height = height;
 for (int row = 0; row < height; row++) {
 for (int col = 0; col < width; col++) {
 oceanMatrix[row][col] = new Empty(row,col);
 }
 }
 }
 /**
 * This method adds Critter in an ocean.
 * @param object
 * is the Critter object to be added in Ocean.
 */
 public void addCritter(Critter object){
 Point p = object.getLocation();
 int x = p.getX();
 int y = p.getY();
 /*
 * I understand that, location property make sense to be be moved 
 * to corresponding Critter<type> class as it's property, which i did, But 
 * also captured location property of a Critter Object in Ocean class(with
 * above 3 lines of code) which is redundant and not relevant, But 2d array
 * is more efficient than list, for checking neighbor in update() method.
 * Are we Breaking SRS????
 * So, Instead of List am using 2d array. Let us see what happens!!!
 */
 oceanMatrix[x][y] = object;
 }
 /**
 * This method returns either Critter Object reference
 * 
 * @param x
 * is the x-coordinate of the cell whose contents are queried.
 * @param y
 * is the y-coordinate of the cell whose contents are queried.
 */
 public Critter cellContents(int x, int y) {
 return oceanMatrix[x][y];
 }
 /**
 * getWidth() returns the width of an ocean Object.
 * 
 * @return 
 * the width of the ocean.
 * 
 */
 public int getWidth() {
 return this.width;
 }
 /**
 * getHeight() returns the height of an Ocean object.
 * 
 * @return
 * the height of the Ocean.
 */
 public int getHeight() {
 return this.height;
 }
 /**
 * timeStep() performs a simulation time step as described in README.
 * 
 * @return
 * an ocean representing the elapse of one time Step.
 */
 public Ocean timeStep() {
 Ocean nextTimeStepSea = new Ocean(width, height);
 for (int row = 0; row < this.height; row++) {
 for (int col = 0; col < this.width; col++) {
 Critter creature = this.cellContents(row, col);
 nextTimeStepSea.addCritter(creature.update(this));
 }
 }
 return nextTimeStepSea;
 }
 
 /**
 * The following method is required for Part II.
 */
 /**
 * addShark() (with three parameters) places a shark in cell (x, y) if the
 * cell is empty. The shark's hunger is represented by the third parameter.
 * If the cell is already occupied, leave the cell as it is. You will need
 * this method to help convert run-length encodings to Oceans.
 * @param x is the x-coordinate of the cell to place a shark in.
 * @param y is the y-coordinate of the cell to place a shark in.
 * @param feeding is an integer that indicates the shark's hunger. You may
 * encode it any way you want; for instance, "feeding" may be the
 * last timestep the shark was fed, or the amount of time that has
 * passed since the shark was last fed, or the amount of time left
 * before the shark will starve. It's up to you, but be consistent.
 */
 public void addShark(int x, int y, int feeding) {
 if (this.cellContents(x, y).getClass().getName().equals("Empty")){
 this.addCritter(new Shark(x, y, feeding));
 }
 }

 /**
 * addFish() places a fish in cell (x, y) if the cell is empty. If the
 * cell is already occupied, leave the cell as it is.
 * @param x is the x-coordinate of the cell to place a fish in.
 * @param y is the y-coordinate of the cell to place a fish in.
 */
 public void addFish(int x, int y) {
 if (this.cellContents(x, y).getClass().getName().equals("Empty")){
 this.addCritter(new Fish(x, y));
 }
 }

}
/* Ocean.java */
package Project1;
/**
 * The Ocean class defines an object that models an ocean full of sharks and
 * fish.
 * @author mohet01
 *
 */
class Ocean {
 
 /**
 * The following method is required for Part II.
 */
 /**
 * addShark() (with three parameters) places a shark in cell (x, y) if the
 * cell is empty. The shark's hunger is represented by the third parameter.
 * If the cell is already occupied, leave the cell as it is. You will need
 * this method to help convert run-length encodings to Oceans.
 * @param x is the x-coordinate of the cell to place a shark in.
 * @param y is the y-coordinate of the cell to place a shark in.
 * @param feeding is an integer that indicates the shark's hunger. You may
 * encode it any way you want; for instance, "feeding" may be the
 * last timestep the shark was fed, or the amount of time that has
 * passed since the shark was last fed, or the amount of time left
 * before the shark will starve. It's up to you, but be consistent.
 */
 public void addShark(int x, int y, int feeding) {
 if (this.cellContents(x, y).getClass().getName().equals("Empty")){
 this.addCritter(new Shark(x, y, feeding));
 }
 }

 /**
 * addFish() places a fish in cell (x, y) if the cell is empty. If the
 * cell is already occupied, leave the cell as it is.
 * @param x is the x-coordinate of the cell to place a fish in.
 * @param y is the y-coordinate of the cell to place a fish in.
 */
 public void addFish(int x, int y) {
 if (this.cellContents(x, y).getClass().getName().equals("Empty")){
 this.addCritter(new Fish(x, y));
 }
 }
}
/* Ocean.java */
package Project1;
/**
 * The Ocean class defines an object that models an ocean full of sharks and
 * fish.
 * @author mohet01
 *
 */
class Ocean {
 /**
 * Define any variables associated with an Ocean object here. These
 * variables MUST be private.
 * 
 */
 //width of an Ocean
 private final int width;
 //height of an Ocean
 private final int height;
 /**
 * Below data member is the number of simulation time steps that a Shark
 * can live through without eating.
 */
 private static int starveTime;
 /**
 * Do not rename these constants. WARNING: if you change the numbers, you
 * will need to recompile Test4.java.
 * 
 */
 public final static int EMPTY = 0;
 public final static int SHARK = 1;
 public final static int FISH = 2;
 
 /*
 * This method provides the starvation time in the Ocean for sharks
 */
 public static int getStarvationTime(){
 return starveTime;
 }
 /*
 * This method provides the starvation time in the Ocean for sharks
 */
 public static void setStarvationTime(int starveTime){
 Ocean.starveTime = starveTime; 
 }
 
 
 /*
 * I preferred, 2d array of references to Critter objects
 * rather than List. Reasons(correct me),
 * 1) To display an array of ocean, it adds more logic in paint() method.
 * 2) Checking 8 nearest neighbors of each Critter looks inefficient,
 * For example: for an ocean of SEEFE 
 * FEEFE a 2x2 ocean, If i maintain
 * a list of Critter for this 2x2 ocean, i need to traverse 
 * S->E->E->F->E->F to get my first nearest neighbor of Shark,
 * In contrast, With 2d array, I would just use modulo operation as
 * mentioned in update() method. Let us see what happens!!!
 * 
 */
 private Critter[][] oceanMatrix;
 /**
 * Constructor that creates an empty ocean with below dimension
 * 
 * @param width
 * is the width of the ocean.
 * @param height
 * is the height of the ocean.
 * 
 */
 public Ocean(int width, int height){
 this.oceanMatrix = new Critter[height][width];
 this.width = width;
 this.height = height;
 for (int row = 0; row < height; row++) {
 for (int col = 0; col < width; col++) {
 oceanMatrix[row][col] = new Empty(row,col);
 }
 }
 }
 /**
 * This method adds Critter in an ocean.
 * @param object
 * is the Critter object to be added in Ocean.
 */
 public void addCritter(Critter object){
 Point p = object.getLocation();
 int x = p.getX();
 int y = p.getY();
 /*
 * I understand that, location property make sense to be be moved 
 * to corresponding Critter<type> class as it's property, which i did, But 
 * also captured location property of a Critter Object in Ocean class(with
 * above 3 lines of code) which is redundant and not relevant, But 2d array
 * is more efficient than list, for checking neighbor in update() method.
 * Are we Breaking SRS????
 * So, Instead of List am using 2d array. Let us see what happens!!!
 */
 oceanMatrix[x][y] = object;
 }
 /**
 * This method returns either Critter Object reference
 * 
 * @param x
 * is the x-coordinate of the cell whose contents are queried.
 * @param y
 * is the y-coordinate of the cell whose contents are queried.
 */
 public Critter cellContents(int x, int y) {
 return oceanMatrix[x][y];
 }
 /**
 * getWidth() returns the width of an ocean Object.
 * 
 * @return 
 * the width of the ocean.
 * 
 */
 public int getWidth() {
 return this.width;
 }
 /**
 * getHeight() returns the height of an Ocean object.
 * 
 * @return
 * the height of the Ocean.
 */
 public int getHeight() {
 return this.height;
 }
 /**
 * timeStep() performs a simulation time step as described in README.
 * 
 * @return
 * an ocean representing the elapse of one time Step.
 */
 public Ocean timeStep() {
 Ocean nextTimeStepSea = new Ocean(width, height);
 for (int row = 0; row < this.height; row++) {
 for (int col = 0; col < this.width; col++) {
 Critter creature = this.cellContents(row, col);
 nextTimeStepSea.addCritter(creature.update(this));
 }
 }
 return nextTimeStepSea;
 }
 
 /**
 * The following method is required for Part II.
 */
 /**
 * addShark() (with three parameters) places a shark in cell (x, y) if the
 * cell is empty. The shark's hunger is represented by the third parameter.
 * If the cell is already occupied, leave the cell as it is. You will need
 * this method to help convert run-length encodings to Oceans.
 * @param x is the x-coordinate of the cell to place a shark in.
 * @param y is the y-coordinate of the cell to place a shark in.
 * @param feeding is an integer that indicates the shark's hunger. You may
 * encode it any way you want; for instance, "feeding" may be the
 * last timestep the shark was fed, or the amount of time that has
 * passed since the shark was last fed, or the amount of time left
 * before the shark will starve. It's up to you, but be consistent.
 */
 public void addShark(int x, int y, int feeding) {
 if (this.cellContents(x, y).getClass().getName().equals("Empty")){
 this.addCritter(new Shark(x, y, feeding));
 }
 }
 /**
 * addFish() places a fish in cell (x, y) if the cell is empty. If the
 * cell is already occupied, leave the cell as it is.
 * @param x is the x-coordinate of the cell to place a fish in.
 * @param y is the y-coordinate of the cell to place a fish in.
 */
 public void addFish(int x, int y) {
 if (this.cellContents(x, y).getClass().getName().equals("Empty")){
 this.addCritter(new Fish(x, y));
 }
 }

}
added 1 character in body
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
Loading
added 977 characters in body
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
Loading
added 192 characters in body
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
Loading
deleted 3039 characters in body
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
Loading
deleted 200 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 4709 characters in body
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
Loading
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /