Question: Using DrJava create a program that does the following: You are given a file that contains data describing a binary tree whose nodes are strings.

Using DrJava create a program that does the following:

You are given a file that contains data describing a binary tree whose nodes are strings. The file begins with an integer N, the number of nodes in the tree, on a line by itself. This first line is followed by N additional lines where each line specifies child information for a node in the tree. The line for a node X starts with X itself followed by a list of the children of X. Names of nodes are separated by whitespace. For example, the data set

5

Al Bob Carol

Bob Debby Elaine

Carol

Debby

Elaine

represents a binary tree in which Al has two children named Bob and Carol; Bob has two children named Debby and Elaine; and Carol, Debby, and Elaine have no children. Write a program that reads in data from such a file and displays it in a JTree component.

This requires you to read in a file with the characteristics given in the text. You will have to read in this file and then display the tree by

Read in the first line of the file and create a binary tree.

Add nodes to the binary tree using the subsequent lines in the file.

Display your tree graphically using javax.swing

Thank you to all who take a look and try to help!

My code:

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; //implements the JBtree public class JBtree { //Declare variables private int nodesCount; private String dataInput[]; private JTree treeIns;

//creates the constructor public JBtree() { nodesCount=10; dataInput=new String[nodesCount]; treeIns=new JTree(); }

//Defines the FileDataRead() public void FileDataRead(String fileName) throws FileNotFoundException { //reads the FileInputStream() FileInputStream inputFile=new FileInputStream(fileName); InputStreamReader inputFileReader=new InputStreamReader(inputFile); BufferedReader inputFileReaderbuff=new BufferedReader(inputFileReader); String linesof; int nodeCounts=0; //try blocks try { while((linesof=inputFileReaderbuff.readLine())!=null) { if(linesof.matches("^-?\\d+$")) { nodesCount=Integer.parseInt(linesof); } else { dataInput[nodeCounts]=linesof; nodeCounts++; } } //files close inputFile.close(); //files close() inputFileReader.close(); inputFileReaderbuff.close(); } catch (IOException e1) {

e1.printStackTrace(); } }

//Defines the function populate tree public void populateTreeS() { //creates the node DefaultMutableTreeNode nodeRoot=new DefaultMutableTreeNode(); //for loop for(int it=0;it1) { //tree node root nodeRoot.add(new DefaultMutableTreeNode(ValueNode[0])); for(int jth=1;jth

//computesthe GUI public void createAndShowGUI() { //create the frame JFrame setFrames=new JFrame("JBtree Computation"); setFrames.add(treeIns); setFrames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setFrames.pack(); setFrames.setVisible(true); }

//Program starts with main public static void main(String[] args) throws FileNotFoundException { //Declares file input String nameFile="data.txt";

//Create the tree data JBtree treeDemo=new JBtree(); //checks the location and name of the file treeDemo.FileDataRead(nameFile); treeDemo.populateTreeS(); treeDemo.createAndShowGUI(); } }

Breaks when ran:

java.lang.NullPointerException at JBtree.populateTreeS(JBtree.java:68) at JBtree.main(JBtree.java:109) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

Data.txt:

10

Al Bob Carol

Bob Debby Elaine

Carol

Debby

Using DrJava create a program that does the following: You are given

a file that contains data describing a binary tree whose nodes are

Userslacobmcdermott Desktop NERIJB tree java C New open Save Close Cut copy Paste undo e Redo Find compile Reset Run Test Javadoc code coverage 89 90 //create the frame 91 JFrame setFrames new JFrame ("JBtree Computation 92 setFrames addCtreeIns) 93 set Frames ration(JFrame .EXIT ON CLOSE) setDefaultClose0pel 94 set Frames packOi set visible 95 set Frames true 96 97 98 //Program starts with main 99 public static void ma (StringO args) throws FileNotFoundException 100 10 Declares file input 102 String nameFile "data.txt 103 105 //Create the tree data 196 JBtree tree Demo-new JBtree(); 107 //checks the location and name of the file 108 tree Demo. FileDataReadCnameFile) 109 treeDemo populateTreeSC); 110 treeDemo.createAndShowGUIC) 112 113 14 Console Compiler Output Welcome to Dr Java Working directory s Users Jacobercdermott/Desktop/BINER run JBtree java.lang. NullPointerException at JBtree populateTreesCJBtree .java :68) at JBtree main J Btree. java:109) at sun reflect NativeMethodAccessorImpl nvoke (Native Method) at sun reflect NativeMethodAccessor Impl ,invokeCNativelMethodAccessorImpl.java:62) at sun reflect DelegatingMethodAccessor Impl nvokeCDelegatingMethodAccessor Impl .java:43) at java lang reflect ,Method ,invoke(Method.java :4980 at edu.rice.cs.drjava.model compiler. JavacCompiler, runCommandCJavacCompiler.java:267) Running main Method of Current Document 07:42

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!