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

This question has been posted on chegg before, but the answer provided is full o

ID: 3705881 • Letter: T

Question

This question has been posted on chegg before, but the answer provided is full of errors.

Part 5: Queries that use Select
In FamilyRecordQuery.java uncomment the code required for Part 5 in the main method and the classes.
As you noticed, the family has a lot of people named Robert and a lot of engineers. Use your
selectIterative and selectRecursive to find these people in the family tree.
To find all the Roberts, fix the SelectName class and search for the exact string “Robert” in the name
field of the FamilyRecord. This must use selectIterative.
To find all the Engineers, fix the SelectJob class and search for any text containing “Engineer” in the
job field of the FamilyRecord. This must use selectRecursive. This query will output a list of many
engineer types (people born in 1920 couldn’t have been Software Engineers).
Feel free to test our different names or jobs with these queries. To check your work, look at the
displayed tree or the CVS file to see that the output is correct.

Reference Code:

FamilyRecordQuery.java


package queries;

import java.util.Iterator;
import readers.LineFileReader;
import iterators.Apply;
import iterators.ApplyFunction;
import binaryTree.BinaryTree;
import iterators.Predicate;
import iterators.ReduceFunction;
import java.io.IOException;
import java.util.List;


public class FamilyRecordQuery {
public static void main(String[] args) throws IOException{
Iterator<String> lines = new LineFileReader("familyrecord.csv");
Iterator<Object[]> recordsGeneric = new Apply<>(new ParseCSVLine(), lines);
Iterator<FamilyRecord> records = new Apply<>(new ConvertToRecord(), recordsGeneric);   
  
BinaryTree<FamilyRecord> treeOfRecords = new BinaryTree();
BinaryTree<String> treeOfNames = new BinaryTree();
while(records.hasNext()){
FamilyRecord record = records.next();
treeOfNames.insertNode(record.name);
treeOfRecords.insertNode(record);
}
  
treeOfNames.displayTree();

//PART 3
int generationNumberB = 3;
String ageGroupB = treeOfNames.reduceAtDepthRecursive(generationNumberB, new ConcatentateNames());
System.out.println("Generation " + generationNumberB + ": " + ageGroupB);

int generationNumberA = 4;
String ageGroupA = treeOfRecords.reduceAtDepth(3, new ConcatenateNamesFromRecord());
System.out.println("Generation " + generationNumberA + ": " + ageGroupA);


//END PART 3
  
//PART 5
// List<FamilyRecord> robertList = treeOfRecords.selectIterative(new SelectName("Robert"));
// System.out.println(robertList);
  
// List<FamilyRecord> engineerList = treeOfRecords.selectRecursive(new SelectJob("Engineer"));
// System.out.println(engineerList);
  
//END PART 5


//Part 6
// List<FamilyRecord> under50;

// //INSERT CODE HERE

// System.out.println(under50);
  
}

private static class ConcatenateNamesFromRecord implements ReduceFunction<FamilyRecord,String>{
//PART 3   
}

private static class ConcatentateNames implements ReduceFunction<String, String>{
//PART 3

}
  
//PART 5 START
// private static class SelectName implements Predicate<FamilyRecord>{
  
// }
  
// private static class SelectJob implements Predicate<FamilyRecord>{
  
// }
  

//PART 5 END

//PART 6 add new class here
  

//////////////// Dont edit after here //////////////////////
  
// Converts a CSV record from an Object[] to a FlightRecord
private static class ConvertToRecord implements ApplyFunction<Object[], FamilyRecord> {
@Override
public FamilyRecord apply(Object[] r) {
return new FamilyRecord((String)r[0],
(int)r[1],
(String)r[2],
(String)r[3],
(String)r[4]);
}
}
  
private static class ParseCSVLine implements ApplyFunction<String, Object[]> {
@Override
public Object[] apply(String x) {
String[] fields = x.split(",");
Object[] r = new Object[fields.length];
for (int i=0; i<fields.length; i++) {
// try to convert to integer
try {
r[i] = Integer.parseInt(fields[i]);
} catch (NumberFormatException ex) {
// if it fails, then leave a string
r[i] = fields[i];
}
}
return r;
}
}

private static class FamilyRecord {
public final String name;
public final int birthYear;
public final String city;
public final String state;
public final String job;
  
private FamilyRecord(String n, int y, String c, String s, String j) {
name = n;
birthYear = y;
city = c;
state = s;
job = j;
}
  
@Override
public String toString(){
return "Family record(Name=" + name + ", Birth Year=" + birthYear + ", City=" + city + ", State=" + state + ", Job=" + job + ")";
}
  
}

}

