Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part IV: Tree Sort Driver Download the following files: TreeSortDriver.java Keyboard.java make.bat cds.txt Complete the provided driver to test your work. You may want to

Part IV: Tree Sort Driver Download the following files: TreeSortDriver.java

Keyboard.java

make.bat

cds.txt

Complete the provided driver to test your work. You may want to write a make.bat file that includes the classpath information that you need for the jar file. You will also need to refer to the FileIO documentation (see the top of the lab). Use try-catch to continue to prompt for a valid file until the end user enters one.

//TreeSortDriver.java

import java.util.ArrayList;

public class TreeSortDriver {

private static CD[] readMusic(String fileName) { //DO THIS complete this method using the FileIO class FileIO file = String str = ArrayList cds = new ArrayList();

while ( ) { String title = int year = Integer.parseInt( ); int rating = Integer.parseInt( ); int numTracks = Integer.parseInt( ); CD cd = new CD(title, str, year, rating, numTracks);

cds.add(cd); int tracks = 1;

while (tracks <= numTracks) { String track_str = String[] pieces = track_str.?? //divide the string up into two pieces String len = pieces[0]; String songTitle = pieces[1]; cd.addSong(songTitle, len); tracks++; }

str = }

//create a CD[] of the correct size, populate it using a for-each statement CD[] cds_array =

return cds_array; } public static void main (String[] args) { //use the Keyboard class, try-catch, and a while loop to continue calling readMusic //until a valid file name is entered //as checked exceptions have been converted to unchecked exceptions, //you must remember to do this with end user input, the compiler will not help you

//once you have the array of CDs back from readMusic, sort them //and print them out to make sure that they are sorted

} }

//Keyboard.java

** * A class to simplify keyboard input. * Abstracts the Scanner class provided in the Java SDK. */ public class Keyboard { private static Keyboard kb = new Keyboard();

/** The SDK provided Scanner object, used to obtain keyboard input */ private java.util.Scanner scan;

private Keyboard() { scan = new java.util.Scanner(System.in); }

public static Keyboard getKeyboard() { return kb; } /** * Reads an integer from the keyboard and returns it. * Uses the provided prompt to request an integer from the user. */ public int readInt(String prompt) { System.out.print(prompt); int num = 0;

try { num = scan.nextInt(); readString(""); //clear the buffer } catch (java.util.InputMismatchException ime) //wrong type inputted { readString(""); //clear the buffer num = 0; } catch (java.util.NoSuchElementException nsee) //break out of program generates an exception { readString(""); //clear the buffer num = 0; } return num; }

/** * Reads a double from the keyboard and returns it. * Uses the provided prompt to request a double from the user. */ public double readDouble(String prompt) { System.out.print(prompt); double num = 0.0;

try { num = scan.nextDouble(); readString(""); //clear the buffer } catch (java.util.InputMismatchException ime) { readString(""); //clear the buffer num = 0; } catch (java.util.NoSuchElementException nsee) { readString(""); //clear the buffer num = 0; }

return num; }

/** * Reads a line of text from the keyboard and returns it as a String. * Uses the provided prompt to request a line of text from the user. */ public String readString(String prompt) { System.out.print(prompt); String str = "";

try { str = scan.nextLine(); } catch (java.util.NoSuchElementException nsee) { readString(""); //clear the buffer str = ""; }

return str; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions