Question: You are to create a Program that simulates a Database with baseball pitchers information that is stored in a Binary Search Tree. Summary of program:

You are to create a Program that simulates a Database with baseball pitchers information that is stored in a Binary Search Tree. Summary of program: Your program will start with reading a csv input file filled with pitcher stats. Each line of this file will contain the needed information to create a pitcher object. The first line of this file defines what data is in the columns. This file will be given to you. As each line of the file is read, you will create a Pitcher object and place it in the Binary Search Tree. Then a menu will be displayed giving you choices to display pitcher information, update, remove, or add new pitcher. When quit is chosen the contents of your binary search tree will be traversed and output to update the same datafile. (You will be writing over the original data) You may want to create a file of a different name for testing purposes, so that you do not corrupt the original file during debugging.) Pitcher class: The Pitcher class will contain the following private data variables: - lastname (String) - firstname (String) - team (String) - #games pitched - #games won (int) - #shutouts (int) - #innings pitched (double) - #hits - #earned runs (int) - #strikeouts (int) - ERA (double) You will calculate the value of ERA in the constructor from the data in the file. The math is: ERA = #earned runs / #innings pitched * 9 Remember to use double precision math. The Pitcher class must also contain the following methods: - constructor that takes a line from the CSV file - constructor that takes only a first name, last name, and a team abbreviation - accessors for each of the variables - modifiers for each of the variables - method to add the results of a new game, i.e., was the game a win? was the game a shutout? increment the number of innings pitches, the number of earned runs, the number of strikeouts, and automatically update the ERA. - compareTo method to help sort (class will implement Comparable) - toString that will convert to one string all of the stored info You may wish to add other methods to the Pitcher class for your own use. To create the Binary Search Tree you will be using the BTNode class given to you and you will be finishing methods in the template class TreeBag. In the TreeBag class you only need to implement methods that you will be using for this project. You are not required to implement the rest of the methods in the class. You may leave them as program stubs and ignore. Make sure all references to Pitcher objects are generic ones. I have written Pitcher_Object in as the parameters so that you better understand what each method is doing, but in the actual Treebag class they should be listed as E. Among the methods that you will need to create are: public void add (Pitcher_Object) You will be adding pitchers in the tree to locations based on the pitchers lastname, but there is method in the Pitcher class that you have created already called compareTo. Therefore you should use the compareTo method to compare this new pitcher to pitchers already within the tree. public Pitcher_Object retrieve (Pitcher_Object). The retrieve method will search the Binary Search tree for the element, the method will return the object that was found. If the object is not found the method returns null. You will need to create a Pitcher object with only the lastname and firstname, in order to do the search. I created an extra constructor for this task. When compareTo gives a zero, you know that you have found the correct Node. public boolean remove (Pitcher_Object) The remove method returns boolean therefore you need first find if the object is in the tree, if not return false, if yes, then delete the node from the tree and return true. Make careful sure that after Pitcher removal you still have a valid binary search tree. (Handle all cases) public void display() Display the pitcher name and his ERA in alphabetical order based on the lastname (think of the appropriate traversal through the tree to get this) You may want to create other methods that you will be using in the TreeBag class. You are to make use of the following class that we have discussed without making any changes. These classes are stored in the project folder. BTNode(unaltered) You will create these classes to finish the project: Pitcher (explained above) TreeBag (template given) PitcherStatsTree - main driver of program. The main driver was described above and contains the main menu which must contain the following choices: Menu choices: 1. Display listing to screen of all pitchers stats (ordered by lastname) 2. Display the pitchers in current tree format (Use displayAsTree ) 3. Find and display one individual pitchers stats 4. Update an individual pitchers stats (by adding results of a new game) 5. Remove a pitcher from the Database 6. Add a new pitcher to the Database 7. Quit and update datafile The Datafile used is data stored in a CSV (comma separated values) text format, similar to what you have seen in previous assignments, with one pitcher on each line and commas between fields. It is organized like this: lastname,firstname,team,games_pitched,games_won,win_pct,shu touts,innings_pitched,hits,earnedruns,strikeouts Commas are used to separate the individual columns; the first line gives the column names be sure to skip the first line when reading this file. You can ignore the WIN_PCT column. The sample data file given is called pitcherstats.txt and will be used as a starting point to test your program. A copy of this file is given in with these program directions. Tips for good grades: Make sure you use comments where needed and use variable names that make sense, some of your grade will depend on program style as well as the use of your program. Update the comments in the class file, to include your names and any new information You will lose points for things like not indenting, or naming variables in nondescriptive ways. Do no leave in debugging code, or commented out code. I use jGrasp and the java version that is in the lab computers. So make sure that your programs work with this. Test your own projects thoroughly before you hand them in. Late projects will not be accepted so plan ahead. The four classes you are using for this project should bein separate files. Name them PitcherStatsTree.java, Pitcher.java , TreeBag.java andBTNode.java. If you do not name these files correctly, you will lose points. Use Javadoc to create the documentation for your TreeBag class.

