Modify the DepartmentNode.
the average number of employees in a tree node. Print it outwith two decimal places. Your code must compute itrecursively.Hint: Create two recursive methods, one that returns the totalnumber of employees, and another that returns the number of nodes,then divide the two results.how do I make the avg read out to 2 decimal places?
public class Ch15Recursion {
public static void main(String[] args) {
DepartmentNode node1 = newDepartmentNode("Bonneville Power Administration", 21, 150000);
DepartmentNode node2 = newDepartmentNode("Southeastern Power Administration", 11,190000);
DepartmentNode node3 = newDepartmentNode("Southwestern Power Administration", 15,110000);
DepartmentNode node4 = new DepartmentNode("WesternArea Power Administration", 14, 120000);
DepartmentNode node5 = newDepartmentNode("Assistant Secretary for Electricity", 12,191000,
node1, node2, node3, node4);
DepartmentNode node6 = newDepartmentNode("Assistant Secretary for Fossil Energy", 10,100000);
DepartmentNode node7 = newDepartmentNode("Assistant Secretary for Nuclear Energy", 10,100000);
DepartmentNode node8 = newDepartmentNode("Assistant Secretary for Energy Efficiency andRenewable Energy", 10, 100000);
DepartmentNode node9 = new DepartmentNode("Officeof the Undersecretary of Energy", 10, 100000,
node5, node6, node7, node8);
DepartmentNode node10 = new DepartmentNode("Officeof Science", 15, 110000);
DepartmentNode node11 = new DepartmentNode("Officeof Artifical Intelligence and Technology", 14, 120000);
DepartmentNode node12 = new DepartmentNode("Officeof the Undersecretary for Science", 12, 191000,
node10, node11);
DepartmentNode node13 = new DepartmentNode("Chiefof Staff", 1, 50000);
DepartmentNode node14 = newDepartmentNode("Ombudsman", 1, 50000);
DepartmentNode root = new DepartmentNode("Officeof Secretary", 12, 191000,
node12, node9, node13, node14);
root.printSubtree(0);
out.println("Total budget is " +root.totalBudget());
out.println("The tree has " + root.NodeCount() + "nodes");
out.println("The tree has " + root.EmpCount() + "employees");
//HOW DO I GET THE AVG TO READ TO 2 DECIMALS
out.println("The average number of employees thetree has is " + root.EmpCount()/root.NodeCount());
}
}
class DepartmentNode{
String name;
int employees;
int budget;
List<DepartmentNode> children = newArrayList<>();
public DepartmentNode(String name, int employees, intbudget, DepartmentNode...children) {
super();
this.name = name;
this.employees = employees;
this.budget = budget;
for(DepartmentNode child : children)
this.children.add(child);
}
public void printSubtree (int level) {
for (int i = 0; i < level; i++)
out.print(" ");
out.println(name);
for (DepartmentNode child : children)
child.printSubtree(level + 1);
}
public int totalBudget() {
int answer = budget;
for (DepartmentNode child : children)
answer += child.totalBudget();
return answer;
}
public int NodeCount() {
int answer = 1;
for (DepartmentNode child : children)
answer += child.NodeCount();
return answer;
}
public int EmpCount() {
int answer = employees;
for (DepartmentNode child : children)
answer += child.EmpCount();
return answer;
}
}
Modify the DepartmentNode. the average number of employees in a tree node. Print it out with two decimal places. Your co
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am