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

Using Java: The attached java class uses an array for holding a collection of bo

ID: 3572111 • Letter: U

Question

Using Java:

The attached java class uses an array for holding a collection of book names. Please modify this class replacing the books array with an ArrayList using the same name books. The list array in the main method does not need to change, it's strictly used for assigning names of books to the 2 members.

My advice would be to download the file, compile it and run it to see the output it generates. After you've replaced the array with an ArrayList the output generated should be the same.

Member.java




Explanation / Answer

import java.util.ArrayList;

public class Member{

private int id;
private String name;
//private String[] books = new String[5];
private ArrayList<String> books= new ArrayList<>();// creating array list of String data type
//replace the String books array with an ArrayList variable named books
//that functions exactly the same way

public Member() {
id = 0;
name = "John Doe";
}
  
public Member(int pId, String pName) {
id = pId;
name = pName;
}
  
public int getId(){
return id;
}
  
public void setId(int pId){
id=pId;
}
  
public String getName(){
return name;
}
  
public void setName(String pName){
name=pName;
}
//note the return type of getBooks as ArrayList
public ArrayList<String> getBooks(){
return books;
}
  
//adding the books to the arrayList
public void setBooks(String[] pBooks){
  
//books=pBooks;
   for(int i=5; i<pBooks.length; i++) {
       books.add(pBooks[i]);
       }
}

public String toString(){
StringBuffer buf = new StringBuffer();
buf.append("ID: " + getId() + " Member: " + getName() + " ");
for(int x=0; x<books.size(); x++ ) {
buf.append("Book: " + books.get(x) + " ");
}
return buf.toString();
}

  
/**
* @param args
*/
public static void main(String[] args) {
String[] list = {"Java Data Structures", "The Bible", "Grapes of Wrath",
"Goldfinger", "Sam I Am", "The Cat in the Hat", "Shawshenk Redemption",
"Green Eggs and Ham", "Linus and Lucy", "Abraham Lincoln"};
  
Member m1 = new Member();
m1.setId(431);
m1.setName("William Wallace");
for(int i=0; i<5; i++) {
   m1.books.add(list[i]);//changes to add to the ArrayList instead of array
//m1.books[i] = list[i];
}

Member m2 = new Member(7010, "Bonny Clyde");
for(int i=5; i<list.length; i++) {
int x = i-5;
//m2.books[x] = list[i];
m2.books.add(list[i]);//changes to add to the ArrayList instead of array
}

System.out.println(m1.toString());
System.out.println();
System.out.println(m2.toString());
}

}

-----output------

ID: 431 Member: William Wallace
Book: Java Data Structures
Book: The Bible
Book: Grapes of Wrath
Book: Goldfinger
Book: Sam I Am


ID: 7010 Member: Bonny Clyde
Book: The Cat in the Hat
Book: Shawshenk Redemption
Book: Green Eggs and Ham
Book: Linus and Lucy
Book: Abraham Lincoln

-----------output-----------

Note: Feel free to ask question/doubts... God bless you!!

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