For this assignment, you will start with your CDInfo.java and CDDBMgr.java files from homework 10, and with my additional hw11 starting point files. You will implement the CD Database assignment using a Binary Search Tree to store the database of CDInfo rather than using a linked list. In addition, you will add two new menu options to your program.
% cd cs21 % mkdir hw11 % cd hw11 % cp ../hw10/CDInfo.java . % cp ../hw10/CDDBMgr.java . % cp ~newhall/public/cs21/hw11/* .
Here is more detailed information of what you need to do:
Since there may be duplicate keys in the BST (multiple entries for the same artist name), you should order BSTNodes such that Nodes in a node's left subtree are strictly less than it, and nodes in its right subtree are greater than OR EQUAL to it (duplicates go right).
If the search succeeds, then print out the single matching CDInfo object that is returned, otherwise print out a message indicating that there is no matching artist in the DB To do this, first implement the find method of the BST class so that returns a ref to the first matching element it finds...you should call the compareTo method on the data fields since you changed it to compare only by artist name).
root's left child's right child, d = 2 root's left child node, d = 1 root's left child's left child, d = 2 root node, d = 0 root's right child's left child, d = 2 root's right child node d = 1 root's right child's right child, d = 2
To the BST class you will need to add methods that do a full traversal of the BST, and that print out matching items as you go (this is slightly different than how you did it in the LinkedList version where the LinkedList class just returned a list of matches and the CDDMgr called the printList method out the answer list to print out the results).
For example, to the BST class you could add a method named printAllMatchingArtists that takes a String (or a CDInfo ref) with an artist name, and performs a full traversal of the BST printing out the value of any CDInfo nodes with matching artist fields as you go (remember that you will need to re-cast the BSTNode data field to be a ref to a CDInfo object inorder to invoke CDInfo-specific methods on the data field).
After the call to PerformActions in main returns make a call to a method SaveToFile like this:
SaveToFile(cds, args[0] + "_new");This is what the shell of this CDDMgr method looks like (note that it makes a call to the writeToFile method of the BST class that you need to implement):
public static void SaveToFile(BST cds, String outfile) throws IOException { // create a new FileWriter object to write data to the // file with the name of the outfile String: FileWriter f = new FileWriter(new File(outfile)); // call a BST method that traverses the BST in some order // and writes out the data field contents to the passed file cds.writeToFile(f); // always close the file after you are done writing to it f.close(); }The BST method that does this traversal and writing needs to know that the data fields are refs to CDInfo objects (i.e. you need to recast the value in the data field of the BSTNode to be a CDInfo object ref before you can invoke its getAlbum or GetArtist methods:
CDInfo data = (CDInfo)curr.getData(); f.write(data.getArtist() + "\n"); f.write(data.getAlbum() + "\n"); f.write(String.valueOf(data.getDate()) + "\n");
% java CDDBMgr hw11.txt There are 21 CDS. **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 4 Al Green, greatest hits , 2005 Bob Marley, Survival, 1979 Charles Mingus, Mingus Ah Um, 1959 De La Soul, 3 Feet High and Rising, 1989 De La Soul, De La Soul Is Dead, 1991 James Brown, Get on the Good Foot, 1972 Killdozer , For Ladies Only, 1989 MC5, Kick Out the Jams, 1969 Patti Smith, Horses, 1975 Public Enemy, It Takes a Nation of Millions to Hold Us Back, 1988 Ramones, Greatest Hits, 1990 Sleater-Kinney, Dig Me Out, 1997 Sleater-Kinney, One Beat, 2002 Sly & the Family Stone, Fresh, 1973 Sly & the Family Stone, Greatest hits, 1970 Spot 1019, Spot 1019, 1986 The Fall , The Infotainment Scan, 1993 X, Under the Big Black Sun, 1982 X, More Fun in the New World, 1983 X , Los Angeles, 1980 sly & the family stone, There's a Riot Goin' On, 1971 ----------------------------------------------- There are 21 CDs **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 7 Al Green, greatest hits , 2005 d = 3 Bob Marley, Survival, 1979 d = 2 Charles Mingus, Mingus Ah Um, 1959 d = 4 De La Soul, 3 Feet High and Rising, 1989 d = 3 De La Soul, De La Soul Is Dead, 1991 d = 4 James Brown, Get on the Good Foot, 1972 d = 1 Killdozer , For Ladies Only, 1989 d = 3 MC5, Kick Out the Jams, 1969 d = 4 Patti Smith, Horses, 1975 d = 2 Public Enemy, It Takes a Nation of Millions to Hold Us Back, 1988 d = 0 Ramones, Greatest Hits, 1990 d = 3 Sleater-Kinney, Dig Me Out, 1997 d = 2 Sleater-Kinney, One Beat, 2002 d = 3 Sly & the Family Stone, Fresh, 1973 d = 1 Sly & the Family Stone, Greatest hits, 1970 d = 4 Spot 1019, Spot 1019, 1986 d = 3 The Fall , The Infotainment Scan, 1993 d = 4 X, Under the Big Black Sun, 1982 d = 2 X, More Fun in the New World, 1983 d = 3 X , Los Angeles, 1980 d = 4 sly & the family stone, There's a Riot Goin' On, 1971 d = 5 ------------------------------------- **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 5 Enter the artist's name: Public Enemy Enter the album title: Fear of a Black Planet Enter a release year: 1990 **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 7 Al Green, greatest hits , 2005 d = 3 Bob Marley, Survival, 1979 d = 2 Charles Mingus, Mingus Ah Um, 1959 d = 4 De La Soul, 3 Feet High and Rising, 1989 d = 3 De La Soul, De La Soul Is Dead, 1991 d = 4 James Brown, Get on the Good Foot, 1972 d = 1 Killdozer , For Ladies Only, 1989 d = 3 MC5, Kick Out the Jams, 1969 d = 4 Patti Smith, Horses, 1975 d = 2 Public Enemy, It Takes a Nation of Millions to Hold Us Back, 1988 d = 0 Public Enemy, Fear of a Black Planet, 1990 d = 4 Ramones, Greatest Hits, 1990 d = 3 Sleater-Kinney, Dig Me Out, 1997 d = 2 Sleater-Kinney, One Beat, 2002 d = 3 Sly & the Family Stone, Fresh, 1973 d = 1 Sly & the Family Stone, Greatest hits, 1970 d = 4 Spot 1019, Spot 1019, 1986 d = 3 The Fall , The Infotainment Scan, 1993 d = 4 X, Under the Big Black Sun, 1982 d = 2 X, More Fun in the New World, 1983 d = 3 X , Los Angeles, 1980 d = 4 sly & the family stone, There's a Riot Goin' On, 1971 d = 5 ------------------------------------- **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 4 Al Green, greatest hits , 2005 Bob Marley, Survival, 1979 Charles Mingus, Mingus Ah Um, 1959 De La Soul, 3 Feet High and Rising, 1989 De La Soul, De La Soul Is Dead, 1991 James Brown, Get on the Good Foot, 1972 Killdozer , For Ladies Only, 1989 MC5, Kick Out the Jams, 1969 Patti Smith, Horses, 1975 Public Enemy, It Takes a Nation of Millions to Hold Us Back, 1988 Public Enemy, Fear of a Black Planet, 1990 Ramones, Greatest Hits, 1990 Sleater-Kinney, Dig Me Out, 1997 Sleater-Kinney, One Beat, 2002 Sly & the Family Stone, Fresh, 1973 Sly & the Family Stone, Greatest hits, 1970 Spot 1019, Spot 1019, 1986 The Fall , The Infotainment Scan, 1993 X, Under the Big Black Sun, 1982 X, More Fun in the New World, 1983 X , Los Angeles, 1980 sly & the family stone, There's a Riot Goin' On, 1971 ----------------------------------------------- There are 22 CDs **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 6 Enter an the artist's name: X CDs by X do appear in the database, here is one of them: X, Under the Big Black Sun, 1982 -------------------------------------------- **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 1 Enter the name of the artist: X CDs by X: -------------------------------------------- X, Under the Big Black Sun, 1982 X, More Fun in the New World, 1983 X , Los Angeles, 1980 -------------------------------------------- **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 2 Enter a CD title: greatest hits CDs named greatest hits: -------------------------------------------- Al Green, greatest hits , 2005 Ramones, Greatest Hits, 1990 Sly & the Family Stone, Greatest hits, 1970 -------------------------------------------- **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 1 Enter the name of the artist: de la soul CDs by de la soul: -------------------------------------------- De La Soul, 3 Feet High and Rising, 1989 De La Soul, De La Soul Is Dead, 1991 -------------------------------------------- **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 8 bye, bye % java CDDBMgr hw11.txt_new There are 22 CDS. **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 4 Al Green, greatest hits , 2005 Bob Marley, Survival, 1979 Charles Mingus, Mingus Ah Um, 1959 De La Soul, 3 Feet High and Rising, 1989 De La Soul, De La Soul Is Dead, 1991 James Brown, Get on the Good Foot, 1972 Killdozer , For Ladies Only, 1989 MC5, Kick Out the Jams, 1969 Patti Smith, Horses, 1975 Public Enemy, It Takes a Nation of Millions to Hold Us Back, 1988 Public Enemy, Fear of a Black Planet, 1990 Ramones, Greatest Hits, 1990 Sleater-Kinney, Dig Me Out, 1997 Sleater-Kinney, One Beat, 2002 Sly & the Family Stone, Fresh, 1973 Sly & the Family Stone, Greatest hits, 1970 Spot 1019, Spot 1019, 1986 The Fall , The Infotainment Scan, 1993 X, Under the Big Black Sun, 1982 X, More Fun in the New World, 1983 X , Los Angeles, 1980 sly & the family stone, There's a Riot Goin' On, 1971 ----------------------------------------------- There are 22 CDs **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 1 Enter the name of the artist: sly & the family STONE: CDs by sly & the family STONE: -------------------------------------------- Sly & the Family Stone, Fresh, 1973 Sly & the Family Stone, Greatest hits, 1970 sly & the family stone, There's a Riot Goin' On, 1971 -------------------------------------------- **** MENU OPTIONS **** 1) to see all works by a given artist 2) to see all albums with a given title 3) to see all albums released in a give year 4) to see the complete list 5) to add a new CD to the collection 6) to see if an particular artist is in the database 7) to print out the BST of CDInfo (only do this for small BSTs) 8) to quit *********************** Enter your choice: 8 bye, bye