Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you please help me to implement this doesPathExist() method in Java assignment? Basically, this method takes in a list of Rooms (constructed from .txt
Can you please help me to implement this doesPathExist() method in Java assignment? Basically, this method takes in a list of Rooms (constructed from .txt file) , as well as names of the start and end room. It should return true if,from the start room , you can repeatedly move to connected neighbouring room until you reach the end room , or false if that is not possible . Please don't just copy and paste code from another users that already posted this problem already.
Note: Every room is reachable from every other room on the same floor but you will notice there is no way to move between floors. If you ask whether a path exists between a ground floor room and a basement room, the answer should be false.
Here are my Room.java, RoomNavigator.java and .txt files.
RoomNavigator.java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class RoomNavigator { /** * Returns true if and only if a path exists between the start room and the end room, based on the description of * rooms provided by the allRooms list. The start and end rooms are specified by their names. */ public boolean doesPathExist(ArrayListallRooms, String startRoom, String endRoom) { // TODO: Implement. Step 1: create and populate your Room map with everything from the rooms list. // TODO: Implement. Step 2: create your breadth-first search queue and start it off with the initial room. // TODO: Implement. Step 3: in a loop, repeatedly process the next Room in the queue. return false; } /** Program execution begins here. */ public static void main(String[] args) { ArrayList allRooms = readRooms(); RoomNavigator navigator = new RoomNavigator(); // Paths between rooms that are on the same floor (expecting 'true'): lookForPath(navigator, allRooms, "Larder", "Gardens"); lookForPath(navigator, allRooms, "Upper Landing", "Attic"); lookForPath(navigator, allRooms, "Chapel", "Wine Cellar"); // Paths between rooms that are on different floors (expecting 'false'): lookForPath(navigator, allRooms, "Patio", "Game Room"); lookForPath(navigator, allRooms, "Tower", "Crypt"); lookForPath(navigator, allRooms, "Underground Lake", "Library"); } /** * Reads the "layout.txt" file and creates a list of Room objects with their connections set up accordingly. You * should not modify this method in any way. */ private static ArrayList readRooms() { ArrayList rooms = new ArrayList(); try { BufferedReader bufferedReader = new BufferedReader(new FileReader("layout.txt")); String line; while ((line = bufferedReader.readLine()) != null) { if (line.strip().isEmpty()) { continue; } int splitIndex = line.indexOf(';'); if (splitIndex allRooms, String startRoom, String endRoom) { System.out.printf( "Path from '%s' to '%s': %s%n", startRoom, endRoom, navigator.doesPathExist(allRooms, startRoom, endRoom)); } }
Room.java file
import java.util.ArrayList; /** * Represents a single room in the RoomNavigator program. Each room keeps track of a name, as well as a list of the * names of the other rooms it's connected to. */ public class Room { private String name; private ArrayListconnectedRooms; public Room(String name) { this.name = name; this.connectedRooms = new ArrayList(); } public String getName() { return name; } public ArrayList getConnectedRooms() { return new ArrayList(connectedRooms); } public void addConnectedRoom(String roomName) { connectedRooms.add(roomName); } }
.txt file Entrance Hall; Foyer Foyer; Entrance Hall, Library, Dining Hall, Conservatory Conservatory; Foyer, Graveyard, Gardens Graveyard; Conservatory Gardens; Conservatory Library; Foyer, Storeroom Storeroom; Library, Larder Larder; Storeroom, Kitchen Kitchen; Larder, Charred Room, Dining Hall Charred Room; Kitchen Dining Hall; Kitchen, Patio, Foyer, Ballroom Ballroom; Dining Hall Patio; Dining Hall Upper Landing; Dusty Hallway Dusty Hallway; Upper Landing, Game Room, Collapsed Room, Bedroom Game Room; Dusty Hallway Collapsed Room; Dusty Hallway Bedroom; Dusty Hallway, Junk Room, Balcony, Creaky Hallway Balcony; Bedroom Junk Room; Bedroom, Abandoned Room Creaky Hallway; Bedroom, Abandoned Room, Chasm Chasm; Creaky Hallway, Tower Tower; Chasm Abandoned Room; Creaky Hallway, Junk Room, Attic Attic; Abandoned Room Basement Landing; Catacombs Catacombs; Basement Landing, Chapel, Crypt, Statuary Corridor Chapel; Catacombs Crypt; Catacombs Statuary Corridor; Catacombs, Underground Lake, Furnace Room, Gallery Underground Lake; Statuary Corridor Furnace Room; Statuary Corridor Gallery; Statuary Corridor, Research Laboratory, Vault Research Laboratory; Gallery Vault; Gallery, Wine Cellar Wine Cellar; Vault
Room lay out in a diagram:
Ground Floor Charred Room Patio Larder Kitchen Dining Hall Ballroom Storeroom Library Foyer Conservatory Graveyard Entrance Hall Gardens Upper Floor Attic Abandoned Room Creaky Hallway Chasm Tower Junk Room Bedroom Balcony Collapsed Room Dusty Hallway Game Room Upper Landing Basement Wine Cellar Vault Gallery Research Laboratory Underground Lake Statuary Corridor Furnace Room Crypt Catacombs Chapel Basement Landing
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started