Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

books.txt Thomas Jefferson and the Tripoli Pirates:GENRE_HISTORY:Brian Kilmeade,Don Yaeger:ABEW2345:234 Component-oriented programming:GENRE_ENGINEERING:C. Szyperski,J. Bosch,W. Weck:HGNH4567:456 Microfabricated microneedles, a novel approach to transdermal drug delivery:GENRE_ENGINEERING:S. Henry,D. V.

image text in transcribedimage text in transcribedimage text in transcribedbooks.txt

Thomas Jefferson and the Tripoli Pirates:GENRE_HISTORY:Brian Kilmeade,Don Yaeger:ABEW2345:234 Component-oriented programming:GENRE_ENGINEERING:C. Szyperski,J. Bosch,W. Weck:HGNH4567:456 Microfabricated microneedles, a novel approach to transdermal drug delivery:GENRE_ENGINEERING:S. Henry,D. V. McAllister,M. G. Allen:HJKG2342:378 Nikola Tesla:GENRE_HISTORY:Sean Patrick:NJKG7456:987 English landscaping and literature, 1660-1840:GENRE_LITERATURE:E. Malins:MNBV3456:980 A history and theory of informed consent:GENRE_HISTORY:R. R. Faden,T. L. Beauchamp,N. M. King:HJGF7645:654 The Feminist Companion to Literature in English Women Writers From the Middle Ages to the Present:GENRE_LITERATURE:V. Blain,P. Clements,I. Grundy:JHGF9089:767 Climate and atmospheric history of the past 420,000 years:GENRE_HISTORY:J. R. Petit, J. Jouzel,D. Raynaud,N. I. Barkov,J. M. Barnola:FDST9878:675 The comparative method in evolutionary biology:GENRE_SCIENCE:P. H. Harvey,M. D. Pagel:BGHF8976:234 Human-computer interaction:GENRE_ENGINEERING:J. Preece,Y. Rogers,H. Sharp,D. Benyon,S. Holland:UYHG1223:889 Free radicals in biology and medicine:GENRE_SCIENCE:B. Halliwell,J. M. C. Gutteridge:HGHB8909:234 Electron transfers in chemistry and biology:GENRE_SCIENCE:R. A. Marcus,N. Sutin:LKJH2345:890 English landscaping and literature, 1660-1840:GENRE_LITERATURE:E. Malins:MNBV3456:980 Device electronics for integrated circuits:GENRE_ENGINEERING:R. S. Muller,T. I. Kamins,M. Chan,P. K. Ko:JHKG2343:654 An outline of English literature:GENRE_LITERATURE:G. C. Thornley,G. Roberts:YUTY9098:89 Gene Ontology:GENRE_SCIENCE:M. Ashburner,C. A. Ball,J. A. Blake,D. Botstein,H. Butler:KJHG8909:90 Nikola Tesla:GENRE_HISTORY:Sean Patrick:NJKG7456:987 Microfabricated microneedles, a novel approach to transdermal drug delivery:GENRE_ENGINEERING:S. Henry,D. V. McAllister,M. G. Allen:HJKG2342:378 

//Sample output

image text in transcribed

//BookRecord Class

package library.service.classes;//set up the package

import library.service.classes.BookGenre;

