0% found this document useful (0 votes)
6 views2 pages

Java Assignment 2

The document contains a Java class named 'Book' with private attributes for title, author, and year, along with a constructor and getter methods. It also includes a main method that creates an array of five Book objects with different titles and authors, and prints their details. This code demonstrates basic object-oriented programming concepts in Java.

Uploaded by

mikeyragz6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java Assignment 2

The document contains a Java class named 'Book' with private attributes for title, author, and year, along with a constructor and getter methods. It also includes a main method that creates an array of five Book objects with different titles and authors, and prints their details. This code demonstrates basic object-oriented programming concepts in Java.

Uploaded by

mikeyragz6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like