Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the above exercise, the number of authors cannot be changed once a Book insta

ID: 666105 • Letter: I

Question

In the above exercise, the number of authors cannot be changed once a Book instance is constructed. Suppose that we wish to allow the user to add more authors (which is really unusual but presented here for academic purpose). Instead of using a fixed-length array in this case, it is better to be a dynamically allocated array (e.g., ArrayList), which does not have a fixed length. Modify the Book class. You should: o Modify the authors array instance variable to an ArrayList o remove the authors from the parameter of the constructors. (don't forget to create a new instance of the authors ArrayList inside the constructors. add a new method called addAuthor() to add the given Author instance to this Book. modify the printAuthors to print the names of all the authors from an ArrayList Write the Book class in the answer box below assuming that the Author class has been done for you in the system. For example: Test Result Book java Dummy1 new Book ("Java for Dummy 19.99 99) Java for Dummy by 0 authors System.out -println (javaDummy1) Book java Dummy new Book Java for Dummy 19.99 99) Java for Dummy by 2 authors java Dummy.addAuthor (new Author "Tan Ah Teck AhTecka somewhere .com 'm')) java Dummy addAuthor (new Author ("Paul Tan Paul@nowhere. com m') System. out println (javaDummy) Book java Dummy2 new Book ("Java for Dummy 19.99) Java for Dummy by 2 authors java Dummy2.addAuthor (new Author ("Tan Ah Teck AhTecka somewhere com m') java Dummy2.addAuthor (new Author Paul Tan Paul@nowhere com. m') System.out..println (javaDummy2) Answer: nz/moodle mod quiz/summary php? attemp 28144 uckland

Explanation / Answer

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

import java.util.ArrayList;
import java.util.*;

class Book
{
ArrayList al= new ArrayList();
Scanner sr = new Scanner(System.in);
System.out.println("initial Size is"+al.size());
   String author;
public String addAuthor(){
al.add(author);
}
public String viewAuthor(){
Iterator itr=al.iterator();//getting Iterator from arraylist to traverse elements
while(itr.hasNext()){
   System.out.println(itr.next());
}
}
   System.out.println("authors inerted then size is"+al.size());

   public static void main(String[] args)
   {
       Book b= new Book();

       System.out.println("enter author name");
       b.addAuthor();
       author = sr.nextLine();
       b.viewAuthor();
       //System.out.println("Hello World!");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote