Question

****WRITTEN IN JAVA. MUST INCORPORATE A STACK OR QUEUE**** A file has genealogy data for a...

****WRITTEN IN JAVA. MUST INCORPORATE A STACK OR QUEUE****

A file has genealogy data for a collection of N people. The first line of the file contains the integer N followed by N additional lines of data. Each of these additional lines specifies a list of children for a single person. The line starts with the name of the person, followed by the number of that person's children, followed by the names of the children. Here is an example of a file specifying genealogy information for ten people.

10          
Al          3       Beth Carol Dino
Beth        1       Pablo
Carol       3       Ben Alex Mary
Dino        0       
Pablo       1       Angela Miguel
Ben         0
Alex        0
Mary        0
Angela      0
Miguel      0

For example, Al has three children named Beth, Carol, and Dino; Beth has one child named Pablo; and Dino has no children. You may assume that all names are unique.

Write a program which reads a file of genealogy information and then allows the user to enter pairs of names X and Y. The program then determines whether Y is a descendant of X, and if so, prints a list of names beginning with X and ending with Y, such that each person in the chain is a child of the person preceding them on the list. Otherwise, the program states that Y is not a descendant of X.

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

public class ReadDataFromFile {
   /**
   * @param args
   */
   public static void main(String[] args) {

       Scanner scanner = null;
       try {
           scanner = new Scanner(new File("data.txt"));
           String N = (String) scanner.next();
           // gets count from file
           System.out.println(N);
           while (scanner.hasNext()) {
               // read name
               String name = (String) scanner.next();
               // read count
               int childCount = Integer.parseInt(scanner.next());

               System.out.print(name + " " + childCount);
               // repeat reading child name upto childCount
               for (int i = 1; i <= childCount; i++)
                   System.out.print(" " + scanner.next());
               System.out.println();
           }
       } catch (Exception e) {
           // TODO: handle exception
       } finally {

           if (scanner != null)
               scanner.close();
       }
   }
}

OUTPUT:

10
Al 3 Beth Carol Dino
Beth 1 Pablo
Carol 3 Ben Alex Mary
Dino 0
Pablo 2 Angela Miguel
Ben 0
Alex 0
Mary 0
Angela 0
Miguel 0

Add a comment
Know the answer?
Add Answer to:
****WRITTEN IN JAVA. MUST INCORPORATE A STACK OR QUEUE**** A file has genealogy data for a...
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
  • Computer science organization and architecture! Help! Instructions The answers to questions below must be written in...

    Computer science organization and architecture! Help! Instructions The answers to questions below must be written in the file named you MUST replace which has been provided with this test. Important: login the FSUl of the filename with you actual FSU login ID. That file has lines that begin with Question N. where N corresponds to the question number. Below or beside each Question N. there are placeholder as described must be replaced with the correct answer in the specific formats,...

  • Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows...

    Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows in a graphical way, as shown below, the names stored in a file. The coordinates to print the names will be also read from another file. Here is an example of what your graphic output should look like with the two files that are provided for test purposes. Drawing Panel Adam Bernie Cameron Daniel Fanny Gerard Harold Issac Jackie Raitlyn Note that the colors...

  • This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named...

    This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named Budgeter.java. To use a Scanner for console input, you must import java.util.*; in your code. This program prompts a person for income and expense amounts, then calculates their net monthly income. Below are two example logs of execution from the program. This program’s behavior is dependent on the user input (user input is bold and underlined below to make it stand out and differentiate...

  • C++ Create an application that searches a file of male and female first names. A link...

    C++ Create an application that searches a file of male and female first names. A link to the file is provided on the class webpage. "FirstNames2015.txt" is a list of the most popular baby names in the United States and was provided by the Social Security Administration. Each line in the file contains a boy's name and a girl's name. The file is space-delimited, meaning that the space character is used to separate the boy name from the girl name....

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • The following are screen grabs of the provided files Thanks so much for your help, and have a n...

    The following are screen grabs of the provided files Thanks so much for your help, and have a nice day! My Java Programming Teacher Gave me this for practice before the exam, butI can't get it to work, and I need a working version to discuss with my teacher ASAP, and I would like to sleep at some point before the exam. Please Help TEST QUESTION 5: Tamagotchi For this question, you will write a number of classes that you...

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