Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need some assistance in fixing my Java code below for the given question, please read the required tasks first for a thorough explanation

Hello, I need some assistance in fixing my Java code below for the given question, please read the required tasks first for a thorough explanation and you will find my code near the bottom

image text in transcribed

image text in transcribed

The test cases could be found here: http://bit.ly/CS_assn_trees. These test cases are random input values that check to see if the program outputs the expected values as well.

Currently my current code passes 7 out of twenty test cases and must be changed and modified to pass the 20 given test cases. Listed below are my two Java programs TreeBuilder.java and Node.java. If anyone has a better solution and implementation (by using just a one program) please answer the question with your method instead, because the more simple and lesser lines yield better efficiency

Node.java

image text in transcribed

TreeBuilder.java

image text in transcribed

image text in transcribed

image text in transcribed

These are the copyable codes below:

Node.java:

import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class TreeBuilder { static String test = "AAATTCAACT " + "AAATTCCGA " + "AAATCGA " + "AAATA " + "AAAATC " + "AAATTCAACTT " + "AAATT " + "AAATTAAG " + "AAAAGAC " + "AAATTCAAC " + "AAAAGACTGG " + "AAATTC " + "AAATTCAACTA " + "AAATTCAACA " + "AAATTCAACG " + "done"; static ArrayList tree = new ArrayList(); static Node root = new Node("*", null); public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = ""; String ch = ""; while(!ch.equals("done")) { ch = sc.next(); tree.add(new Node(ch, null)); } tree.remove(tree.size()-1); Collections.sort(tree, NAME); root.children.addAll(tree); tree.clear(); tree.add(root); TreeBuilder T = new TreeBuilder(); T.buildTree(root.children); if(root.children.isEmpty()) System.out.println(root.name); else printTree(tree); } public void buildTree(ArrayList T) { for(int i = 0; i  j && T.get(j).name.startsWith(T.get(i).name)) { T.get(i).children.add(T.get(j)); T.remove(j); Collections.sort(T.get(i).children, NAME); buildTree(T.get(i).children); } } } } public TreeBuilder() { } public static int tabindex = -1; public static void printTree(ArrayList T) { if(!T.isEmpty()) { //print * if(tabindex  n2.name.length()) { return 1; } else { return 0; } } }; public static Comparator NAME = new Comparator( ) { @Override public int compare(Node n1, Node n2) { return n1.name.compareTo(n2.name); } }; } TreeBuilder.java:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class TreeBuilder { static String test = "AAATTCAACT " + "AAATTCCGA " + "AAATCGA " + "AAATA " + "AAAATC " + "AAATTCAACTT " + "AAATT " + "AAATTAAG " + "AAAAGAC " + "AAATTCAAC " + "AAAAGACTGG " + "AAATTC " + "AAATTCAACTA " + "AAATTCAACA " + "AAATTCAACG " + "done"; static ArrayList tree = new ArrayList(); static Node root = new Node("*", null); public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = ""; String ch = ""; while(!ch.equals("done")) { ch = sc.next(); tree.add(new Node(ch, null)); } tree.remove(tree.size()-1); Collections.sort(tree, NAME); root.children.addAll(tree); tree.clear(); tree.add(root); TreeBuilder T = new TreeBuilder(); T.buildTree(root.children); if(root.children.isEmpty()) System.out.println(root.name); else printTree(tree); } public void buildTree(ArrayList T) { for(int i = 0; i  j && T.get(j).name.startsWith(T.get(i).name)) { T.get(i).children.add(T.get(j)); T.remove(j); Collections.sort(T.get(i).children, NAME); buildTree(T.get(i).children); } } } } public TreeBuilder() { } public static int tabindex = -1; public static void printTree(ArrayList T) { if(!T.isEmpty()) { //print * if(tabindex  n2.name.length()) { return 1; } else { return 0; } } }; public static Comparator NAME = new Comparator( ) { @Override public int compare(Node n1, Node n2) { return n1.name.compareTo(n2.name); } }; } 
Your task is to create a program that generates phylogenetic tree based on DNA samples and outputs a representation of the tree Write a program called TreeBuilder.java that reads in DNA sequences from the console (System.in) and outputs the corresponding phylogenetic tree. Your TreeBuilder class must contain the mainO method where your program starts running Input Your program should read in the input using a Scanner object, which is instantiated with System.in. The input will contain one or more lines of input. Each will contain a single DNA sequence, comprising four letters (A,C,G,T). The last line will be the word done, indicating that no more input follows line, except the last one Hint: All you need to use is the next () method of the Scanner object Semantics The DNA sequences can be in any order and will all be unique. The root of the tree is the empty string (""), which on Pluto is the root of all life. All data will generate a single tree. Each species (except the root) will be evolved from exactly one species, but multiple species may evolve from a single species, as in the example. (This is a simplification.) Output Your program should output to System.out. The output should represent the generated phylogenetic tree. Each species should be on separate line. All children of a node in the tree are to be displayed in lexically sorted order. Each species should be prefixed with d-1 "followed by "- followed by the species name where d is the depth of the node. (See Figure 3.) For the root node, output +" instead of the empty string. Exampl Sample Input Sample Output aactaac aactaaccgaagc aactaaccgata aactt aactaaca aacta aactaaccga I-aaaag l-aacta I I-aactaac I I I-aactaaca I I-aactaaccga I I I-aactaaccgaagc I-aactaaccgata I-aacttc Hints and Suggestions The sample solution uses a 2-pass algorithm. The first pass builds the phylogenetic tree. The second pass recursively outputs the tree At least one of the submitted files must be TreeBuilder.java, which is where the main program starts to run. If you have more than one Java file to submit, place them all in a zip file and submit that Test Cases You must ensure that your program has the exact output for all 20 test cases for the program to be considered fully functional and a copy of the test cases are found in a zip file here http://bit.ly/CS assn trees 1 import java.util.ArrayList; 3public class Node 4 String name; Node parent; ArrayList(O; public Node (String n, Node p) 10 7 name n parentp 12 13 14 15 16 17 18 19 20 21 public Node (Node n) name n.name parent -n.parent; children.addAll (n.children); public boolean hasChildren() return children.isEmpty); 23 24 25 26 27 28 public void sort0 for( int = 0 ; tree = new ArrayListNodes(); static Node root-new Node("", null); public static void main(String[] args) 29 30 31 32 Scanner sc new Scanner(System.in); String input -"; string ch ""; 34 while(!ch.equals ("done")) { ch-sc.next); tree.add (new Node(ch, null)); 37 tree.remove(tree.size()-1); 39 40 41 Collections.sort(tree, NAME); 43 root.children.addA11 (tree); tree.clear); tree.add(root); 46 TreeBuilder T-new TreeBuilder); T.buildTree(root.children); 48 49 50 51 52 53 54 if(root.children.isEmpty)) System.out.println(root.name); else printTree tree); 57 58 58 public void buildTree (ArrayList T) 59 60 61 62 63 64 65 for(int 0; int j T.size ( )-1; j && T.get (j).name.startswith (T.get(i).name)) t children.add (T.get( T.remove(j) Collections.sort(T.get(i).children, NAME) buildTree(T.get(i).children); 67 68 69 70 71 72 73 74 75 76 public TreeBuilder) 78 79 80 public static int tabindex 82 83 84 85 86 877 -1; public static void printTree(ArrayList T) t if(T.isEmpty) //print * if(tabindex SIZE = new ComparatorNode>() { 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137, public static ComparatorNode> NAME = new ComparatorNode>( ) { 138 139 140 System.out.print("| "); 0verride public int compare(Node n1, Node n2) if(n1.name.length) n2.name.length)) return-1; else if (n1.name.length) n2.name.length)) f return 1; else return 0; 1; @Override public int compare(Node n1, Node n2) return n1.name.compareTo (n2.name); 142 143 1; Your task is to create a program that generates phylogenetic tree based on DNA samples and outputs a representation of the tree Write a program called TreeBuilder.java that reads in DNA sequences from the console (System.in) and outputs the corresponding phylogenetic tree. Your TreeBuilder class must contain the mainO method where your program starts running Input Your program should read in the input using a Scanner object, which is instantiated with System.in. The input will contain one or more lines of input. Each will contain a single DNA sequence, comprising four letters (A,C,G,T). The last line will be the word done, indicating that no more input follows line, except the last one Hint: All you need to use is the next () method of the Scanner object Semantics The DNA sequences can be in any order and will all be unique. The root of the tree is the empty string (""), which on Pluto is the root of all life. All data will generate a single tree. Each species (except the root) will be evolved from exactly one species, but multiple species may evolve from a single species, as in the example. (This is a simplification.) Output Your program should output to System.out. The output should represent the generated phylogenetic tree. Each species should be on separate line. All children of a node in the tree are to be displayed in lexically sorted order. Each species should be prefixed with d-1 "followed by "- followed by the species name where d is the depth of the node. (See Figure 3.) For the root node, output +" instead of the empty string. Exampl Sample Input Sample Output aactaac aactaaccgaagc aactaaccgata aactt aactaaca aacta aactaaccga I-aaaag l-aacta I I-aactaac I I I-aactaaca I I-aactaaccga I I I-aactaaccgaagc I-aactaaccgata I-aacttc Hints and Suggestions The sample solution uses a 2-pass algorithm. The first pass builds the phylogenetic tree. The second pass recursively outputs the tree At least one of the submitted files must be TreeBuilder.java, which is where the main program starts to run. If you have more than one Java file to submit, place them all in a zip file and submit that Test Cases You must ensure that your program has the exact output for all 20 test cases for the program to be considered fully functional and a copy of the test cases are found in a zip file here http://bit.ly/CS assn trees 1 import java.util.ArrayList; 3public class Node 4 String name; Node parent; ArrayList(O; public Node (String n, Node p) 10 7 name n parentp 12 13 14 15 16 17 18 19 20 21 public Node (Node n) name n.name parent -n.parent; children.addAll (n.children); public boolean hasChildren() return children.isEmpty); 23 24 25 26 27 28 public void sort0 for( int = 0 ; tree = new ArrayListNodes(); static Node root-new Node("", null); public static void main(String[] args) 29 30 31 32 Scanner sc new Scanner(System.in); String input -"; string ch ""; 34 while(!ch.equals ("done")) { ch-sc.next); tree.add (new Node(ch, null)); 37 tree.remove(tree.size()-1); 39 40 41 Collections.sort(tree, NAME); 43 root.children.addA11 (tree); tree.clear); tree.add(root); 46 TreeBuilder T-new TreeBuilder); T.buildTree(root.children); 48 49 50 51 52 53 54 if(root.children.isEmpty)) System.out.println(root.name); else printTree tree); 57 58 58 public void buildTree (ArrayList T) 59 60 61 62 63 64 65 for(int 0; int j T.size ( )-1; j && T.get (j).name.startswith (T.get(i).name)) t children.add (T.get( T.remove(j) Collections.sort(T.get(i).children, NAME) buildTree(T.get(i).children); 67 68 69 70 71 72 73 74 75 76 public TreeBuilder) 78 79 80 public static int tabindex 82 83 84 85 86 877 -1; public static void printTree(ArrayList T) t if(T.isEmpty) //print * if(tabindex SIZE = new ComparatorNode>() { 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137, public static ComparatorNode> NAME = new ComparatorNode>( ) { 138 139 140 System.out.print("| "); 0verride public int compare(Node n1, Node n2) if(n1.name.length) n2.name.length)) return-1; else if (n1.name.length) n2.name.length)) f return 1; else return 0; 1; @Override public int compare(Node n1, Node n2) return n1.name.compareTo (n2.name); 142 143 1

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

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

Students also viewed these Databases questions

Question

What are the diff erent states of consciousness?

Answered: 1 week ago