Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// class BookGenre package library.service.classes; public enum BookGenre {GENRE_HISTORY,GENRE_SCIENCE,GENRE_ENGINEERING,GENRE_LITERATURE}; // class BookRecord package library.service.classes;//set up the package import library.service.classes.BookGenre; public class BookRecord{ int recordNo; String

image text in transcribed

image text in transcribed

// class BookGenre

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

// class BookRecord

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

import library.service.classes.BookGenre;

public class BookRecord{

int recordNo;

String title;

String []authors;

BookGenre genre;

static int cnt=10000;

/on-default constructor

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

this.setTitle(title);

this.setGenre(genre);

this.setAuthors(name);

this.setRecordNo();

}

//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;

}

//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];

}

}

//toString method

public String toString(){

String str="";

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

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

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

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

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

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

return str;

}

//toEquals method

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

if(!this.getTitle().equals(aRecord.getTitle()))

return false;

if(!this.equalsHelper(aRecord))

return false;

return true;

}

private boolean equalsHelper(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;

}

}

// class library

package library.client.classes; import java.util.Scanner; import java.io.File; import java.io.IOException; import library.service.classes.BookGenre; import library.service.classes.BookRecord; import java.util.Vector; class library{ /*Task 1: Declare the vector of objects*/ Vector books; library(){ /* Task 2: Initialize vector*/ books = new Vector(); /* Task 3: Print out its capacity and its size*/ System.out.println("capacity = "+books.capacity()+" Size = "+books.size()); } public void searchByGenre(BookGenre genre){//search the vector for records of specific genre /* Task 5: implement searchByGenre() to search for records of a particular genre in the vector*/ if(books.isEmpty()){ return; } for (BookRecord book : books) { if (book.getGenre() == genre) { System.out.println(book); } } } public void print(){//list the records /* Task 6: Implement the print() to list out the records*/ if(books.isEmpty()){ return; } System.out.println(); for(int i = 0; i getRecord(){ return books; } public void findNum() { } public static void main(String []args){//instantiate the library //args[0]: text file library myLib = new library(); //read the the files from text files String []authors; BookRecord aRecord; Scanner scan; String str; 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); /*Task 4: Add the objects to the vector*/ myLib.add(aRecord); } 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); option1 = ""; while(!option1.equals("Q")){ 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 \"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 user will type in a valid genre myLib.searchByGenre(BookGenre.valueOf(option2));//implement this method break; case "P": myLib.print();//print the array; implement this method break; case "Q": System.out.println("Quitting interactive part"); break; default: System.out.println("Wrong option! Try again"); break; } } /*Task 7- Create 2 dimensional array using the records from the vector The array has rows for GENRE_HISTORY, GENRE_SCIENCE, GENRE_ENGINEERING, GENRE_LITERATURE*/ BookRecord [][] lib; //declaration of the 2D array lib = new BookRecord[4][]; int num = 0; for (BookGenre d : BookGenre.values()) {//for each genre count the number of records belonging to it and then instantiate its row /*add code here*/ } } System.out.println("Printing out the array. Type enter to proceed"); option1=scan.nextLine(); /*Task 8: Print out the array*/ for(int i = 0; i/* Task 9: Identify and remove the duplicate records IN THE VECTOR (NOT THE ARRAY) and print out the removed records */ Vector vec = myLib.getRecord(); BookRecord b; for(int i = 0; i/* Task 10: print out the array again; are the duplicated printed out again? */ for(int i = 0; i

//books.txt

Thomas Jefferson and the Tripoli Pirates:GENRE_HISTORY:Brian Kilmeade,Don Yaeger Component-oriented programming:GENRE_ENGINEERING:C. Szyperski,J. Bosch,W. Weck Microfabricated microneedles, a novel approach to transdermal drug delivery:GENRE_ENGINEERING:S. Henry,D. V. McAllister,M. G. Allen Nikola Tesla:GENRE_HISTORY:Sean Patrick English landscaping and literature, 1660-1840:GENRE_LITERATURE:E. Malins A history and theory of informed consent:GENRE_HISTORY:R. R. Faden,T. L. Beauchamp,N. M. King The Feminist Companion to Literature in English Women Writers From the Middle Ages to the Present:GENRE_LITERATURE:V. Blain,P. Clements,I. Grundy 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 The comparative method in evolutionary biology:GENRE_SCIENCE:P. H. Harvey,M. D. Pagel Human-computer interaction:GENRE_ENGINEERING:J. Preece,Y. Rogers,H. Sharp,D. Benyon,S. Holland Free radicals in biology and medicine:GENRE_SCIENCE:B. Halliwell,J. M. C. Gutteridge Electron transfers in chemistry and biology:GENRE_SCIENCE:R. A. Marcus,N. Sutin English landscaping and literature, 1660-1840:GENRE_LITERATURE:E. Malins Device electronics for integrated circuits:GENRE_ENGINEERING:R. S. Muller,T. I. Kamins,M. Chan,P. K. Ko An outline of English literature:GENRE_LITERATURE:G. C. Thornley,G. Roberts Gene Ontology:GENRE_SCIENCE:M. Ashburner,C. A. Ball,J. A. Blake,D. Botstein,H. Butler Nikola Tesla:GENRE_HISTORY:Sean Patrick Microfabricated microneedles, a novel approach to transdermal drug delivery:GENRE_ENGINEERING:S. Henry,D. V. McAllister,M. G. Allen

A. EXERCISE Use the java files in the accompanying zip file. We will be using the BookRecord object from the HW2. In HW2, you used an array to store objects. You will modifv the service class to use a Vector of obiects instead of the array. You will need to modify the "library.java" file. See the comments in this java file tagged with the word "Task" -they specify the location where you need to make the modifications for the programming tasks listed below. Modify the client class in "library. Java" and the make the following changes Task 1: Declare the Vector of objects Task 2: Initialize the Vector in the class constructor Task 3: Print out its capacity and its size in the class constructor. Do they have the same value? Task 4: After reading the data from the text file "books.txt", BookRecord objects are created. Now add the objects to the vector. DON'T WORRY ABOUT DUPLICATE RECORDS AT THIS POINT. WE WILL DEAL WITH THEM LATER. Task 5: Option "S" searches for records of a particular genre in the vector. Modify the searchByGenre() method to implement this feature. Task 6: Option "P" will list out all the records in the Vector. Implement print() to list out the records

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_2

Step: 3

blur-text-image_3

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

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago

Question

Write short notes on departmentation.

Answered: 1 week ago

Question

What are the factors affecting organisation structure?

Answered: 1 week ago

Question

What are the features of Management?

Answered: 1 week ago

Question

Briefly explain the advantages of 'Management by Objectives'

Answered: 1 week ago