+1 (208) 254-6996 essayswallet@gmail.com
  

please help part 1 Explain the main purpose of each class and… please help  part 1Explain the main purpose of each class and explain how each class interacts with other classes. 1. LinkedTree.java// Your answer here2. MultiTreeNode.java// Your answer here3. Person.java// Your answer here4. PyramidScheme.java// Your answer here5. PyramidSchemeSim.java// Your answer here  part 2 The LinkedTree class uses MultiTreeNode objects to maintain its structure. Every MultiTreeNode object carries a specific data element, as usual. However, instead of having references to its left and right child node, these MultiTreeNode objects keep an ArrayList of references to all its child MultiTreeNodes, which can hold as many children as you add to it.In this way, you can construct a hierarchy of nodes. At the top of this hierarchy is the root node, which is kept as the only field in the LinkedTree class.The LinkedTree has methods foradding children to specific nodes already in the treefinding those specific nodesother utility methods such as contains and size.You must implement the following methods in LinkedTree.java: getRootElement(), addChild(), Contains(), and countDown()getRootElement(): This method returns the element held by the root.addChild(): This method has two overloading methods (corresponding to the same function name but with different parameter lists); you will implement both.The first overloading addChild() method takes a generic T parent and T child, invokes the existing method findNode() to locate the parent MultiTreeNode which holds the given element parent, and then, invokes another addChild method to add the child element into the located parent node.The second overloading method takes a MultiTreeNode parent and the T child element. Its job is to construct a MultiTreeNode which holds the child element, then call the node’s own addChild() method to append this new child node into its children list.If the given parent element cannot be found, an ElementNotFoundException should be thrown.Here we do not care about whether the child element is duplicated within other sibling child nodes of the same parent node. Nothing will be done if the given child element is null.Contains(): This method would invoke the findNode() method to determine if the tree contains a T target element.countDown(): This method is a private helper method for the size method; it will use recursion to count the number of nodes in the tree.Hint: The methods you implement in LinkedTree are all really short – none should be more than 5 lines of code, some of them can be a single line of code. If you are writing complicated methods, you are overthinking it!Run the tests to ensure that your LinkedTree methods are working.  Part 3: Finishing the PyramidScheme implementation The PyramidScheme class extends LinkedTree, meaning that it has access to all the functionality of a normal LinkedTree, except it only holds Person elements. This is because the PyramidScheme has its own methods, and overrides some of its superclass’ methods, that contain logic specifically meant for the Person class. The Person class is a basic representation of a person as an object, with fields for a person’s name and their balance (money).You must implement the following methods in PyramidScheme.java: findNodeChain()findNodeChain: This method will use recursion to find a target node, and then return an ArrayList of every node above the target node, up to the node that was initially passed in (in most cases, the root). Essentially, you can think of this as returning an ArrayList of the target’s parent, then grandparent, then great grandparent, et cetera. The whoBenefits method calls this method (with root as the parameter so that the entire tree is searched) in order to get a list of every node containing a person who benefits from the money made by the target. This will be a complex recursive method; however, its logic is similar to that of findNode in the LinkedTree class, so you should examine that to help determine the logic for this. Also, look at whoBenefits to understand how these results are used. This is the hardest part of the project.Hint: Finding the node chain is backward recursive: you need to make your way down through the tree, find the node, and then on the way back up, add each ‘parent’ node to the list. So the computation here (assigning the node to the ArrayList) happens as the recursive calls are returning and popping off the runtime stack.You could apply recursion or use an external linear or non-linear data structure(s) that we have learned this semester to facilitate the back-tracking process without recursion.If in need, please feel free to add private method(s)./* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package pyramid_scheme;import ADTs.TreeADT;import Exceptions.ElementNotFoundException;/** * * @author pmele (modified by ITSC 2214) * @param * @version 3/28/2019 */public class LinkedTree> implements TreeADT { /** * The root of the tree. */ protected MultiTreeNode root; /** * Normal constructor* Create the root node which holds the input data element* as in the parameter variable startElem ** @param startElem*/ public LinkedTree(T startElem) { root = new MultiTreeNode<>(startElem); } /** * Normal constructor* Use the input startNode as the root node* @param startNode the root node of the tree */ public LinkedTree(MultiTreeNode startNode) { root = startNode; } /** * TODO Returns the element stored at the root ** @return root element */ @Override public T getRootElement() { if (root != null) // TODO: add a return statement else // TODO: add a return statement } /** * Retrieve root node * @return MultiTreeNode root node */ private MultiTreeNode getRootNode() { return root; } /** * TODO * Adds a node containing the child element to another* node that is already in the tree containing the parent* element. This particular overload of the addChild method* is responsible for locating the parent node, then * calling the other overload to complete the task. * @param parent An element contained by a particular node in* the tree, which will ultimately be given a child node * @param child The element to be packaged into a node and* added to the tree* @throws ElementNotFoundException*/ public void addChild(T parent, T child) throws ElementNotFoundException { // locate the parent node MultiTreeNode parentNode = findNode(parent); if (parentNode == null) { // TODO: throw the proper exception }// declare and initialize new node with child element MultiTreeNode newNode = new MultiTreeNode(child); // TODO: add node containing the child element to another // node containing the parent element (1 line of code) } /** * TODO * Adds a node containing the child element to another* node that is already in the tree containing the parent* element. This particular overload of the addChild method* is responsible for creating a node for child and * adding it to the children of parentNode. * @param parentNode the node receiving a child node * @param child the element to be packaged and added as a* child node */ protected void addChild(MultiTreeNode parentNode, T child) { // TODO: construct a MultiTreeNode which is initialized by calling // a method that determines if a given node is holding a given target MultiTreeNode childNode = // TODO: call the new node’s own addChild() method to append // the new child node onto its children list (1 line of code) } /** * Finds the node that contains a target element. Calls the* recursive overload to search for target. * @param target the element being searched for * @return the MultiTreeNode containing the target, or null* if not found */ public MultiTreeNode findNode(T target) { if (target == null) { return null; } return findNode(root, target); } /** * Finds the node that contains a target element. This checks* the current node, and if it is the target, returns it.* Otherwise, it recursively checks each of the current node’s * children, to see if they can find it. * If none of this node’s children can find it either, * then null is returned. * @param node the node currently being examined * @param target the element being searched for * @return the MultiTreeNode containing the target, or null* if not found */ private MultiTreeNode findNode(MultiTreeNode node, T target) { //If this node is the one holding the target… if (node.getElement().compareTo(target) == 0) { return node; //…Return this node, so it is passed upwards. }else { MultiTreeNode temp; for (MultiTreeNode child : node.getChildren()) { temp = findNode(child, target); if (temp != null) {return temp; } } return null; } } /** * TODO* Tries to find a node that contains the target element,* and indicates true if it can, false if it cannot. * @param target the element being searched for. * @return a boolean representing whether or not the tree* contains a node with the target element. */ @Override public boolean contains(T target) { // TODO } /** * Returns the size of the tree, measured by how many nodes* are in it. Calls the recursive method countDown to* determine this. * @return the size of the tree. */ @Override public int size() { if (root == null) { return 0; } return countDown(root); } /** * TODO * Recursively counts the current node, its children, and so* on. Used by size() to determine how many nodes are in the* tree. * @param node the node currently being examined. * @return the amount of nodes from the starting node down. */ private int countDown(MultiTreeNode node) { if (node == null) { // TODO: add a return statement } if (node.getNumChildren() == 0) { // TODO: add a return statement } int count = 1; for (MultiTreeNode child : node.getChildren()) { // TODO: use recursion to count the number of nodes // in the tree (1 line of code) } return count; } /** * Retrieve a text representation of a tree rooted by given node * @return A series of lines demonstrating elements on each layer. */ @Override public String toString() { if (this.root.getChildren() == null || this.root.getChildren().size() == 0) return “”; return toString(this.root); } /** * Retrieve a text representation of a tree rooted by given node * @param node root node of a subtree * @return a text representation */ public String toString(MultiTreeNode node) { if (node.getChildren() == null || node.getChildren().size() == 0) return node.getElement().toString(); String treeinfo = node.getElement().toString() + “n”; for (MultiTreeNode child : node.getChildren()) treeinfo += child.toString() + “n”; return treeinfo; } /** * Count the number of leaf nodes in the tree * @return number of leaf nodes in the tree or subtree* rooted by given node */ public int leafCounter() { return leafCounter(root); } /** * leafCounter * If the parameter node is empty, the tree rooted by the* parameter node is empty, having zero leaf node. * If the parameter node has empty left subtree and empty* right subtree, the parameter node is the only node in* the tree rooted by the parameter node, and it is the* unique leaf node. Or else, the tree or subtree rooted by* the parameter node includes more than one node. The number * of leaves of such a tree is the addition of the numbers of* left subtree and right subtree rooted by the parameter node. * This recursive method calculates the number of leaf nodes * in a tree rooted by the parameter node. * @param node* @return number of leaf nodes in the tree or subtree* rooted by given node */ public int leafCounter(MultiTreeNode node) { if (node == null) return 0; if (node.isLeaf()) return 1; int leafs = 0; for (MultiTreeNode child : node.getChildren()) leafs += leafCounter(child); return leafs; } /** * Examine whether the tree is empty * @return true – the tree is empty *false – the tree is not empty */ public boolean isEmpty() { return this.root == null; }} package pyramid_scheme;import java.util.ArrayList;/** * A MultiTreeNode is a tree node that can have any number of children * @author clatulip (modified by ITSC 2214) * @param * @version 1.0 */public class MultiTreeNode> { /** * Stores the element that is of type */ private T element; /** * Stores links to other MultiTreeNodes in an indexed list */ private ArrayList> children; /** * Default constructor creates an empty node */ public MultiTreeNode() { children = new ArrayList<>(); element = null; } /*** Creates a node containing element * @param elem element to be stored */ public MultiTreeNode(T elem) { children = new ArrayList<>(); element = elem; } /** * Add a child node to this node, by adding it to the list * @param child*/ public void addChild(MultiTreeNode child) { children.add(child); } /** * Remove a child node from this node, by removing it from the list * @param child*/ public void removeChild(MultiTreeNode child) { children.remove(child); } /** * Find out how many child nodes this node has * @return size (int) of the arrayList of MultiTreeNodes */ public int getNumChildren() { return children.size(); } /** * Get the child node at index specified. Note that if the index isn’t valid * then null is returned. No exception is thrown. * @param index int* @return MultiTreeNode that was at specified index */ public MultiTreeNode getChild(int index) { if (index >= children.size()) return null;return children.get(index); } /** * Returns an ArrayList of all the child nodes * @return arrayList that contains all the children of this node */ public ArrayList> getChildren() { return children; } /** * Returns the element stored in this node * @return element stored in the node */ public T getElement() { return element; } /** * Sets the element in this node to the passed in element * @param elem of generic type */ public void setElement(T elem) { element = elem; } /** * TODO* Examine whether this multi-way tree node is empty * @return true : it is a leaf *false: it is not a leaf */ public boolean isLeaf() { return (this.children == null || ((this.children != null) && (this.children.size() == 0))); } /** * Creates a string out of the element, the number of* children, and each child * @return a string representing this node and it’s* offspring */ @Override public String toString() { String temp = ” MultiTreeNode{” + “element=” + this.element.toString() + “, num_children=” + children.size(); if (children.size() > 0) temp += “, children=”; for (int i = 0; i < children.size(); i++) { temp = temp.concat("tchild " + i+ ": " + children.get(i).toString() + "n"); } temp = temp.concat("}"); return temp; //return element.toString(); }} package pyramid_scheme;/** * A basic representation of a person and their money. * @author pmele * @version 3/29/2019 */public class Person implements Comparable { private String name; private double balance; /** * Default Person constructor */ public Person() { name = “Anonymous”; balance = 0.0; } /** * Name-only Person constructor ** @param n The Person’s name */ public Person(String n) { name = n; balance = 0.0; } /** * Normal Person constructor ** @param n The Person’s name * @param bal The Person’s account balance */ public Person(String n, double bal) { name = n; balance = bal; } /** * Returns the Person’s account balance ** @return balance */ public double getBalance() { return balance; } /** * Adds or subtracts an amount from the Person’s balance ** @param amount The amount to adjust by (Can be negative). */ public void adjustBalance(double amount) { balance += amount; } /** * Returns the Person’s name ** @return name */ public String getName() { return name; } /** * Compares this Person to another object for equality, based on name. ** @param o Another Object to compare this to * @return a boolean, true if this and o are equal, otherwise false */ @Override public boolean equals(Object o) { if (!(o instanceof Person)) { return false; } Person p = (Person)o; return this.name.equals(p.getName()); } /** ** @return a String describing this Person */ @Override public String toString() { return name + ” (Bal: $” + balance + “)”; } @Override public int compareTo(Person t) { return this.name.compareTo(t.getName()); }} package pyramid_scheme;import DataStructures.LinkedStack;import Exceptions.ElementNotFoundException;import java.util.ArrayList;import java.util.Collections;/** * A Pyramid Scheme is a particular form of LinkedTree * that has methods for simulating a pyramid scheme scam. * * @author pmele * @version 3/28/2019 */public class PyramidScheme extends LinkedTree { private double recruitPrice; private double paidUp; //Add constant for amount passed upwards /** * Initialized-root Default constructor (takes a Person) * * @param startElem the root of the scheme (Element) */ PyramidScheme(Person startElem) { super(startElem); recruitPrice = 25.00; paidUp = 0.10; //10% } /** * Normal constructor (takes a Person) * * @param startElem the root of the scheme (Element) * @param rPrice Price to recruit in this scheme * @param pUp Fraction of earnings paid upwards in this scheme */ PyramidScheme(Person startElem, double rPrice, double pUp) { super(startElem); recruitPrice = rPrice; paidUp = pUp; while (paidUp > 1) { paidUp *= .01; //This will convert percentages into //decimals } } /** * Initialized-root Default constructor (takes a MultiTreeNode) * * @param startNode the root of the scheme (Node) */ PyramidScheme(MultiTreeNode startNode) { super(startNode); recruitPrice = 25.00; paidUp = 0.10; } /** * Normal constructor (takes a MultiTreeNode) * * @param startNode the root of the scheme (Node) * @param rPrice Price to recruit in this scheme * @param pUp Fraction of earnings paid upwards in this* scheme */ PyramidScheme(MultiTreeNode startNode, double rPrice, double pUp) { super(startNode); recruitPrice = rPrice; paidUp = pUp; while (paidUp > 1) { paidUp *= .01; //This will convert percentages into decimals } } /** * Initiates the recursive findNodeChain method to return a* list of all nodes that are above the node containing the* target. This overload takes a Person. * * @param sucker the individual from whom others above are* benefitting* @return an ArrayList of individuals higher up in the* hierarchy */ public ArrayList> whoBenefits(Person sucker) { return findNodeChain(root, sucker); } /** * Initiates the recursive findNodeChain method to return a* list of all nodes that are above the node containing the* target. This overload takes a MultiTreeNode. * * @param sucker the individual from whom others above are* benefitting* @return an ArrayList of individuals higher up in the* hierarchy */ public ArrayList> whoBenefits(MultiTreeNode sucker) { return findNodeChain(root, sucker.getElement()); } /** * TODO Similar to findNode, but rather than returning the node* that contains the target, instead returns a list of nodes* above the one that contains the target, in low to high* level order. * * @param node the current node being examined * @param target the Person being searched for * @return an ArrayList of nodes that are directly above the* target in the hierarchy */ private ArrayList> findNodeChain(MultiTreeNode node, Person target) { throw new UnsupportedOperationException(“Not supported yet.”); } /** * Similar to findNode, this will see it the target exists* and return a truth value. * * @param node the current node being examined * @param target the person being searched for * @param list the array list sent in * @return a boolean if the target is found */ private boolean findNodeChain(MultiTreeNode node, Person target, ArrayList> list) { if (target == null || node == null) { return false; } if (node.getElement().equals(target)) { return true; } for (MultiTreeNode child : node.getChildren()) { if (findNodeChain(child, target, list)) { if (!child.getElement().equals(target)) list.add(child); return true; } } return false; } /** * An override of LinkedTree’s addChild, which does the same* thing, but ensures the appropriate payment between the* recruit and the recruiter, as well as those above them in* the hierarchy. * * @param recruiter the person who is recruiting the other* into the scheme * @param recruit the person being brought into the scheme * @throws ElementNotFoundException */ @Override public void addChild(Person recruiter, Person recruit) throws ElementNotFoundException { addChild(findNode(recruiter), recruit); } /** * An override of LinkedTree’s addChild, which does the same* thing, but ensures the appropriate payment between the* recruit and the recruiter, as well as those above them in* the hierarchy. * * @param recruiter the person who is recruiting the other* into the scheme* @param recruit the person being brought into the scheme */ @Override public void addChild(MultiTreeNode recruiter, Person recruit) { super.addChild(recruiter, recruit); recruit.adjustBalance(-recruitPrice); payUp(recruiter, recruitPrice); } /** * Performs all the transactions inherent to the system* wherein a percentage of anything earned by an individual* is passed upwards in the hierarchy, such that each* individual directly above gets an increasingly smaller * fraction of the money. * * @param moneyMaker the initial individual earning the money * @param moneyMade the initial money earned */ private void payUp(MultiTreeNode moneyMaker, double moneyMade) { ArrayList> owed = whoBenefits(moneyMaker); if (owed == null || owed.isEmpty()) { //Person at top gets paid all moneyMaker.getElement().adjustBalance(moneyMade); return; } //Initial money maker gets payed, with a portion lost to//those above moneyMaker.getElement().adjustBalance( (1.0 – paidUp) * moneyMade); //Each step above takes a share, but passes a portion of//it up for (int i = 0; i < owed.size() - 1; i++) { moneyMade = paidUp * moneyMade; owed.get(i).getElement().adjustBalance((1.0 - paidUp) * moneyMade); } //Person at the very top gets his whole share owed.get(owed.size() - 1).getElement() .adjustBalance(paidUp * moneyMade); } /** * Initiates the collapse of the pyramid scheme */ public void initiateCollapse() { collapse(root); } /** * A recursive method that determines the punishment received* by different individuals in the pyramid scheme after it is* discovered and halted. Also severs each node from its* children, such that no hierarchy exists at all when the* collapse is complete. * * @param personNode the individual currently being examined * @return */ private int collapse(MultiTreeNode personNode) { int underlings = 1; ArrayList> toSever = new ArrayList<>(); for (MultiTreeNode child : personNode.getChildren()) { underlings += collapse(child); toSever.add(child); } for (MultiTreeNode child : toSever) { personNode.removeChild(child); } if (personNode.getElement().getBalance() < 0) { System.out.println(personNode.getElement() + " is a victim "+ "and has been returned their money."); personNode.getElement().adjustBalance(-personNode.getElement().getBalance()); }else if (personNode.getElement().getBalance() < 100) { System.out.println(personNode.getElement() + " has been "+ "appropriately warned, and fined $" + underlings * 5.0); personNode.getElement().adjustBalance(-underlings * 5.0); }else if (personNode.getElement().getBalance() < 1000) { System.out.println(personNode.getElement() + " has been charged "+ "with a misdeamenor, and fined $" + underlings * 5.0); personNode.getElement().adjustBalance(-underlings * 5.0); }else { System.out.println(personNode.getElement() + " has been charged "+ "with a felony, and fined $" + underlings * 5.0); personNode.getElement().adjustBalance(-(underlings * 5.0)); } return underlings; }} package pyramid_scheme;import Exceptions.ElementNotFoundException;import java.util.ArrayList;/** * * @author pmele * @version 3/28/2019 */public class PyramidSchemeSim { /** * Main method for running the simulation. ** @param args*/ public static void main(String[] args) { Person conMan = new Person("Brynjolf"); MultiTreeNode conArtist = new MultiTreeNode<>(conMan); PyramidScheme myPyramid = new PyramidScheme(conArtist, 100.00, 0.25); Person[] anons = new Person[30000]; for (int i = 0; i < 30000; i++) { anons[i] = new Person("Anon " + i); } ArrayList layerReps = new ArrayList<>(); layerReps.add(conMan); ArrayList people = new ArrayList<>(); people.add(conMan); int currAnon = 0; for (int i = 0; i < 6; i++) { ArrayList recruits = new ArrayList<>(); for (Person recruiter : people) { int numRecruits = (int)(Math.random() * 5) + 1; for (int n = 0; n < numRecruits; n++) {try { myPyramid.addChild(recruiter, anons[currAnon]);} catch (ElementNotFoundException ex) { ex.printStackTrace();}recruits.add(anons[currAnon]);currAnon++; } } people = recruits; layerReps.add(recruits.get(0)); } System.out.println("Size of Pyramid Scheme: " + myPyramid.size()); System.out.println("Recruits on last layer: " + (people.size())); int layer = 1; for (Person rep : layerReps) { System.out.println("Layer " + layer + ": $" + rep.getBalance()); layer++; } myPyramid.initiateCollapse(); layer = 1; for (Person rep : layerReps) { System.out.println("Layer " + layer + ": $" + rep.getBalance()); layer++; } System.out.println("Size of Pyramid Scheme: " + myPyramid.size()); }} Computer Science Engineering & Technology Java Programming

Don't use plagiarized sources. Get Your Custom Essay on
Top answer: please help part 1 Explain the main purpose of each class and…
Just from $10/Page
Order Essay

Order your essay today and save 10% with the discount code ESSAYHELP