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

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

ID: 3826679 • Letter: U

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. 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;it<nodesCount;it++)
{
//splits the data input
String[] ValueNode=dataInput[it].split(" ");
//checks the length
if(ValueNode.length>1)
{
//tree node root
nodeRoot.add(new DefaultMutableTreeNode(ValueNode[0]));
for(int jth=1;jth<ValueNode.length;jth++)
{
((DefaultMutableTreeNode)nodeRoot.getChildAt(it)).add(new DefaultMutableTreeNode(ValueNode[jth]));;
}
}
else if(ValueNode.length==1)
{
nodeRoot.add(new DefaultMutableTreeNode(ValueNode[0]));
}
}
treeIns=new JTree(nodeRoot);
}

//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

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

Explanation / Answer

Ans

Please Add your data.txt to your project directory or give its correct path.

Add Swing componenet in it

and then add this program

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication6;

/**
*
* @author Rashmi Tiwari
*/
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;it<nodesCount;it++)
{
//splits the data input
String[] ValueNode=dataInput[it].split(" ");
//checks the length
if(ValueNode.length>1)
{
//tree node root
nodeRoot.add(new DefaultMutableTreeNode(ValueNode[0]));
for(int jth=1;jth<ValueNode.length;jth++)
{
((DefaultMutableTreeNode)nodeRoot.getChildAt(it)).add(new DefaultMutableTreeNode(ValueNode[jth]));;
}
}
else if(ValueNode.length==1)
{
nodeRoot.add(new DefaultMutableTreeNode(ValueNode[0]));
}
}
treeIns=new JTree(nodeRoot);
}
//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();
}
}

and run it it will show you binary tree .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote