0
0
Javaprogramming~30 mins

Object interaction in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Object Interaction in Java
πŸ“– Scenario: You are creating a simple program to manage a library. The library has books, and each book has a title and an author. You want to show how books and the library can work together.
🎯 Goal: Build two classes, Book and Library, where the library holds multiple books. You will create book objects, add them to the library, and then print the list of books in the library.
πŸ“‹ What You'll Learn
Create a Book class with title and author fields
Create a Library class with a list to hold Book objects
Add a method in Library to add a Book
Add a method in Library to print all books with their titles and authors
πŸ’‘ Why This Matters
🌍 Real World
Managing collections of items like books, movies, or products often requires objects that hold other objects. This project shows how to organize such data.
πŸ’Ό Career
Understanding object interaction is key for software development jobs where you build systems with multiple parts working together.
Progress0 / 4 steps
1
Create the Book class
Create a class called Book with two String fields: title and author. Add a constructor that sets these fields.
Java
Need a hint?

Think of a book as having a name and a writer. Use a constructor to set these when you create a book.

2
Create the Library class with a list of books
Create a class called Library with a field books that is an ArrayList<Book>. Initialize this list in the constructor.
Java
Need a hint?

Use ArrayList<Book> to hold many books. Initialize it inside the constructor.

3
Add method to add books to the library
In the Library class, add a method called addBook that takes a Book object as a parameter and adds it to the books list.
Java
Need a hint?

Use the add method of ArrayList to add the book.

4
Print all books in the library
In the Library class, add a method called printBooks that loops over the books list and prints each book's title and author in the format: Title: [title], Author: [author]. Then, in the main method, create a Library object, add two Book objects with titles "1984" and "Animal Farm" by author "George Orwell", and call printBooks.
Java
Need a hint?

Use a for-each loop to go through each book and print its details. Then create the library and books in main.