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.)
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);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.