Here is a simplified class for representing books owned by l

 Here is a simplified class for representing books owned by library:     
 class Book {   String title, author; // title, author   String callNumber; // cal! number, such as V\'QA567.23 P23\"   } 
    
 Suppose that a library owns 27,532 books and that information about all the books has already been stored in an array     
 Bookt[]books=new bookst[27532];     
 a) A science book is one whose call number begins with the letter Write a method that will count the number of science books owned by the library and return that number.   HINT there is a string method called charAt().     
 b) Write another method that wilt print out the answer to this question: Does the library own more books by \"Isaac Asimov\" or more books by \'William Shakespeare\"? Write a code segment that will find out. Print the answer with System.out.printin().    

Solution

//below code demonstrates the methods you want
//copy the complete answer and save it as Library.java and run it to check the output.

//i have highlighted the methods that you have asked
class Book {
String title, author;
String callNumber;
Book(String title,String author,String callNumber)
{
this.author=author;
this.title=title;
this.callNumber=callNumber;
}
String author()
{
return author;
}
String title()
{
return title;
}
String callNumber()
{
return callNumber;
}
}

public class Library {
public static void main(String args[])
{ //i am initalising some books to check the methods
Book[] books=new Book[5];
books[0]=new Book(\"hgsgsd\",\"Isaac Asimov\",\"CX67356\");
books[1]=new Book(\"hgsgsd\",\"Isaac Asimov\",\"DX67356\");
books[2]=new Book(\"hgsgsd\",\"William Shakespeare\",\"67356XX\");
books[3]=new Book(\"hgsgsd\",\"Isaac Asimov\",\"Xu67356\");
books[4]=new Book(\"hgsgsd\",\"William Shakespeare\",\"FX67356\");
System.out.println(\"Science books are:\"+numScience(books));
System.out.println(moreBooks(books,\"Isaac Asimov\",\"William Shakespeare\"));
  
}
static int numScience(Book[] books)
{
int count=0; //counting the books whose call number starts with
letter.
for (Book book : books) {
if (Character.isLetter(book.callNumber().charAt(0))) {
count++;
}
}
return count;
}
static String moreBooks(Book[] books,String author1,String author2)
{
int au1=0,au2=0; //count variables for authors
for (Book book : books) {
if (book.author().equalsIgnoreCase(author1)) { //compare author 1 and 2 names
au1++;
}
else if(book.author().equalsIgnoreCase(author2))
{
au2++;
}
}
if(au1>au2) //check which author have more books
return \"Author havinng more books is\"+author1;
else if(au2>au1)
return \"Author havinng more books is\"+author2;
else if(au1==0&&au2==0)
return \"Both have no books\"; //if both have no books
else
return \"Both having
same number of books\"; //if both have same number of books
}
  
}

/*
example output
Science books are:4
Author having more books isIsaac Asimov
*/

 Here is a simplified class for representing books owned by library: class Book { String title, author; // title, author String callNumber; // cal! number, such
 Here is a simplified class for representing books owned by library: class Book { String title, author; // title, author String callNumber; // cal! number, such

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site