familyRecord.cvs

Robert 2024 Seattle WA Student Ryan 1994 Arlington Heights IL Software Engineer Jisoon 1995 Dubuque IA Pharmacist Robert 1963 Park Ridge IL Bio Medical Engineer Angie 1964 Park Ridge IL Teacher Boge 1963 East Dubque IL Computer Engineer Jackie 1970 Bloomington IL Teacher Robert 1930 Chicago IL Mechanical Engineer Lee 1932 Chicago IL Homemaker Mathew 1932 Chicago IL Dentist Sandy 1936 Chicago IL Teacher David 1938 Cedar Falls IA Farmer Suki 1942 Orange CA Shop Owner Michael 1939 Chicago IL Scientist Amy 1942 Chicago IL Pharmacist Carl 1915 New York NY Business Betty 1920 Washington DC Homemaker John 1921 Cedar Rapids IA Construction Jane 1928 Chicago IL Homemaker Peter 1916 Fargo ND Engineer Stella 1917 Bismark ND Shop Owner Nick 1917 Holmes OH Factory Worker Ethal 1924 Ames IA Programmer Joe 1922 Fort Wane IN Farmer Mel 1920 Green Bay WI Chef Mitch 1919 Chicago IL Inventor Tara 1911 Desplaines IL Homemaker Ted 1920 Vernon Hills IL Farmer Tammy 1918 Springfield IL Seamstress Chris 1926 Normal IL Farmer Anna 1925 Traverse City MI Unknown

Explanation / Answer

//Below is iterative Level Order Traversal based solution to search 'Robert' string in binary tree.

// new code comment specifies the lines where new code is being inserted in the program provided. 2 functions- selectIterative and selectRecursive have been created which traverses in pre order traversal and search for strings "Robert" and "Engineer" in the binary tree. First root node is checked for the string and counter incremented if root node contains string 'robert'

package queries;

import java.util.Iterator;

import readers.LineFileReader;

import iterators.Apply;

import iterators.ApplyFunction;

import binaryTree.BinaryTree;

import iterators.Predicate;

import iterators.ReduceFunction;

import java.io.IOException;

import java.util.List;

