## Amdocs Interview Question

- 0of 0 votes

AnswerDefine the following classes

- ruhi July 30, 2014 in United States

- Book which has the fields title(String), author (String), count (int). Also define the following function in the class

increment() : increases count by 1.

decrement() : reduces the count by 1. If count is 0, it remains 0.

- Library which has a field books (of type Book[]). It also has the following functions

getCount(String title) : given the title of a book as parameter, return how many copies of the book are there in the library

getCount(Book book) : given the object of type Book as parameter, return how many copies are there of that book in the library

getCount() : returns the total number of books in the library

getDiffCount() : returns the number of different books in the library

addBook(Book book) : given an object of type Book as input, increment its count if it is in the library else adds the book.

removeBook(Book book) : given an object of type Book as input, decreases the count of that book if it exists in the library. If the count becomes 0, it is removed.| Report Duplicate | Flag | PURGE

Amdocs

**Country:**United States

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

public class Book {

- Anonymous November 01, 2014String title;

String author;

int count;

public Book()

{

title="";

author="";

count=0;

}

public Book(String title,String author)

{

this.title=title;

this.author=author;

count=1;

}

public Book(String title,String author,int count)

{

this.title=title;

this.author=author;

this.count=count;

}

public void increment()

{

count++;

}

public void decrement()

{

if(count>0)

count--;

}

}

public class Library {

Book[] books;

public Library()

{

books=new Book[0];

}

public Library(Book[] books)

{

this.books=books;

}

public int getCount(String title)

{

//given the title of a book as parameter, return how many copies of the book are there in the library

int count=0;

for(int i=0;i<books.length;i++)

{

if(books[i].title.equalsIgnoreCase(title));

{

count= books[i].count;

break;

}

}

return count;

}

public int getCount(Book book)

{

//given the object of type Book as parameter, return how many copies are there of that book in the library

int count=0;

for(int i=0;i<books.length;i++)

{

if(books[i].title.equalsIgnoreCase(book.title)&&(books[i].author.equalsIgnoreCase(book.author)));

{

count= books[i].count;

break;

}

}

return count;

}

public int getCount()

{

//returns the total number of books in the library

int count=0;

for(int i=0;i<books.length;i++)

{

count+=books[i].count;

}

return count;

}

public int getDiffCount()

{

//returns the number of different books in the library

return books.length;

}

public void addBook(Book book)

{

//given an object of type Book as input, increment its count if it is in the library else adds the book.

int pos=-1;

for(int i=0;i<books.length;i++)

{

if(books[i].title.equalsIgnoreCase(book.title)&&(books[i].author.equalsIgnoreCase(book.author)))

{

pos=i;

books[i].count+=book.count;

break;

}

}

if(pos==-1)

{

Book[] newbooks=new Book[books.length+1];

int i;

for(i=0;i<books.length;i++)

{

newbooks[i]=books[i];

}

newbooks[i]=book;

books=newbooks;

}

}

public void removeBook(Book book)

{

boolean flag=false;

int i;

for(i=0;i<books.length;i++)

{

if(books[i].title.equalsIgnoreCase(book.title)&&(books[i].author.equalsIgnoreCase(book.author)))

{

books[i].count-=book.count;

if(books[i].count<=0)

flag=true;

break;

}

}

if(flag)

{

Book[] newbooks=new Book[books.length-1];

for(int j=0,k=0;k<newbooks.length;j++,k++)

{

if(j==i)

j++;

newbooks[k]=books[j];

}

books=newbooks;

}

}

}