Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

--------------------------------------------------------------------------------

ID: 3726915 • Letter: #

Question

-----------------------------------------------------------------------------------------

Phase 1 code :

abstract class Tile
{
//instance variable
String symbol;
boolean passable;
//constructor for initializing instance variables
public Tile(String symbol,boolean passable)
{
this.symbol=symbol;
this.passable=passable;
}
public String getSymbol()
{
return this.symbol;
}
public boolean isPassable()
{
return this.passable;
}
}
class Wall extends Tile
{
public Wall()
{
super("#",false); //calls super class for initializing symbol and boolean value
}
}
class OpenSpace extends Tile
{
public OpenSpace()
{
super(".",true); //calls super class for initializing symbol and boolean value
}
}

-------------------------------------------------------------------------------------------

public class TestPhase2 {

public static void main(String[] args) {
  
Tile[][] board = {
{new Wall(), new Wall(), new Wall(), new Wall()},
{new Wall(), new OpenSpace(new Trap()), new OpenSpace(new Amulet()), new Wall()},
{new Wall(), new OpenSpace(new Trap()), new OpenSpace(new HealthPotion()), new Wall()},
{new Wall(), new Wall(), new Wall(), new Wall()},
};
  
for (Tile[] row : board) {
for (Tile t: row) {
System.out.print(t.getSymbol());
}
System.out.println();
}

}

}

Phase 2: Content Next, implement the abstract Content class, which represents items that are sitting on the Tiles The Content class should have One instance variable: a String that represents the symbol that will be drawn on the map A constructor that accepts only one parameter (the String) A String getSymbol()method that returns the symbol for this item. . . . Implement an abstract subclass of Contents named Item. Subclasses of Item will be objects in the game that the player can touch to interact with. Item should have A constructor with only a String parameter specifying the symbol An int getEffect() method that returns how much of the player's health ("hit points") are added, or removed by touching this item. The default value to return is 0 . · Now write three subclasses of Item Amulet : which is marked by the character "Y" on the map. This is the "Amulet of Yendor". Picking up this item will result in winning the game. This item has no effect on hit points. This will be implemented later. In addition to returning a 0, the getEffect () method should also write "You picked up the Amulet of Yendor!" to the console HealthPotion Represented by an "h" on the map, a health potion has the effect of adding 5 hit points (health) to the player. In addition to returning a 5, the getEffect) method should also write "You picked up a health potion!" to the console Trap: Represented by a “^" on the map, the player will lose 5 hit points when touching this item. In addition to returning a -5, the getEffect() method should also write "You set off a trap!" to the console · · These three classes only contain a no-parameters constructor, and a getEffect() method Now make the following modifications to class Tile and its subclasses: Add an instance variable of type Content to Tile. when there is no content to a tile, this variable should be null · Add methods removeContent(), which will set your Content instance variable to null, and a getContent) method which will return the contents of this tile, and a setContent(Content) methood which will set the content of this tile Change the getSymbol() method to return the symbol of the content, if there is a content in the tile Otherwise, return the symbol of the tile itself Add a second contructor to the Tile and OpenSpace classes, that accepts an additional parameter of type Content. The signature should be: public Tile(String, boolean, Content). The Wall class should not have a constructor of this kind, since a wall cannot have any content. * ·

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

/**

* Modified Tile.java class

*/

abstract class Tile {

                // instance variable

                String symbol;

                boolean passable;

                Content content;

                // constructor for initializing instance variables

                public Tile(String symbol, boolean passable) {

                                this.symbol = symbol;

                                this.passable = passable;

                                content = null;

                }

                /**

                * Newly added constructor to allow a Content also, in addition to symbol

                * and passable flag

                */

                public Tile(String symbol, boolean passable, Content content) {

                                this.symbol = symbol;

                                this.passable = passable;

                                this.content = content;

                }

                public String getSymbol() {

                                if (content != null) {

                                                return content.getSymbol();

                                }

                                return this.symbol;

                }

                public boolean isPassable() {

                                return this.passable;

                }

                public void removeContent() {

                                content = null;

                }

                public Content getContent() {

                                return content;

                }

                public void setContent(Content content) {

                                this.content = content;

                }

}

class Wall extends Tile {

                public Wall() {

                                super("#", false); // calls super class for initializing symbol and

                                                                                                                // boolean value

                }

}

class OpenSpace extends Tile {

                /**

                * Newly added constructor to allow passing a Content object also

                */

                public OpenSpace(Content content) {

                                super(".", true, content);

                }

                public OpenSpace() {

                                super(".", true); // calls super class for initializing symbol and

                                                                                                                // boolean value

                }

}

/**

* Content.java (abstract)

*/

public abstract class Content {

                private String symbol;

                /**

                * Constructor with one attribute

                * @param symbol - symbol of the content

                */

                public Content(String symbol) {

                                this.symbol = symbol;

                }

                //getter method

                public String getSymbol() {

                                return symbol;

                }

               

}

/**

* Item.java (abstract). the Amulet, HealthPotion and Trap classes are within

* this java file

*/

public abstract class Item extends Content {

                public Item(String symbol) {

                                super(symbol);// passing symbol to super class

                }

                /**

                * returns the health effect

                *

                * @return 0 by default

                */

                public int getEffect() {

                                return 0;

                }

}

class Amulet extends Item {

                public Amulet() {

                                super("Y"); // passing "Y" symbol to the super class

                }

                @Override

                public int getEffect() {

                                System.out.println("You picked up the Amulet of Yendor!");

                                // no health effect

                                return 0;

                }

}

class HealthPotion extends Item {

                public HealthPotion() {

                                super("h");// passing "h" symbol to the super class

                }

                @Override

                public int getEffect() {

                                System.out.println("You picked up a health potion!");

                                // gain 5 health points

                                return 5;

                }

}

class Trap extends Item {

                public Trap() {

                                super("^");// passing "^" symbol to the super class

                }

                @Override

                public int getEffect() {

                                System.out.println("You set off a trap!");

                                // lose 5 health points

                                return -5;

                }

}

/**

* TestPhase2.java (not modified)

*/

public class TestPhase2 {

                public static void main(String[] args) {

                                Tile[][] board = {

                                                                { new Wall(), new Wall(), new Wall(), new Wall() },

                                                                { new Wall(), new OpenSpace(new Trap()),

                                                                                                new OpenSpace(new Amulet()), new Wall() },

                                                                { new Wall(), new OpenSpace(new Trap()),

                                                                                                new OpenSpace(new HealthPotion()), new Wall() },

                                                                { new Wall(), new Wall(), new Wall(), new Wall() }, };

                                for (Tile[] row : board) {

                                                for (Tile t : row) {

                                                                System.out.print(t.getSymbol());

                                                }

                                                System.out.println();

                                }

                }

}

/*OUTPUT*/

####

#^Y#

#^h#

####