public class FamilyRecordQuery {

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

Iterator<String> lines = new LineFileReader("familyrecord.csv");

Iterator<Object[]> recordsGeneric = new Apply<>(new ParseCSVLine(), lines);

Iterator<FamilyRecord> records = new Apply<>(new ConvertToRecord(), recordsGeneric);

//new code

private String data;

private BinaryStringTree left;

private BinaryStringTree right;

private int countRobert = 0;

private int countEngineer = 0;

public BinaryStringTree() {

this.data = null;

this.left = null;

this.right = null;

}

public BinaryStringTree(String data) {

this.data = data;

this.left = null;

this.right = null;

}

BinaryTree<FamilyRecord> treeOfRecords = new BinaryTree();

BinaryTree<String> treeOfNames = new BinaryTree();

while(records.hasNext()){

FamilyRecord record = records.next();

treeOfNames.insertNode(record.name);

treeOfRecords.insertNode(record);

}

treeOfNames.displayTree();

//PART 3

int generationNumberB = 3;

String ageGroupB = treeOfNames.reduceAtDepthRecursive(generationNumberB, new ConcatentateNames());

System.out.println("Generation " + generationNumberB + ": " + ageGroupB);

int generationNumberA = 4;

String ageGroupA = treeOfRecords.reduceAtDepth(3, new ConcatenateNamesFromRecord());

System.out.println("Generation " + generationNumberA + ": " + ageGroupA);

//END PART 3

//PART 5

List<FamilyRecord> robertList = treeOfRecords.selectIterative(new SelectName("Robert"));

System.out.println(robertList);

List<FamilyRecord> engineerList = treeOfRecords.selectRecursive(new SelectJob("Engineer"));

System.out.println(engineerList);

//END PART 5

//Part 6

// List<FamilyRecord> under50;

// //INSERT CODE HERE

// iterative process to search an element in a given binary tree

// new code

public void selectIterative() {

System.out.println(this.data);

if(this.data.equals("Robert"){

countRobert++;

// robert found at the root, find more roberts in the tree, so traverse the tree till it reached end of node

while(this.left != null){

this.left.traversePreOrder();

}

}

// if robert is not found at the root, search left subtree

else if (this.left != null ) {

if(this.left.data.equals("Robert"){

countRobert++;

this.left.traversePreOrder();

}

}

if (this.right != null) {

if(this.right.data.equals("Robert"){

countRobert++;

this.right.traversePreOrder();

}

}

return countRobert;

} // end of selectIterative function

public void selectRecursive() {

System.out.println(this.data);

if(this.data.equals("Engineer"){

countRobert++;

// Engineer found at the root, find more Engineers in the tree, so traverse the tree till it reached end of node

while(this.left != null){

this.left.traversePreOrder();

}

}

// if Engineer is not found at the root, search left subtree

else if (this.left != null ) {

if(this.left.data.equals("Engineer"){

countRobert++;

this.left.traversePreOrder();

}

}

if (this.right != null) {

if(this.right.data.equals("Engineer"){

countEngineer++;

this.right.traversePreOrder();

}

}

return countEngineer;

} // end of selectRecursive function

// System.out.println(under50);

}

private static class ConcatenateNamesFromRecord implements ReduceFunction<FamilyRecord,String>{

//PART 3

}

private static class ConcatentateNames implements ReduceFunction<String, String>{

//PART 3

}

//PART 5 START

// private static class SelectName implements Predicate<FamilyRecord>{

// }

// private static class SelectJob implements Predicate<FamilyRecord>{

// }

//PART 5 END

//PART 6 add new class here

//////////////// Dont edit after here //////////////////////

// Converts a CSV record from an Object[] to a FlightRecord

private static class ConvertToRecord implements ApplyFunction<Object[], FamilyRecord> {

@Override

public FamilyRecord apply(Object[] r) {

return new FamilyRecord((String)r[0],

(int)r[1],

(String)r[2],

(String)r[3],

(String)r[4]);

}

}

private static class ParseCSVLine implements ApplyFunction<String, Object[]> {

@Override

public Object[] apply(String x) {

String[] fields = x.split(",");

Object[] r = new Object[fields.length];

for (int i=0; i<fields.length; i++) {

// try to convert to integer

try {

r[i] = Integer.parseInt(fields[i]);

} catch (NumberFormatException ex) {

// if it fails, then leave a string

r[i] = fields[i];

}

}

return r;

}

}

private static class FamilyRecord {

public final String name;

public final int birthYear;

public final String city;

public final String state;

public final String job;

private FamilyRecord(String n, int y, String c, String s, String j) {

name = n;

birthYear = y;

city = c;

state = s;

job = j;

}

@Override

public String toString(){

return "Family record(Name=" + name + ", Birth Year=" + birthYear + ", City=" + city + ", State=" + state + ", Job=" + job + ")";

}

}

}

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