Question

Write a program in Java that will accept input and display a person's family tree. This program n...

Write a program in Java that will accept input and display a person's family tree. This program needs to have a root person that branches off to parents and grandparents and be made so that you can add people.

1 0
Add a comment Improve this question Transcribed image text
Answer #1

This is the program in Java . I think, it would help you....

import javax.swing.*;

   import java.awt.*;

   import java.awt.event.*;

   import java.util.*;

/*

* @author      Amber Hisaw <[email protected]>

* @version     1.0

* @since       5-9-2015

* @purpose:    This class creates a binary tree that

*              holds strings and will add them to a

*              specified node.

*/

   public class FamilyTree

   {

      private class Node

      {

         String value;     // Value stored in node

         Node left, right; // Left and right child     

       

      /**

      Constructor for leaf nodes.

      @param val The value to store in the node.

      */

         Node(String val)

         {

            value = val;

            left = null;

            right = null;

         }  

       

      /**

      Constructor for non-leaf nodes.

      @param val The value to initialize the node.

      @param leftChild The link to the left child.

      @param rightChild The link to the right child.

      */

         Node(String val, Node leftChild, Node rightChild)

         {

            value = val;

            left = leftChild;

            right = rightChild;

         }

        }

    

   /**

    The BTreeDisplay class graphically displays trees

   in a JPanel. The JPanel is recursively partitioned

   into a top part dislaying the root, and two lower

   parts displaying the left and right subtrees.

   */

      private class BTreeDisplay extends JPanel

      {

      /**

      Constructor.

      @param tree The root of the binary

      tree to display.

      */

       

         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;   // Root of binary tree

    

    

   /**

   The getView method creates and returns a

   a graphical view of the binary tree.

   @return A panel that displays a view of the tree.

   */

      public JPanel getView()

      {

         return new BTreeDisplay(root);      

      }   

    

   /**

   The public root method adds a value to the

   tree by calling a private root method and

   passing it the root of the tree.

   @param x The value to make root.

   @return true.    

   */

      public boolean root(String x)

      {

         if (root == null){

            root = new Node(x);

            return true;}

         else

            return false;

      }

    

   /**

   The public addLeft method adds a value to the

   tree by locating the desired parent and ensuring

   all requirements are met.

   @param p The desired parent. x The value to add to the tree.

   @return true.    

   */

      public boolean addLeft(String p, String x)

      {

         Node parent = locate(p);

      //Check if root is established.

         if (root == null ){

            return false;}

         //Locate desired parent and checks if child exists.

         else if (parent != null && parent.left == null){

         //Adds node

            parent.left = new Node(x);

            return true;}

         else

            return false;

      }

    

   /**

   The public addRight method adds a value to the

   tree by locating the desired parent and ensuring

   all requirements are met.

   @param p The desired parent. x The value to add to the tree.

   @return true.    

   */

      public boolean addRight(String p, String x)

      {

         Node parent = locate(p);

      //Check if root is established.

         if (root == null ){

            return false;}

         //Locate desired parent and checks if child exists.

         else if (parent != null && parent.right == null){

         //Adds node

            parent.right = new Node(x);

            return true;}

         else

            return false;

      }     

    

      public Node locate(String p)

      {

      // Call the private recursive method

         return locate(p, root);

      }

    

   /**

   The method contains checks whether an

   item is in a binary search tree.

   @param x The item to check for.

   @param famTree The binary tree to look in.

   @return true if found, false otherwise.

   */

      private Node locate(String p, Node famTree)

      {

        //new Node result, set to null.

         Node result = null;

            if (famTree == null)

            return null;

        //if passed node contains value, return it.     

            if (famTree.value.equals(p))

            return famTree;

            //if left child not null, recursively call locate with left child.

         if (famTree.left != null)

            result = locate(p,famTree.left);

            //if parent still not found, recursively call right locate passing the right child.

         if (result == null)

            result = locate(p,famTree.right);

         return result;

      }

    

   }

Please like my answer and show some love and support.

Thank you.

> you stole this off the internet

George Calvert Thu, Dec 2, 2021 12:06 PM

Add a comment
Know the answer?
Add Answer to:
Write a program in Java that will accept input and display a person's family tree. This program n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In BlueJ using Java, write a program that can input and display a person’s family tree....

    In BlueJ using Java, write a program that can input and display a person’s family tree. This project requires a GUI class that allows the user to input the members of the family tree and print the family tree.

  • Write a program that takes as input a fully parenthesized , arithmetic expression and convert it...

    Write a program that takes as input a fully parenthesized , arithmetic expression and convert it to a binary expression tree. Your program should display the tree in some way and also print the value associated with the root. LANGUAGE In java Please answer...

  • JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work...

    JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work with “big” numbers.) Create a completely-skewed BST S containing 1, 2, . . . , x. Create a BST R containing x integers without repetitions gen- erated at random. (To minimize the risk of repetitions, you can multiply the value returned by random() by a big number.) Given that the numbers are generated uniformly at random, the tree will likely be balanced. Measure the...

  • *Java* You will write a program to do the following: 1. Read an input file with...

    *Java* You will write a program to do the following: 1. Read an input file with a single line of text. Create a method named load_data. 2. Determine the frequency distribution of every symbol in the line of text. Create a method named 3. Construct the Huffman tree. 4. Create a mapping between every symbol and its corresponding Huffman code. 5. Encode the line of text using the corresponding code for every symbol. 6. Display the results on the screen....

  • Need help on following Java GUI problem: Write a program that lets a user display and...

    Need help on following Java GUI problem: Write a program that lets a user display and modify pictures. Create a window. Add four buttons so that clicking a particular button will shift the image by a small amount in the north, south, east or west direction inside the window. Add a menu bar with two menus: File and Image. The File menu should contain an Open menu item that the user can select to display JPEG and PNG files from...

  • Write a program that will read a line of text as input and then display the...

    Write a program that will read a line of text as input and then display the line with the first work moved to the end of the line. For example I love Java

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • Write a program to transform a free tree to a rooted tree. The free tree and...

    Write a program to transform a free tree to a rooted tree. The free tree and the root are input by the user, and for the rooted tree, you output it level by level. (Hint: Use Adjacency Matrix or Adjacency Lists to store the tree, and use a queue to implement the transformation.)

  • Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...

    Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format The following operators are required to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT