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

Project3.pf Adobe Acrobat Reader DC File Edit View Window Help Home Tools Projec

ID: 3593007 • Letter: P

Question

Project3.pf Adobe Acrobat Reader DC File Edit View Window Help Home Tools Project3 pdfx Sign In -)(#) 80% Submission: Only upload the .java files Warning: Formatting and proper accessibility will be graded. Export PDF A File names other than those specified below will NOT be graded and receive 0 points Exercise 1 Package Name Tree Adobe Export PDF Convert PDF Files to Word or Excel Online In this assignment yau will create a tree that can store fully-qualified JDK class names as strings. Each node may have 0..N child nodes. The root holds the initial package name component (prefix), each descendant will hold a following name component of the entire package name, and each leaf will hold a Java class name. Dots are not stored in the tree but are implicitly defined by the transitions between nodes. Any subsequent insertions after the root has been created requires the class name to start with the root package name, otherwise the class name will not be stored in the tree. Select PDF File Project3.pdf Convert to For example Microsoft Worddocx) Document Language: English (U.S.) Change lang util Convert String Object Create PDF - PackageNode.java: Create a class called PackageNode according to the following class diagram: PackageNode Edit PDF value: String children: ListcPackageNode> Comment Combine Files Organize Pages Fill & Sign Send for Signature tisLeaf): boolean PackageTree.java: Create a class called PackageTree according to the following class diagram. The class must implement the java.lang.Iterable PackageNode> interface PackageTree -root: PackageNode +add className boolean Store and share files in the Document Cloud Loan More

Explanation / Answer

1.

import java.util.List;

public class PackageNode {

private String value;

private List<PackageNode> children;

private boolean leaf;

public PackageNode(String value) {

this.value = value;

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

public List<PackageNode> getChildren() {

return children;

}

public void setChildren(List<PackageNode> children) {

this.children = children;

}

public boolean isLeaf() {

return leaf;

}

public void setLeaf(boolean leaf) {

this.leaf = leaf;

}

@Override

public String toString() {

return "PackageNode [value=" + value + ", children=" + children + ", leaf=" + leaf + "]";

}

}

import java.util.ArrayList;

import java.util.Iterator;

/**

* This class represent a tree whose Nodes are of type PackageNode.

*/

public class PackageTree {

// Top element of the tree

private PackageNode root;

// Height of the tree

private int height;

// Total number of nodes in the tree.

private int size;

/**

* This method return the size i.e total number of elements in the tree.

*

* @return - Total number of nodes.

*/

public int size() {

return size;

}

/**

* This method return the height of the tree.

*

* @return -height

*/

public int height() {

return height;

}

/**

* This method adds provided className into the tree by breaking the

* provided className by "." . As per the algorithm it ensures the every

* class name should start with the root. If root is null then we can insert

* any class name.

*

* @param className

* - Class name that will be inserted into the tree.

* @return - return true if class name added into the tree successfully,

* otherwise false.

*/

public boolean add(String className) {

if (className == null || className.length() == 0) {

return false;

}

String[] array = className.split("\.");

if (root == null) {

root = new PackageNode(array[0]);

height++;

size++;

if (array.length == 1) {

root.setLeaf(true);

return true;

}

} else if (!root.getValue().equals(array[0])) {

return false;

}

PackageNode node = root;

for (int i = 1; i < array.length; i++) {

PackageNode child = getNode(array[i], node);

if (child == null) {

child = new PackageNode(array[i]);

if (node.getChildren() == null) {

node.setChildren(new ArrayList<>());

node.setLeaf(false);

height++;

}

node.getChildren().add(child);

size++;

node = child;

}

}

return true;

}

/**

* This method checks whether the provided className exists into the tree by

* breaking the provided className by "." .

*

* @param className

* - Class name whose existence is checked.

* @return - return true if class name exists in the tree, otherwise false.

*/

public boolean contains(String className) {

String[] array = className.split("\.");

if (!root.getValue().equals(array[0])) {

return false;

}

PackageNode node = root;

for (int i = 1; i < array.length; i++) {

PackageNode child = getNode(array[i], node);

if (child == null) {

return false;

}

}

return true;

}

/**

* This method returns an instance of LeafIterator.

*

* @return - new LeafIterator instance.

*/

public Iterator<PackageNode> iterable() {

return new LeafIterator(root);

}

private PackageNode getNode(String value, PackageNode node) {

if (node.getChildren() == null) {

return null;

}

for (PackageNode packageNode : node.getChildren()) {

if (packageNode.getValue().equals(value)) {

return packageNode;

}

}

return null;

}

}

2.

import java.util.Iterator;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Stack;

public class LeafIterator implements Iterator<PackageNode> {

private PackageNode next;

private Queue<PackageNode> stack = new LinkedList<>();

public LeafIterator(PackageNode root) {

initialize(root);

this.next = stack.poll();

}

public void initialize(PackageNode node) {

if (node.getChildren() == null) {

stack.add(node);

} else {

for (PackageNode packageNode : node.getChildren()) {

initialize(packageNode);

}

}

}

@Override

public boolean hasNext() {

// TODO Auto-generated method stub

return next != null;

}

@Override

public PackageNode next() {

PackageNode itemToReturn = next;

next = stack.poll();

return itemToReturn;

}

}

3.

import java.util.Iterator;

public class Run {

public static void main(String[] args) {

PackageTree packageTree = new PackageTree();

System.out.println(packageTree.add("java.lang.String"));

System.out.println(packageTree.add("com.google.search"));

System.out.println(packageTree.add("java.lang.Boolean"));

System.out.println(packageTree.add("java.util.Scanner"));

System.out.println(packageTree.add("java.util.LinkedList"));

System.out.println(packageTree.add("java.lang.Double"));

System.out.println("Height:- " + packageTree.height());

System.out.println("Size:- " + packageTree.size());

Iterator<PackageNode> iterator = packageTree.iterable();

while (iterator.hasNext()) {

System.out.println(iterator.next().getValue());

}

}

}

OUTPUT:

true
false
true
true
true
true
Height:- 4
Size:- 8
String
Boolean
Scanner
LinkedList
Double