public class BookRecord{

int recordNo;

String title;

String []authors;

BookGenre genre;

String tag;

int pages;

static int cnt=10000;

/on-default constructor

public BookRecord(String title, String genre, String [] name, String tag, int numPages){

this.setTitle(title);

this.setGenre(genre);

this.setAuthors(name);

this.setRecordNo();

this.setTag(tag);

this.setPages(numPages);

}

//accessors

public String getTitle(){

return this.title;

}

public BookGenre getGenre(){

return this.genre;

}

public String [] getAuthors(){

String []ret = new String[this.authors.length];

for(int i=0;i

ret[i]=this.authors[i];

}

return ret;

}

public String getAuthorList(){

String ret="";

for(int i=0;i

ret=ret + " " +this.authors[i];

}

return ret;

}

public int getRecordNo(){

return this.recordNo;

}

public String getTag() {

return this.tag;

}

public int getNumPages() {

return this.pages;

}

//mutators

public void setRecordNo(){

this.recordNo=BookRecord.cnt++;

}

public void setTitle(String title){

this.title=title;

}

public void setGenre(String genre){

this.genre=BookGenre.valueOf(genre);

}

public void setAuthors(String []authorList){

this.authors = new String[authorList.length];

for(int i=0;i

this.authors[i]=authorList[i];

}

}

public void setTag(String tag){

this.tag = tag;

}

public void setPages(int numPages) {

this.pages = numPages;

}

//toString method

public String toString(){

String str="";

str = str + "=================================== ";

str = str + "Tag: " + this.getTag() + " ";

str = str + "Record No:" + this.getRecordNo() + " ";

str = str + "Title:" + this.getTitle() + " ";

str = str + "Genre: " + this.getGenre() + " ";

str = str + "Authors: " + this.getAuthorList() + " ";

str = str + "No. of Pages: " + this.getNumPages() + " ";

str = str + "=================================== ";

return str;

}

//toEquals method

public boolean equals(BookRecord aRecord){//remember you should not compare the record no

if(!title.equals(aRecord.title))

return false;

if(!this.authorEqual(aRecord))

return false;

if(!genre.equals(aRecord.genre))

return false;

if(pages != aRecord.pages)

return false;

if(!tag.equals(aRecord.tag))

return false;

return true;

}

private boolean authorEqual(BookRecord aRecord){

for(int i=0;i

if(!this.authors[i].equals(aRecord.authors[i])){ //assume the authors will always be provided in the same order

return false;

}

}

return true;

}

}

//BookGenre Class

package library.service.classes; public enum BookGenre {GENRE_HISTORY,GENRE_SCIENCE,GENRE_ENGINEERING,GENRE_LITERATURE};

//Library class

package library.client.classes;

import java.util.Scanner;

import java.io.BufferedReader;

import java.io.File;

import java.io.*;

import java.io.IOException;

import library.service.classes.BookGenre;

import library.service.classes.BookRecord;