picherstats.txt

Morton charlie Hou 25 14 0.667 0 146.2 125 59 163 Kershaw Clayton LAD 27 18 0.818 0 175 136 45 202 Vargas jason KC 32 18 0.621 1 179.2 181 83 134

/****************************************************************************** * A BTNode< provides a node for a binary tree. Each node * contains a piece of data (which is a reference to an E object) and references * to a left and right child. The references to children may be null to indicate * that there is no child. The reference stored in a node can also be null. * * 
Limitations:
* Beyond Int.MAX_VALUE elements, treeSize, is * wrong. * *
Java Source Code for this class:
* * http://www.cs.colorado.edu/~main/edu/colorado/nodes/BTNode.java * * @author Michael Main * (main@colorado.edu) * * @version * Jul 22, 2005 ******************************************************************************/ public class BTNode { // Invariant of the BTNode class: // 1. Each node has one reference to an E Object, stored in the instance // variable data. // 2. The instance variables left and right are references to the node's // left and right children. private E data; private BTNode left, right; /** * Initialize a BTNode with a specified initial data and links * children. Note that a child link may be the null reference, * which indicates that the new node does not have that child. * @param <CODE>initialData * the initial data of this new node * @param <CODE>initialLeft * a reference to the left child of this new node--this reference may be null * to indicate that there is no node after this new node. * @param <CODE>initialRight * a reference to the right child of this new node--this reference may be null * to indicate that there is no node after this new node. *
Postcondition:
* This node contains the specified data and links to its children. **/ public BTNode(E initialData, BTNode initialLeft, BTNode initialRight) { data = initialData; left = initialLeft; right = initialRight; } /** * Accessor method to get the data from this node. * @param - none * @return * the data from this node **/ public E getData( ) { return data; } /** * Accessor method to get a reference to the left child of this node. * @param - none * @return * a reference to the left child of this node (or the null reference if there * is no left child) **/ public BTNode getLeft( ) { return left; } /** * Accessor method to get the data from the leftmost node of the tree below * this node. * @param - none * @return * the data from the deepest node that can be reached from this node by * following left links. **/ public E getLeftmostData( ) { if (left == null) return data; else return left.getLeftmostData( ); } /** * Accessor method to get a reference to the right child of this node. * @param - none * @return * a reference to the right child of this node (or the null reference if there * is no right child) **/ public BTNode getRight( ) { return right; } /** * Accessor method to get the data from the rightmost node of the tree below * this node. * @param - none * @return * the data from the deepest node that can be reached from this node by * following right links. **/ public E getRightmostData( ) { if (right == null) return data; else return right.getRightmostData( ); } /** * Uses an inorder traversal to print the data from each node at or below * this node of the binary tree. * @param - none *
Postcondition:
* The data of this node and all its descendants have been writeen by * System.out.println( ) using an inorder traversal. **/ public void inorderPrint( ) { if (left != null) left.inorderPrint( ); System.out.println(data); if (right != null) right.inorderPrint( ); } /** * Accessor method to determine whether a node is a leaf. * @param - none * @return * true (if this node is a leaf) or * false (if this node is not a leaf. **/ public boolean isLeaf( ) { return (left == null) && (right == null); } /** * Uses a preorder traversal to print the data from each node at or below * this node of the binary tree. * @param - none *
Postcondition:
* The data of this node and all its descendants have been writeen by * System.out.println( ) using a preorder traversal. **/ public void preorderPrint( ) { System.out.println(data); if (left != null) left.preorderPrint( ); if (right != null) right.preorderPrint( ); } /** * Uses a postorder traversal to print the data from each node at or below * this node of the binary tree. * @param - none *
Postcondition:
* The data of this node and all its descendants have been writeen by * System.out.println( ) using a postorder traversal. **/ public void postorderPrint( ) { if (left != null) left.postorderPrint( ); if (right != null) right.postorderPrint( ); System.out.println(data); } /** * Uses an inorder traversal to print the data from each node at or below * this node of the binary tree, with indentations to indicate the depth * of each node. * @param <CODE>depth * the depth of this node (with 0 for root, 1 for the root's * children, and so on)( *
Precondition:
* depth is the depth of this node. *
Postcondition:
* The data of this node and all its descendants have been writeen by * System.out.println( ) using an inorder traversal. * The indentation of each line of data is four times its depth in the * tree. A dash "--" is printed at any place where a child has no * sibling. **/ public void print(int depth) { int i; // Print the indentation and the data from the current node: for (i = 1; i <= depth; i++) System.out.print(" "); System.out.println(data); // Print the left subtree (or a dash if there is a right child and no left child) if (left != null) left.print(depth+1); else if (right != null) { for (i = 1; i <= depth+1; i++) System.out.print(" "); System.out.println("--"); } // Print the right subtree (or a dash if there is a left child and no left child) if (right != null) right.print(depth+1); else if (left != null) { for (i = 1; i <= depth+1; i++) System.out.print(" "); System.out.println("--"); } } /** * Remove the leftmost most node of the tree with this node as its root. * @param - none *
Postcondition:
* The tree starting at this node has had its leftmost node removed (i.e., * the deepest node that can be reached by following left links). The * return value is a reference to the root of the new (smaller) tree. * This return value could be null if the original tree had only one * node (since that one node has now been removed). **/ public BTNode removeLeftmost( ) { if (left == null) return right; else { left = left.removeLeftmost( ); return this; } } /** * Remove the rightmost most node of the tree with this node as its root. * @param - none *
Postcondition:
* The tree starting at this node has had its rightmost node removed (i.e., * the deepest node that can be reached by following right links). The * return value is a reference to the root of the new (smaller) tree. * This return value could be null if the original tree had only one * node (since that one node has now been removed). **/ public BTNode removeRightmost( ) { if (right == null) return left; else { right = right.removeRightmost( ); return this; } } /** * Modification method to set the data in this node. * @param <CODE>newData * the new data to place in this node *
Postcondition:
* The data of this node has been set to newData. **/ public void setData(E newData) { data = newData; } /** * Modification method to set the link to the left child of this node. * @param <CODE>newLeft * a reference to the node that should appear as the left child of this node * (or the null reference if there is no left child for this node) *
Postcondition:
* The link to the left child of this node has been set to newLeft. * Any other node (that used to be the left child) is no longer connected to * this node. **/ public void setLeft(BTNode newLeft) { left = newLeft; } /** * Modification method to set the link to the right child of this node. * @param <CODE>newLeft * a reference to the node that should appear as the right child of this node * (or the null reference if there is no right child for this node) *
Postcondition:
* The link to the right child of this node has been set to newRight. * Any other node (that used to be the right child) is no longer connected to * this node. **/ public void setRight(BTNode newRight) { right = newRight; } /** * Copy a binary tree. * @param <CODE>source * a reference to the root of a binary tree that will be copied (which may be * an empty tree where source is null) * @return * The method has made a copy of the binary tree starting at * source. The return value is a reference to the root of the copy. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new tree. **/ public static BTNode treeCopy(BTNode source) { BTNode leftCopy, rightCopy; if (source == null) return null; else { leftCopy = treeCopy(source.left); rightCopy = treeCopy(source.right); return new BTNode(source.data, leftCopy, rightCopy); } } /** * Count the number of nodes in a binary tree. * @param <CODE>root * a reference to the root of a binary tree (which may be * an empty tree where source is null) * @return * the number of nodes in the binary tree *
Note:
* A wrong answer occurs for trees larger than * INT.MAX_VALUE. **/ public static int treeSize(BTNode root) { if (root == null) return 0; else return 1 + treeSize(root.left) + treeSize(root.right); } }
// File: TreeBag.java  // The implementation of most methods in this file is left as a student // exercise from Section 9.5 of "Data Structures and Other Objects Using Java"  /****************************************************************************** * This class is a homework assignment; * An TreeBag is a collection of int numbers. * * 
Limitations:
* Beyond Integer.MAX_VALUE elements, countOccurrences, * and size are wrong. * *
Note:
* This file contains only blank implementations ("stubs") * because this is a Programming Project for my students. * * @version * Jan 24, 2016 ******************************************************************************/ public class TreeBagextends Comparable> implements Cloneable { // The Term E extends Comparable is letting the compiler know that any type // used to instantiate E must implement Comparable. i. e. that means that whatever // type E is must have a compareTo method so that elements can be compared against one another // This is required becuase we are doing comparisons in our methods // Invariant of the TreeBag class: // 1. The elements in the bag are stored in a binary search tree. // 2. The instance variable root is a reference to the root of the // binary search tree (or null for an empty tree). private BTNode root; /** * Insert a new element into this bag. * @param <CODE>element * the new element that is being inserted *
Postcondition:
* A new copy of the element has been added to this bag. * @exception OutOfMemoryError * Indicates insufficient memory a new BTNode. **/ public void add(E element) { // Implemented by student. } /** * Retrieve location of a specified element from this bag. * @param <CODE>target * the element to locate in the bag * @return * the return value is a reference to the found element in the tree *
Postcondition:
* If target was found in the bag, then method returns * a reference to a comparable element. If the target was not found then * the method returns null. * The bag remains unchanged. **/ public E retrieve(E target) { // Student will replace this return statement with their own code: return target; } /** * Remove one copy of a specified element from this bag. * @param <CODE>target * the element to remove from the bag *
Postcondition:
* If target was found in the bag, then one copy of * target has been removed and the method returns true. * Otherwise the bag remains unchanged and the method returns false. **/ public boolean remove(E target) { // Student will replace this return statement with their own code: return false; } /** * Displays the entire tree of Node elements in a order specified * by the elements compareTo method * * @param * none *
Postcondition:
* Outputs all elements in the tree to Screen. * Does not change the structure **/ public void display() { // Student will replace this with their own code: } /** * Displays the entire tree of Node elements using the * built in print method of BTNode * which displays the entire tree in tree format * * @param * none *
Postcondition:
* Outputs all elements in the tree to Screen. * Does not change the structure **/ public void displayAsTree() { root.print(0); } /** * Generate a copy of this bag. * @param - none * @return * The return value is a copy of this bag. Subsequent changes to the * copy will not affect the original, nor vice versa. Note that the return * value must be type cast to an TreeBag before it can be used. * @exception OutOfMemoryError * Indicates insufficient memory for creating the clone. **/ public TreeBag clone( ) { // Clone an IntTreeBag object. // Student will replace this return statement with their own code: return null; } /** * Accessor method to count the number of occurrences of a particular element * in this bag. * @param <CODE>target * the element that needs to be counted * @return * the number of times that target occurs in this bag **/ public int countOccurrences(E target) { // Student will replace this return statement with their own code: return 0; } /** * Determine the number of elements in this bag. * @param - none * @return * the number of elements in this bag **/ public int size( ) { return BTNode.treeSize(root); } /** * Add the contents of another bag to this bag. * @param <CODE>addend * a bag whose contents will be added to this bag *
Precondition:
* The parameter, addend, is not null. *
Postcondition:
* The elements from addend have been added to this bag. * @exception IllegalArgumentException * Indicates that addend is null. * @exception OutOfMemoryError * Indicates insufficient memory to increase the size of the bag. **/ public void addAll(TreeBag addend) { // Implemented by student. } /** * Create a new bag that contains all the elements from two other bags. * @param <CODE>b1 * the first of two bags * @param <CODE>b2 * the second of two bags *
Precondition:
* Neither b1 nor b2 is null. * @return * the union of b1 and b2 * @exception IllegalArgumentException * Indicates that one of the arguments is null. * @exception OutOfMemoryError * Indicates insufficient memory for the new bag. **/ public static TreeBag union(TreeBag b1, TreeBag b2) { // Student will replace this return statement with their own code: return null; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!