Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Chapter 10, page 699-700, Programming Project #3, Family database program: plus add a Family Tree Class. First complete this Chapter 10 ArrayList and then upgrade

Chapter 10, page 699-700, Programming Project #3, Family database program: plus add a Family Tree Class. First complete this Chapter 10 ArrayList and then upgrade into a FamilyTree Class as described below. I repeat: First complete Chapter 10 Project #3, 99% of the student questions are from those who skip that step!!! I've included the Person Class file from the BJP text author to give you a good start: Person.javaPreview the documentView in a new window After the Chapter 10 run displays as shown here: Capture1.JPGView in a new window These data come from the file names.txtPreview the documentView in a new window which is the exact same list of names provided by our text author back in Chapter 10 (tudor.dat), but modified for this assignment. In the above screen capture, please notice a few things. Note the upper output is identical to what's in the text, then at the bottom, I display the same data as a printSideways Tree. I also note that one of the children is Mary I, so I can enter that name too, and get the following family tree information: Capture2.JPGView in a new window The main() test program from the text author I've changed to FamilyTest.javaPreview the documentView in a new window which you cannot alter, as that's what I'll use to test your FamilyInfo and FamilyTree classes. The person names and family information are provided in a text file names.txtPreview the documentView in a new window which is simply a list of person-mother-father information that the text author has provided. Side note: the data from our text (tudor.dat) has the problem of assuming the first list of names will be exactly the same as the second list of names, no spelling errors or name variations, I avoid this by providing the list of names only once in names.txtPreview the documentView in a new window CRUCIAL: Download the names.txtPreview the documentView in a new window file into your project folder (not \src). You need to actually download this file (not the textbook version) and read the file as is, else you will create code that does not work with my file, and your code will fail all testing (ouch!!!). I will actually test code with a smaller version (8 names) of this same file. So don't let a surprise line feed, blank line, or extra new line kill your program. Try a smaller file!!! Your FamilyTree class should be a binary tree, similar to IntTree in the text, but Person replaces the int as data. This creates a tree of Person nodes. When the user selects a Person, the program builds a customized FamilyTree with that Person at the root, their mother on the left, and father on the right. Since the mother is also a Person, another valid sub-tree needs to be built for her, and another valid sub-tree off the father on the right. An unknown parent can be treated as a null. Upload your completed FamilyInfo.java and FamilyTree.java files when done. I will use FamilyTest.javaPreview the documentView in a new window and Person.javaPreview the documentView in a new window to test your submissions.

---------------------------------------------------------------------------------------------------------------------------------------------------------------

// This program tests the FamilyInfo and Person classes by reading a

// file with family information and showing the maternal line, paternal

// line and children for various people.

//

// Code extracted from Reges and Stepp, Building Java Programs

// modified by W.P. Iverson for CS211, February 2016

import java.io.*;

import java.util.*;

public class FamilyTest {

public static void main (String[] args) throws FileNotFoundException {

// Interact with user via the console

Scanner console = new Scanner(System.in);

// Show program introduction:

System.out.println("This program reads an input file with family");

System.out.println("information and provides information about the");

System.out.println("maternal line, paternal line and children of");

System.out.println("various people. ");

// Read given family information into FamilyInfo object

Scanner input = new Scanner(new File("names.txt"));

System.out.println("Input file is names.txt in local project folder.");

FamilyInfo family = new FamilyInfo();

family.read(input); // BIG method, that reads data file into object

input.close(); // all data in file is read, so file is no longer needed

// Loop main program as long as user desires:

String name = "Keep running";

while(!name.equals("quit")) {

System.out.print(" Person's name ('quit' to end)? ");

name = console.nextLine(); // read line from user

System.out.println();

if (name.equalsIgnoreCase("quit")) break; // break loop upon quit

Person next = family.getPerson(name);

if (next == null) {

System.out.println("No match.");

} else {

showMaternal(next);

showPaternal(next);

showChildren(next);

System.out.println(" Shown as TREE (printSideways):");

FamilyTree structure = new FamilyTree(next);

structure.printSideways();

}

}

console.close();

}

// Shows maternal ancestors for given person

public static void showMaternal(Person current) {

System.out.println("Maternal line:");

int level = 1;

while (current != null) {

for (int i = 0; i

System.out.print(" ");

}

System.out.println(current.getName());

current = current.getMother();

level++;

}

System.out.println();

}

// Shows paternal ancestors for given person

public static void showPaternal(Person current) {

System.out.println("Paternal line:");

int level = 1;

while (current != null) {

for (int i = 0; i

System.out.print(" ");

}

System.out.println(current.getName());

current = current.getFather();

level++;

}

System.out.println();

}

// Shows children for given person

public static void showChildren(Person current) {

System.out.println("Children:");

if (current.getChildren().size()== 0) {

System.out.println(" none");

} else {

for (int i = 0; i

System.out.println(" " + current.getChildren().get(i).getName());

}

System.out.println();

}

}

}

----------------------------------------------------------------------------------------------------------------------------

CHILD/MOTHER/FATHER records follow:

-

Arthur

Elizabeth of York

Henry VII

-

Henry VIII

Elizabeth of York

Henry VII

-

Margaret

Elizabeth of York

Henry VII

-

Mary

Elizabeth of York

Henry VII

-

Mary I

Catherine of Aragon

Henry VIII

-

Elizabeth I

Anne Boleyn

Henry VIII

-

Edward VI

Jane Seymour

Henry VIII

-

James V

Margaret

James IV

-

Mary, Queen of Scots

Mary of Guise

James V

-

James VI & I

Mary, Queen of Scots

Henry, Lord Darnley

-

Frances

Mary

Charles Brandon

-

Lady Jane Grey

Frances

Henry Grey

-

Henry, Lord Darnley

Margaret Stuart

unknown

-

Margaret Stuart

Margaret

unknown

-

END OF FILE

-------------------------------------------------------------------------------------------------------------------------------------------

// Class Person records the name and immediate blood relations of an

// individual (mother, father, children).

import java.util.*;

public class Person {

private String name;

private Person mother;

private Person father;

private ArrayList children;

// post: constructs a person object with no family links

public Person(String name) {

this.name = name;

this.children = new ArrayList();

}

// post: returns this person's name

public String getName() {

return this.name;

}

// post: returns this person's mother (null if not known)

public Person getMother() {

return this.mother;

}

// post: returns this person's father (null if not known)

public Person getFather() {

return this.father;

}

// post: returns this person's list of children

public ArrayList getChildren() {

return this.children;

}

--------------------------------------------------------------------------------------------------------------------------------------------------

image text in transcribedimage text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

How does corporate communication differ from brand communication?

Answered: 1 week ago

Question

3. Why does the aggregate demand curve slope downward?

Answered: 1 week ago