w3resource

Java: Create a Circle class and calculate its area and circumference

Java OOP: Exercise-5 with Solution

Write a Java program to create a class called "Book" with attributes for title, author, and ISBN, and methods to add and remove books from a collection.

Sample Solution:

Java Code:

//Book.java
import java.util.ArrayList;
public class Book {
  private String title;
  private String author;
  private String ISBN;
  private static ArrayList < Book > bookCollection = new ArrayList < Book > ();

  public Book(String title, String author, String ISBN) {
    this.title = title;
    this.author = author;
    this.ISBN = ISBN;
  }

  public String get_Title() {
    return title;
  }

  public void set_Title(String title) {
    this.title = title;
  }

  public String get_Author() {
    return author;
  }

  public void set_Author(String author) {
    this.author = author;
  }

  public String get_ISBN() {
    return ISBN;
  }

  public void set_ISBN(String ISBN) {
    this.ISBN = ISBN;
  }

  public static void add_Book(Book book) {
    bookCollection.add(book);
  }

  public static void remove_Book(Book book) {
    bookCollection.remove(book);
  }

  public static ArrayList < Book > get_BookCollection() {
    return bookCollection;
  }
}

The above class has three private attributes: title, author and ISBN. It has a constructor that initializes these attributes with the values passed as arguments, and getter and setter methods to access and modify these attributes. It also has static methods to add and remove books from a collection, and a static method to get the book collection.

//Main.java
import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    Book book1 = new Book("The C Programming Language", "Dennis Ritchie, Brian Kernighan", "9780131101630");
    Book book2 = new Book("An Introduction to Python", "Guido van Rossum", "9355423489");
    Book.add_Book(book1);
    Book.add_Book(book2);
    ArrayList < Book > bookCollection = Book.get_BookCollection();
    System.out.println("List of books:");
    for (Book book: bookCollection) {
      System.out.println(book.get_Title() + " by " + book.get_Author() + ", ISBN: " + book.get_ISBN());
    }
    Book.remove_Book(book1);
    System.out.println("\nAfter removing " + book1.get_Title() + ":");
    System.out.println("List of books:");
    for (Book book: bookCollection) {
      System.out.println(book.get_Title() + " by " + book.get_Author() + ", ISBN: " + book.get_ISBN());
    }
  }
}

In this example code, we create two instances of the "Book" class and add them to the collection with the ‘addBook’ method. We then print the title, author, and ISBN of each book in the collection using a for loop. We also remove book1 from the collection using the ‘removeBook’ method and print the updated collection.

Sample Output:

List of books:
The C Programming Language by Dennis Ritchie, Brian Kernighan, ISBN: 9780131101630
An Introduction to Python by Guido van Rossum, ISBN: 9355423489

After removing The C Programming Language:
List of books:
An Introduction to Python by Guido van Rossum, ISBN: 9355423489

Flowchart:

Flowchart: Java  OOP Exercises: Create a Circle class and calculate its area and circumference.
Flowchart: Java  OOP Exercises: Create a Circle class and calculate its area and circumference.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Create a Circle class with area and circumference calculation.
Java OOP Next: Employee Management System.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.