ASSIGNMENT
1.
public class Book {
// Private attributes
private String title;
private String author;
private int year;
// Constructor to set attributes during object instantiation
public Book(String title, String author, int year) {
this.title = title;
this.author = author;
this.year = year;
// Getter methods
public String getTitle() {
return title;
public String getAuthor() {
return author;
public int getYear() {
return year;
public static void main(String[] args) {
// Create an array to store five Book objects
Book[] library = new Book[5];
// Instantiate five Book objects with different details
library[0] = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925);
library[1] = new Book("To Kill a Mockingbird", "Harper Lee", 1960);
library[2] = new Book("1984", "George Orwell", 1949);
library[3] = new Book("The Hobbit", "J.R.R. Tolkien", 1937);
library[4] = new Book("Harry Potter and the Sorcerer's Stone", "J.K. Rowling", 1997);
// Loop to traverse the library array and print book details
for (Book book : library) {
System.out.println("Title: " + book.getTitle() +
", Author: " + book.getAuthor() +
", Year: " + book.getYear());