class library{

BookRecord []books; //array of objects

int noRecords=0; /o of records; it is not the size of the array

public void resize(int resizeFactor){//library to expand the array

//initiatilize a new array with larger size and then copy the variables to

int initSize = this.noRecords;

BookRecord [] newArray = new BookRecord[initSize+resizeFactor];

//copy the object over

for(int i=0;i

newArray[i]=this.books[i];

}

this.books=newArray;

System.out.println("Resized the array from " + initSize + " to " + this.books.length);

}

public void searchByGenre(BookGenre genre){

//loop through the book records and list the genre

for(int i=0;i

if(this.books[i].getGenre()==genre){

System.out.println(this.books[i].toString());

}

}

}

public void searchTag(String [] tags){

int length = tags.length;

for (int i = 0; i

for (int j = 0; j

if (tags[i].compareTo(tags[j]) > 0) {

String tag = tags[i];

tags[i] = tags[j];

tags[j] = tag;

}

}

}

}

public int tsearch(String tag, String [] tags, int a, int b) {

if (b

return -1;

}

if (b -a == 1) {

return tags[a].equals(tag) ? a : -1;

}

int pivot = (a+b)/2;

if (tag.compareTo(tags[pivot])

return tsearch (tag, tags, 0, pivot);

} else if (tag.compareTo(tags[pivot]) > 0) {

return tsearch (tag, tags, pivot, b);

}

return pivot;

}

public boolean searchForDuplicate(BookRecord aRecord){

//loop through the library and find duplicates

//return true if duplicate found

//else return false

if(this.noRecords==0) return false;

for(int i=0;i

if(this.books[i].equals(aRecord))

return true;

}

return false;

}

public void print(){//list the library

for(int i=0;i

System.out.println(this.books[i].toString());

}

}

library(){

this.books=new BookRecord[5];//intial size is 5

}

public BookRecord [] sortsString (BookRecord [] myArray, int noRecord) {

}

public BookRecord[ ] sortPages(BookRecord [] myArray, int noRecords) {

}

public static void main(String []args) throws IOException {//instantiate the library

//arg[0]: text file //arg[1]: resize factor

int resizeFactor = Integer.parseInt(args[1]);

library myLib = new library();

//read the the files from text files

String []authors;

BookRecord aRecord;

Scanner scan;

String str;

BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

String [] tags = new String[8];

for (int i = 0; i

tags[i] = br.readLine();

}

int index = tsearch (tag, tags, 0, tags.length);

if (index

System.out.println("No match found");

} else {

System.out.println();

}

try {

File myFile=new File(args[0]);

scan=new Scanner(myFile);//each line has the format title:genre:author-name-1,author-name-2..authorname-m

while(scan.hasNextLine()){

str=scan.nextLine();

String []tok=str.split(":");

authors=tok[2].split(",");

aRecord = new BookRecord(tok[0],tok[1],authors);

//check for duplicate records

if (!myLib.searchForDuplicate(aRecord)){

//create a BookRecord object and load it on the array

myLib.books[myLib.noRecords] = aRecord;

myLib.noRecords++;

//System.out.println("No of records: " + myLib.noRecords);

myLib.books[myLib.tags] = aRecord;

}

else{

System.out.println("Found a duplicate");

System.out.println(aRecord.toString());

}

//check if the array needs to resize

if(myLib.books.length == myLib.noRecords){ /eed to add additional space

myLib.resize(resizeFactor);

}

}

scan.close();

}catch(IOException ioe){

System.out.println("The file can not be read");

}

//User interactive part

String option1, option2;

scan = new Scanner(System.in);

while(true){

System.out.println("Select an option:");

System.out.println("Type \"S\" to list books of a genre");

System.out.println("Type \"P\" to print out all the book recors");

System.out.println("Type \"T\" to search for a record with specific tag");

System.out.println("Type \"Q\" to Quit");

option1=scan.nextLine();

switch (option1) {

case "S": System.out.println("Type a genre. The genres are:");

for (BookGenre d : BookGenre.values()) {

System.out.println(d);

}

option2=scan.nextLine(); //assume the use will type in a valid genre

myLib.searchByGenre(BookGenre.valueOf(option2));

break;

case "P": myLib.print();

break;

case "T":

case "Q": System.out.println("Quitting program");

System.exit(0);

default: System.out.println("Wrong option! Try again");

break;

}

}

}

}

2. Change the client class: Place the client class in library.client.classes. You will make the following changes to the client class a. Modify the file read functionality[O.25pt]: The file text "books.txt", provided along with the assignment has lines with the following format: title:genre:author-1,author-2,...author-m:tag:no-of-pages Modify the code from HW 2 so that it also reads in the tag and no. of pages, along with the other values, and then creates the BookRecord objects. Remember that the text file may have duplicate records (records with the same title, authors genre, tag and page length). As with HW 2, your code should filter out the duplicate records. b. Implement a class method called sortString() with the following signature:[0.75pt] public BookRecord [I sortString(BookRecord [I myArray, int noRecords) The method takes in the array of BookRecord object you wish to sort and also the number of objects in the array. You will implement the selection sort algorithm in this method to sort the BookRecord object array. Once the books.txt file has been read and the array of BookRecord objects has been created, you will call the sortString() method. This method will use selection sorting algorithm to re-arrange the BookRecord objects in the array in a lexicographic increasing order with respect to their tag values. Hint: Lexicographic selection sorting is not difficult as long you can figure out how to implement the comparisons of two strings. Go through the Java APl for String class and figure out the method that you can use to do String comparisons An important point to be noted is that the array of BookRecord objects may not be completely filled. Therefore you need to set the indexes of the sorted and unsorted sub-arrays in your selection sorting algorithm appropriately. c. Implement a second class method called sortPages() with the following signature [0.5pt] public BookRecordI ] sortPages(BookRecord [I myArray, int noRecords) This method will take in an array of BookRecord objects and sort the objects in an increasing order with respect to their page length values. You will use a selection sort algorithm to accomplish this. The purpose for implementing this method is explained below d. Implement a third class method called searchTag) with the following signature [0.75pt]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

What is the preferred personality?

Answered: 1 week ago