Modify the DepartmentNode. The average number of employees in a tree node. Print it out with two decimal places. Your co

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Modify the DepartmentNode. The average number of employees in a tree node. Print it out with two decimal places. Your co

Post by answerhappygod »

Modify the DepartmentNode.
The average number of employees in a tree node. Print it outwith two decimal places. Your code must compute it recursively.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 2decimal places?
public class Ch15Recursin {
public static void main(String[] args) {
DepartmentNode node1 = new DepartmentNode("Bonneville PowerAdministration", 21, 150000);
DepartmentNode node2 = new DepartmentNode("Southeastern PowerAdministration", 11, 190000);
DepartmentNode node3 = new DepartmentNode("Southwestern PowerAdministration", 15, 110000);
DepartmentNode node4 = new DepartmentNode("Western Area PowerAdministration", 14, 120000);
DepartmentNode node5 = new DepartmentNode("Assistant Secretaryfor Electricity", 12, 191000,
node1, node2, node3, node4);
DepartmentNode node6 = new DepartmentNode("Assistant Secretaryfor Fossil Energy", 10, 100000);
DepartmentNode node7 = new DepartmentNode("Assistant Secretaryfor Nuclear Energy", 10, 100000);
DepartmentNode node8 = new DepartmentNode("Assistant Secretaryfor Energy Efficiency and Renewable Energy", 10, 100000);
DepartmentNode node9 = new DepartmentNode("Office of theUndersecretary of Energy", 10, 100000,
node5, node6, node7, node8);
DepartmentNode node10 = new DepartmentNode("Office of Science",15, 110000);
DepartmentNode node11 = new DepartmentNode("Office of ArtificalIntelligence and Technology", 14, 120000);
DepartmentNode node12 = new DepartmentNode("Office of theUndersecretary for Science", 12, 191000,
node10, node11);
DepartmentNode node13 = new DepartmentNode("Chief of Staff", 1,50000);
DepartmentNode node14 = new DepartmentNode("Ombudsman", 1,50000);
DepartmentNode root = new DepartmentNode("Office of Secretary",12, 191000,
node12, node9, node13, node14);
root.printSubtree(0);
System.out.println("Total budget is " + root.totalBudget());
System.out.println("The tree has " + root.NodeCount() + "nodes");
System.out.println("The tree has " + root.EmpCount() + "employees");
//HOW DO I GET THE AVG TO READ TO 2 DECIMALS
System.out.println("The average number of employees the tree hasis " + root.EmpCount()/root.NodeCount());
}
}
class DepartmentNode{
String name;
int employees;
int budget;
List<DepartmentNode> children = newArrayList<>();
public DepartmentNode(String name, int employees, int budget,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++)
System.out.print(" ");
System.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;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply