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

JAVA Using the for loop statement, define a method that reads and displays the d

ID: 3875534 • Letter: J

Question

JAVA

Using the for loop statement, define a method that reads and displays the details of the Book objects added in the ArrayList.

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).

Book.java

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;

   }

   }

Main.java

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);

       }

       sc.close();

}

   }

Explanation / Answer

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 Main {

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

public static void main(String[] args){

  

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();

boolean isDeleted=deleteISBN();

System.out.println("the book is deleted?"+isDeleted);

}

sc.close();

}

public static void display()

{

for(Book book:myList)

{

System.out.println(book);

}

}

  

public static boolean deleteISBN()

{

Scanner sc = new Scanner(System.in);

String ISBN=sc.next();

boolean flag=false;

for(Book book:myList)

{

if(book.getISBN()==ISBN)

{

myList.remove(book);

flag=true;

}

}

return flag;

}

}