Scenario Your client wants a family chart to be displayed which includes all the
ID: 3833931 • Letter: S
Question
Scenario Your client wants a family chart to be displayed which includes all the members of a family. Family details are given below and he want to do some processing on the data. The most appropriate data structure for showing the hierarchical relationship is Tree Represent a tree for the family chart given in Figure 1. Consider each node in the tree as a member of the family. Stage 3.1 Create the member class so that it has 1) a default constructor that initialises its attributes to sensible values. 2) a parameter constructor that sets all the attribute values based on the parameter values. 3) a setter method for each attribute. 4) a getter method for each attribute. 5) a toString method to return a suitably formatted string of the attribute values. Provide proper comments using Javadoc Stage 3.2 Using the DefaultMutableTreeNode where the user Object attribute refers to an riate nodes and link them to get the y member object, create a structure shown on Figurel. The y member details to be used are given on Table 1. Create a class called that makes use of recursion to list the family member as per their hierarchy in the console window, using tabulation to show the parent/cfiild relationships between nodes.Provide proper comments Stage 3.3 Create a class called DisplayTest002 that uses recursion to display all family members who lives in Dubai. Stage 3.4 Create a DefaultTreeModelobject, this tree model should contain the tree created in Stage 2. Add this tree model to a JTree and display the J Tree. Demonstrate this code with a class called Display est003Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class FamilyTree
{
private class Node
{
String value;
Node left, right;
Node(String val)
{
value = val;
left = null;
right = null;
}
Node(String val, Node leftChild, Node rightChild)
{
value = val;
left = leftChild;
right = rightChild;
}
}
private class BTreeDisplay extends JPanel
{
BTreeDisplay(Node tree)
{
setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
if (tree != null)
{
String value = String.valueOf(tree.value);
int pos = SwingConstants.CENTER;
JLabel rootLabel = new JLabel(value, pos);
add(rootLabel, BorderLayout.NORTH);
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(new BTreeDisplay(tree.left));
panel.add(new BTreeDisplay(tree.right));
add(panel);
}
}
}
private Node root = null;
public JPanel getView()
{
return new BTreeDisplay(root);
}
public boolean root(String x)
{
if (root == null){
root = new Node(x);
return true;}
else
return false;
}
public boolean addLeft(String p, String x)
{
Node parent = locate(p);
if (root == null ){
return false;}
else if (parent != null && parent.left == null){
parent.left = new Node(x);
return true;}
else
return false;
}
public boolean addRight(String p, String x)
{
Node parent = locate(p);
if (root == null ){
return false;}
else if (parent != null && parent.right == null){
parent.right = new Node(x);
return true;}
else
return false;
}
public Node locate(String p)
{
return locate(p, root);
}
private Node locate(String p, Node famTree)
{
Node result = null;
if (famTree == null)
return null;
if (famTree.value.equals(p))
return famTree;
if (famTree.left != null)
result = locate(p,famTree.left);
if (result == null)
result = locate(p,famTree.right);
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.