3/29/22, 1:43 PM https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/h…
https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/hw5%285%29.html 1/8
HOMEWORK – SPRING 2022
HOMEWORK 5 – due Tuesday, April 5th no later than 7:00pm
REMINDERS:● Be sure your code follows the coding style for CSE214.● Make sure you read the warnings about academicdishonesty. Remember, all work you submit for homeworkassignments MUST be entirely your own work. Also, groupefforts are not allowed.● Login to your grading account and click "SubmitAssignment" to upload and submit your assignment.● You are not allowed to use ArrayList, Vector or any other JavaAPI Data Structure classes to implement this assignmentexcept where noted.● You may use Scanner, InputStreamReader, or any other classthat you wish for keyboard input.
The friendly Mountain Dew and fedora enthusiasts of the Stony Brook BasementClub have decided to set up a massively multiplayer Nintendo 64 Donkey Kongleague. Because Wolfie Net is not particularly friendly to Nintendo 64s and theirassociated hardware (in this case consisting of raspberry pis with suspectly highnumber of wires sticking out), they have worked tirelessly over days and nightslaying down recycled headphone wire between the various quads on campus, butthey have realized that they need to create a network management software thatwill help them keep track of all the devices on their network (as they expect tohave many network failures and partial failures, especially whenever the lawnmower people run over their wires or careless students spill drinks on theirrouters). They will model their network as a tree with two types of nodes:Nintendo Nodes (these are always leaves, and represent one Donkey Kongplayer with their Nintendo64 device), and Raspberry Nodes (these representRaspberry Pis, which are used as routers in our network). They want to be able toview their network topology at any time, load and save it from a file (so they canoccasionally restart their computer), add nodes, remove subtrees (using the samecut-paste system as in HW 2 – so they can re-route their network in case araspberry router goes down. Finally, sometimes routers and Nintendo 64s go
3/29/22, 1:43 PM https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/h…
https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/hw5%285%29.html 2/8
down, so each device will have a boolean for broken. The final function will be toidentify the root of the minimal subtree that contains all the failures for a givenscenario (this will be extra credit). When we load the file, we will assume that allnodes are working, and no node can contain more than 9 children. Each line willcontain a position string, as well as a node name. If they are separated by a dash,the node is a Nintendo. The tree will be given and should be saved in preorder.
Note: In this homework you may not use ANY java API data structureclasses (this includes ArrayList, LinkedList, Vector,and any of the Treeclasses). You must build your own tree from the nodes (like we did in theLinkedList homework).
Here is a sample text file (format: position in tree, dash if nintendo, text).:
SBU1Central11Mendy111-Irving112-Grey113-LDS2Hill21Tabler211-Tosc212-TAC213-Hand22Roth3West31WestApartments32-GLS Center4-BasementClub
NOTE: All exceptions explicitly thrown in Required Classes except forIllegalArgumentException are custom exceptions that need to be made by you.
UML
The UML Diagram for all the classes specified below is as follows:
Required Classes
NetworkNodeWrite a fully documented class called NetworkNode which holds the type ofcomponent being represented, an array of children (null if this will be a Nintendo),and string for the text. Be sure to include all getters and setters. You may find ithelpful to write helper methods, especially to check input before adding a child tothe tree. Defining custom exceptions for actions like trying to add too manychildren to a node, or adding a child in an invalid position may also be desirable.
3/29/22, 1:43 PM https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/h…
https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/hw5%285%29.html 3/8
● private String name● private boolean isNintendo● private boolean isBroken //default is false● private NetworkNode parent● private NetworkNode[] children //Just like in hw 1, there should be noholes in the array.● final int maxChildren=9 //A full node exception may be desirable.
NetworkTreeWrite a fully documented class called NetworkTree which will serve as the treemanager for your NetworkTree. This must hold references into a tree (the rootand cursor), as well as be able to generate and save the tree to and from a file.Again, defining some custom exceptions may be helpful for code readability.
● private NetworkNode root● private NetworkNode cursor● public void cursorToRoot()
○ Sets the cursor to the root of the NetworkTree● public NetworkNode cutCursor()
○ Removes the child at the specified index of the NetworkTree, aswell as all of its children.○ Cursor goes to parent○ Cutting root clears the tree
● public void addChild(int index, NetworkNode node)○ Adds the given node to the corresponding index of the childrenarray.○ Should throw an exception if adding the node at the specifiedindex makes a hole in the array.
● public void cursorToChild(int index)○ Moves the cursor to the child node of the of the cursorcorresponding to the specified index.
● public void cursorToParent()○ Moves the cursor to the parent of the current node.
● public static NetworkTree readFromFile(String filename)○ Generates the NetworkTree based on the file name that ispassed in.
● public static void writeToFile(NetworkTree tree, String filename)○ Generates a text file that reflects the structure of theNetworkTree.○ The format of the tree of the file should match the format of theinput file.
● public void cursorToMinimalBrokenSubtree()//Extra credit○ The cursor should be on a node where all of the broken nodesare either the cursor or descendants.○ There may not be such a node in the subtree.
■ Accompanying method: public voidchangeCursorBrokenStatus()
NintendoNetworkWrite a fully-documented class named NintendoNetwork that takes in a text file,generates a NetworkTree and provides an interface for a user to manipulate thetree. Please see the UI required functions for the required functionality
● public static void main(String[] args)○ The main method runs a menu driven application which firstcreates a NetworkTree based on the passed in file and thenprompts the user for a menu command selecting the operation.The required information is then requested from the user based onthe selected operation. You can find the list of menu options in theUI required functions section.
3/29/22, 1:43 PM https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/h…
https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/hw5%285%29.html 4/8
● NetworkTree tree;
Note on File I/O:It is possible to read from a file using Scanner, which you should already befamiliar with. Here's an example:https://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java. To write to a file, you may use printwriter, with an example here:https://stackoverflow.com/questions/25298582/how-to-use-printwriter-to-create-and-write-to-a-file. Use nextLine() to read the next line of the file. Use standardstring manipulation techniques like charAt to get the index of the first non-numericcharacter.
Note on Exceptions: all exceptions should be handled gracefully – they shouldbe caught in the main, and the user should be notified by a nice printout. Yourmessages should clearly indicate what the problem is (bad index, full list, negativenumber, etc.). The program should continue to run normally after an exception isencountered. We will not be checking Input Mismatch cases.
Pretty Printing: Here is a tutorial on how to print tables neatly using printf in java.It is highly encouraged that you print the output neatly, as it makes grading mucheasier. https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
General RecommendationsYou can feel free to add extra methods and variables if you need.
UI Required Functions
● L- Load from file● P- Print● C- Cursor to child (index number)● A- Add child (index, type, prompt for text)● U- Cursor up (to parent)● X- Cut/Delete cursor● V- Paste Subtree (index number)● R- Cursor to root● S- Save to Text File● M-Cursor to root of minimal subtree containing all faults [extra credit]● B-Mark cursor as broken/fixed (flip the state) [extra credit, required forM]● Q – Quit
Note: please make sure that the menu is NOT case sensitive (so selecting Ashould be the same as selecting a).
Program Sample
Welcome to the Nintendo Network Manager.
Menu: L) Load from file P) Print tree C) Move cursor to a child node R) Move cursor to root U) Move cursor up to parent A) Add a child //if tree is empty, do not prompt for child indexnumber X) Remove/Cut Cursor and its subtree
3/29/22, 1:43 PM https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/h…
https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/hw5%285%29.html 5/8
V) Paste Cursor and its subtree S) Save to file M) Cursor to root of minimal subtree containing all faults B) Mark cursor as broken/fixed Q) Quit
Please select an option: LPlease enter filename: ashrubbery.txtashrubbery.txt not found.
Please select an option: LPlease enter filename: sbutopology.txtsbutopology.txt loaded
Please select an option: p
->SBU //cursor is automatically at root +Central +Mendy -Irving -Grey -LDS +Hill +Tabler -Tosc -TAC -Hand +Roth +West +WestApartments -GLS Center -BasementClub
Please select an option: cPlease enter an index: 2Cursor moved to Hill.
Please select an option: cPlease enter an index: 2Cursor moved to Roth.
Please select an option: aPlease enter an index: 1Please enter device name: HendrixIs this Nintendo (y/n): YNintendo added.
Please select an option: p
+SBU +Central +Mendy -Irving -Grey -LDS +Hill +Tabler -Tosc -TAC -Hand +Roth ->Hendrix +West +WestApartments
3/29/22, 1:43 PM https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/h…
https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/hw5%285%29.html 6/8
-GLS Center -BasementClub
Please select an option: uCursor moved to Roth.
Please select an option: XRoth cut, cursor is at Hill.
Please select an option: RCursor moved to SBU.
Please select an option: cPlease enter an index: 1Cursor moved to Central.
Please select an option: vPlease enter an index: 1Roth pasted as child of Central.
Please select an option: p
+SBU +Central ->Roth -Hendrix +Mendy -Irving -Grey -LDS +Hill +Tabler -Tosc -TAC -Hand
+West +WestApartments -GLS Center -BasementClub
Please select an option: SPlease enter a filename: theGivingTree.txt // A good way to test yourprogram is to load the saved fileFile saved.
Please select an option: QMake like a tree and leave!
//Extra Credit Example
Please select an option: p
+SBU +Central ->Roth -Hendrix +Mendy -Irving -Grey -LDS +Hill +Tabler
3/29/22, 1:43 PM https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/h…
https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/hw5%285%29.html 7/8
-Tosc -TAC -Hand
+West +WestApartments -GLS Center -BasementClub
Please select an option: bRoth marked as broken.
//after a few devices have been marked as broken:
Please select an option: p
+SBU +Central ->Roth ~Fault~ -Hendrix +Mendy -Irving ~Fault~ -Grey -LDS ~Fault~ +Hill +Tabler -Tosc -TAC -Hand
+West +WestApartments -GLS Center -BasementClub
Please select an option: MCursor moved to Central.
//after a few more devices have been marked as broken:
Please select an option: p
+SBU +Central ->Roth ~Fault~ -Hendrix +Mendy -Irving ~Fault~ -Grey -LDS ~Fault~ +Hill +Tabler -Tosc ~Fault~ -TAC -Hand
+West ~Fault~ +WestApartments -GLS Center -BasementClub
Please select an option: MCursor moved to SBU.
3/29/22, 1:43 PM https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/h…
https://blackboard.stonybrook.edu/bbcswebdav/pid-1703285-dt-content-rid-13101718_1/courses/1224-CSE-214-SEC01-50585/hw5%285%29.html 8/8
Course Info | Schedule | Sections | Announcements | Homework |Exams | Help/FAQ | Grades | HOME