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

Write a Java program called EvoTree.java that reads in phylogenetic data from th

ID: 3728215 • Letter: W

Question

Write a Java program called EvoTree.java that reads in phylogenetic data from the console
(System.int) and outputs the corresponding phylogenetic tree. Your EvoTree class must
implement the provided Tester interface and also contain the main() method
where your program starts running. This is because your program will be tested via
this interface. The interface contains a single method:

public ArrayList compute( Scanner input );

This method must perform the required computation.

Input

The compute() method takes a Scanner object, which contains one or more lines. Each line,
except the last one, is of the form

Species A evolved from Species B where Species A and Species B are names of speci c species. The species names can comprise one or more words, but will never contain the words "evolved from". The last line of input is simply the word "end". See Figure 2 for an example.

Hint: Use the nextLine() method of the Scanner object to read one line of input at a
time, and then parse the line.

Semantics

Each line of the form "Species A evolved from Species B" represents a direct evolution from
species B to species A. The input lines are in arbitrary order.
All data will generate a single tree. That is, there will only be one species that is not
evolved from some other species (the root of the 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 The method compute( Scanner input ) should return an ArrayList of Strings denoting the phylogenetic tree. Each species should be a separate String. All children of a node in the tree are to be displayed in lexically sorted order. Each species should be prefixed with d-1 "I- ” followed by "I-" followed by the species name where d is the depth of the node and "_" denotes a space. This is exemplified in Figure 3 Example Sample Input Sample Output one-celled forms eches evolved from flatworms cecil evolved from snakes monkeys evolved from one-celled forms -1eeches lochness monster evolved from snakes -monkeys snakes evolved from one-celled forms -snakes flatworms evolved from one-celled forms I-cecil fung evolved from sponges sponges evolved from one-celled forms -sponges end l-flatworms | I-lochness monster I I-fung

Explanation / Answer

Hi,

As per the problem statement, Please find the solution below:

Note: Question was lengthy to answer.

So I answered the first part in that like  

1) Take the input from user line by line till end statement

2 ) For tree generation purpose added to treeMap

3) In treeMap, <String,String> as key value pair , key as root (from evolves , add values as list from evolved ) .

4) At the treeMap , having all mapping like : from evolved -> evolves list

Need to done: compute the tree as you expected (beacuse of lengthy , i did till 4 points)

Please find the solution below:

EvoTree.java:

import java.util.HashMap;

import java.util.Scanner;

import java.util.Set;

public class EvoTree {

private static Scanner input = new Scanner(System.in);

private static HashMap<String, String> treeMap = new HashMap<>();

public static void main(String[] args) {

EvoTree evoTree = new EvoTree();

evoTree.compute();

}

private void compute() {

String eachLine;

while (!(eachLine = input.nextLine()).equals("end")) {

String[] lineAtt = eachLine.split(" ");

if (treeMap.containsKey(lineAtt[3])) {

String newValue = treeMap.get(lineAtt[3]) + "," + lineAtt[0];

treeMap.put(lineAtt[3], newValue);

} else {

treeMap.put(lineAtt[3], lineAtt[0]);

}

}

System.out.println(treeMap.toString());

generateTree();

}

private void generateTree() {

Set<String> rootKeys = treeMap.keySet();

for (String eachKey : rootKeys) {

System.out.println(eachKey);

}

}

}

Please reach me out, if any doubts

Thanks

import java.util.HashMap;

import java.util.Scanner;

import java.util.Set;

public class EvoTree {

private static Scanner input = new Scanner(System.in);

private static HashMap<String, String> treeMap = new HashMap<>();

public static void main(String[] args) {

EvoTree evoTree = new EvoTree();

evoTree.compute();

}

private void compute() {

String eachLine;

while (!(eachLine = input.nextLine()).equals("end")) {

String[] lineAtt = eachLine.split(" ");

if (treeMap.containsKey(lineAtt[3])) {

String newValue = treeMap.get(lineAtt[3]) + "," + lineAtt[0];

treeMap.put(lineAtt[3], newValue);

} else {

treeMap.put(lineAtt[3], lineAtt[0]);

}

}

System.out.println(treeMap.toString());

generateTree();

}

private void generateTree() {

Set<String> rootKeys = treeMap.keySet();

for (String eachKey : rootKeys) {

System.out.println(eachKey);

}

}

}

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