Part 1: Cells Since these classes are all public classes, they should be in separate .java files, where each file is named after the class. Cell.java TODO: Instance Variables for Cell currRow stores...

I completed Part 1 of this Intro Java assignment, but am stuck on the last part: part 2. Similar to John Conway's Game of Life. I'm working on this java .class where PetriDish object contains a board which holds all the cells growing on the PetriDish.


Part 1: Cells Since these classes are all public classes, they should be in separate .java files, where each file is named after the class. Cell.java TODO: Instance Variables for Cell currRow stores the row value of the cell. currCol stores the column value of the cell. public abstract class Cell {...} public class CellStationary extends Cell {...} public class CellMoveUp extends Cell {...} public class CellDivide extends Cell {...} public class CellMoveToggle extends CellMoveUp {...} public class CellMoveDiagonal extends CellMoveUp {...} public class CellMoveToggleChild extends CellMoveToggle {...} public abstract class Cell {...} public abstract class Cell { ... public int currRow; public int currCol; public int mass; ... } mass stores the mass of the cell. Valid values for all the instance variables are ones that are not negative. TODO: Methods to Implement for Cell public Cell(int currRow, int currCol, int mass) This is the constructor for Cell . Initialize all instance variables with the values passed in as arguments. If an argument would make the appropriate instance variable invalid, set the appropriate instance variable to 0 instead. Even though Cell is an abstract class, we need this constructor because all subclasses should use this constructor to initialize the instance variables inherited from Cell . public Cell(Cell otherCell) This is the copy constructor for Cell . Initialize all instance variables with the instance variables of the Cell object passed in as an argument. Even though Cell is an abstract class, we need this copy constructor because all subclasses should use this copy constructor to copy the instance variables inherited from Cell . public void apoptosis() This method will be called on a Cell when apoptosis happens. Set currRow , currCol , and mass to -1 to indicate cell death. Getters getCurrRow() , getCurrCol() , getMass() For each getter method, return the current value of the appropriate instance variable. public abstract boolean checkApoptosis(List neighbors) Given a List of Cell s, determine if this cell should initiate apoptosis or not. Return true if the condition being checked for is satisfied and false otherwise (more details below). This is an abstract method that each concrete subclass will implement with its own behavior. public abstract class Cell { ... public Cell(int currRow, int currCol, int mass); public Cell(Cell otherCell); public void apoptosis(); public int getCurrRow(); public int getCurrCol(); public int getMass(); public abstract boolean checkApoptosis(List neighbors); ... } Subclasses of Cell Each of these 6 classes derive from Cell and will have similar (but not the exact same) behaviors. TODO: Methods to Implement for each Subclass They will all implement the following methods with at least the following functionality (more details about special functionality will be described afterward, you will have to modify these signatures to be appropriate per class): So each class will have two constructors (one taking the three int parameters in the order specified here and one being a copy constructor for the current class), override the toString() method, and override the checkApoptosis() method. You will need to adjust the method parameter types (specifically for the constructors) appropriately for each class. Note that even though some of these subclasses may have extra instance variables, the initial values of those are not passed into the constructor, so the first constructor should always only have those 3 parameters. Similarly, for a subclass with name Subclass , the copy constructor should take a parameter of type Subclass . public Subclass(int currRow, int currCol, int mass) This is the constructor for the subclass. Invoke the parent class's constructor to initialize all instance variables with the values passed in as arguments. public Subclass(Subclass otherSubclass) This is the copy constructor for the subclass. Invoke the parent class's copy constructor to initialize all instance variables with the instance variables of the otherSubclass object passed in as an argument. public class CellStationary extends Cell {...} public class CellMoveUp extends Cell {...} public class CellDivide extends Cell {...} public class CellMoveToggle extends CellMoveUp {...} public class CellMoveDiagonal extends CellMoveUp {...} public class CellMoveToggleChild extends CellMoveToggle {...} public class Subclass extends ParentClass { ... public Subclass(int currRow, int currCol, int mass); public Subclass(Subclass otherSubclass); public String toString(); public boolean checkApoptosis(List neighbors); ... } public String toString() Return the String representation of the current object. Each class will have a different representation but they will each be a single character. public boolean checkApoptosis(List neighbors) Return true or false based on neighbors depending on the conditions for apoptosis. Each class will have different conditions for apoptosis. Only non- null neighbors are included in this list. Note that this method does NOT call apoptosis() as this method is only for checking if apoptosis() should be called later. CellStationary Specifics CellStationary String representation "." . CellStationary conditions for checkApoptosis Checks for whether this cell has fewer than or equal to 7 and greater than or equal to 3 neighbors. CellMoveUp Specifics CellMoveUp String representation "^" . CellMoveUp conditions for checkApoptosis Checks for whether this cell does not have exactly 4 neighbors. CellDivide Specifics CellDivide direction instance variable CellDivide will have an extra instance variable, public int direction , that will represent the direction the cell will divide into. The variable is public for grading purposes. The (non-copy) constructor for this class should default direction to 1 . CellDivide String representation "+" . CellDivide conditions for checkApoptosis Checks for whether this cell has exactly 3 neighboring cells. CellMoveToggle Specifics CellMoveToggle toggled instance variable CellMoveToggle will have an extra instance variable, public boolean toggled , that will represent if the cell is currently "toggled" or not. It is made public for grading purposes. The (non-copy) constructor for this class should default toggled to true . CellMoveToggle String representation "T" if "toggled" and "t" otherwise. CellMoveToggle conditions for checkApoptosis Checks for whether this cell has fewer than 2 or greater than 5 neighbors. CellMoveDiagonal Specifics CellMoveDiagonal orientedRight and diagonalMoves instance variables CellMoveDiagonal will have two extra instance variables, public boolean orientedRight , which will represent if the cell is currently oriented to the right or not (to the left), and public int diagonalMoves , which will count the number of moves this cell has made (there will be more about "moving" in the PA8 portion). The (non-copy) constructor for this class should default orientedRight to true and diagonalMoves to 0 . CellMoveDiagonal String representation "/" if oriented right and "\" otherwise. CellMoveDiagonal conditions for checkApoptosis Checks for whether this cell has greater than 3 neighbors. CellMoveToggleChild Specifics CellMoveToggleChild numAlive static variable CellMoveToggleChild will have an extra static variable, public static int numAlive , that will represent the number of instances of this cell type that are currently alive (so it should be 0 when there are no instances of this cell). The constructor for this class should increment this value by 1 each time. This behavior should apply to both the normal constructor and the copy constructor. This class should also override the apoptosis() method to not only call its parent's apoptosis() method but to also decrement numAlive by 1 . CellMoveToggleChild String representation This class should not override CellMoveToggle 's toString() method. CellMoveToggleChild conditions for checkApoptosis Checks for whether CellMoveToggle 's conditions for apoptosis are satisfied AND there are fewer than 10 CellMoveToggleChild s alive. Part 2: PetriDish A PetriDish object contains a board that holds all the cells growing on the Petridish . TODO: Instance Variables for PetriDish dish represents the petri dish in the current iteration. A null value in the array represents an "empty space" and a non- null value represents an "alive" cell. The board is represented with the 0th row at the top and the 0th column at the left. public class PetriDish {...} public class PetriDish { ... public Cell[][] dish; ... } TODO: Methods to Implement for PetriDish public PetriDish(String[][] board) This is the constructor of the PetriDish class. board is a 2D array of Strings representing what cells the dish should be filled with. Each String is the string "null" if that position in the petri dish is an "empty space" and is of the format " {CELL_TYPE} {MASS}" for "alive cells." For example, the 2D String array could be created like this: String[][] petri = new String[][]{ {"CellMoveUp 0", "CellMoveToggle 1", "CellMoveToggleChild 2", "null"}, {"CellMoveDiagonal 3", "CellDivide 4", "CellMoveToggle 5", "null"} }; Populate each position of the instance variable dish with references to unique objects corresponding to the specific types and masses denoted in the 2D String array. Assume that board is always valid. public String toString() This can be used to print dish to visualize this PetriDish (and is optional). Below is an example implementation. public class PetriDish { ... public PetriDish(String[][] board); public String toString(); //optional ... } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(HORIZONTAL_BARS); //typically 2*board.length+3
Nov 25, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here