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

JAVA Using Scanner and the for loop statement, define a method that reads in the

ID: 3875650 • Letter: J

Question

JAVA

Using Scanner and the for loop statement, define a method that reads in the ISBN of a book and deletes the corresponding Book from the ArrayList. The method should return true if the book is deleted and false if it is not (if a book with the ISBN does not exist in the ArrayList). NOTE: Do not change anything in the class provided.

public class Book {

   private String title;

   private String author;

   private String ISBN;

   private double RRP;

   public Book(String t, String a, String ISBN, double RRP) {

       this.title = t;

       this.author = a;

       this.ISBN = ISBN;

       this.RRP = RRP;

   }

   public String getTitle() {

       return title;

   }

   public void setTitle(String title) {

       this.title = title;

   }

   public String getAuthor() {

       return author;

   }

   public void setAuthor(String author) {

       this.author = author;

   }

   public String getISBN() {

       return ISBN;

   }

   public void setISBN(String ISBN) {

       this.ISBN = ISBN;

   }

   public double getRRP() {

       return RRP;

   }

   public void setRRP(double RRP) {

       this.RRP = RRP;

   }

   @Override

   public String toString() {

       return "Book [title=" + title + ", author=" + author + ", ISBN=" + ISBN + ", RRP=" + RRP + "]";

   }

}

-------------------------------

import java.util.ArrayList;

import java.util.Scanner;

public class MyClass {

   public static void main(String[] args) {

       ArrayList<Book> myList = new ArrayList<Book>();

       Scanner sc = new Scanner(System.in);

       for (int i = 0; i < 4; i++) {

           System.out.println("Enter the book title: ");

           String title = sc.next();

           System.out.println("Enter the author of the book: ");

           String author = sc.next();

           System.out.println("Enter ISBN: ");

           String ISBN = sc.next();

           System.out.println("Enter RRP: ");

           double RRP = sc.nextDouble();

           Book b = new Book(title, author, ISBN, RRP);

           myList.add(b);

           Display(myList);

          

       }

       sc.close();

   }

   public static void Display(ArrayList<Book> display) {

       for (int i = 0; i < display.size(); i++) {

           System.out.println(display.get(i));

       }

      

   }

}

Explanation / Answer

import java.util.*;
public class MyClass{
public static void main(String[] args) {
ArrayList myList = new ArrayList();

Scanner sc = new Scanner(System.in);

for (int i = 0; i < 4; i++) {

System.out.println("Enter the book title: ");

String title = sc.next();

System.out.println("Enter the author of the book: ");

String author = sc.next();

System.out.println("Enter ISBN: ");

String ISBN = sc.next();

System.out.println("Enter RRP: ");

double RRP = sc.nextDouble();

Book b = Book (title, author, ISBN, RRP);

myList.add(b);
Display(myList);}
System.out.println(" enter isbn to delete book object");
String isbn_delete=sc.next();

Delete(isbn_delete,myList);

System.out.println(" updated book list is ");

Display(myList);

sc.close();

}

public static void Display(ArrayList display) {
for (int i = 0; i < display.size(); i++) {
System.out.println(display.get(i));
}
  
}

public static void Delete(String isbn_delete,ArrayList myList)

{

Iterator itr=myList.iterator();

while(itr.hasNext())
{
Book book=(Book)itr.next();
if(isbn_delete.equals(book.getISBN()))
itr.remove();
}

}

}