Lab5_AuthorBookComposition.java

← Back to Course
Java Source File Download
/**
 * Lab 5: Author and Book - Composition (Has-A Relationship)
 * PT821 - Object-Oriented Programming
 * State University of Zanzibar (SUZA)
 *
 * LEARNING OBJECTIVES:
 * - Understand composition ("has-a") vs inheritance ("is-a")
 * - Create classes that contain objects of other classes
 * - Access composed object properties through delegation
 * - Compare when to use composition vs inheritance
 *
 * KEY CONCEPT:
 * A Book HAS-A Author (composition)     -- NOT "A Book IS-A Author" (inheritance)
 * A Book is COMPOSED OF an Author        -- The Author is a PART of the Book
 *
 * INSTRUCTIONS:
 * Complete the following exercises step by step.
 * Follow the TODO comments and implement the required functionality.
 */

// ============================================================
// PART A: The Author Class
// ============================================================

/*
 * TODO 1: Create a class called "Author" with:
 *
 * Private instance variables:
 *   - name (String)
 *   - email (String)
 *   - gender (char, 'm' or 'f')
 *
 * Constructor:
 *   - Author(String name, String email, char gender)
 *
 * Public methods:
 *   - getName(), getEmail(), setEmail(String email), getGender()
 *   - toString() - returns "Author[name=?, email=?, gender=?]"
 *
 * NOTE: No setters for name and gender (they are fixed once set)
 */

// Write your Author class here:


// ============================================================
// PART B: The Book Class (composes Author)
// ============================================================

/*
 * TODO 2: Create a class called "Book" with:
 *
 * Private instance variables:
 *   - name (String)
 *   - author (Author) <-- This is COMPOSITION! Book "has-a" Author
 *   - price (double)
 *   - qty (int, quantity in stock)
 *
 * Constructors:
 *   - Book(String name, Author author, double price)
 *     Sets qty to 0
 *   - Book(String name, Author author, double price, int qty)
 *
 * Public methods:
 *   - getName(), getAuthor() - returns the Author object
 *   - getPrice(), setPrice(double price)
 *   - getQty(), setQty(int qty)
 *
 *   Methods that DELEGATE to the composed Author object:
 *   - getAuthorName() - returns author.getName()
 *   - getAuthorEmail() - returns author.getEmail()
 *   - getAuthorGender() - returns author.getGender()
 *
 *   - toString() - returns
 *     "Book[name=?, Author[name=?, email=?, gender=?], price=?, qty=?]"
 *     Hint: Use author.toString() inside Book's toString()
 */

// Write your Book class here:


// ============================================================
// PART C: Test Driver
// ============================================================

public class Lab5_AuthorBookComposition {
    public static void main(String[] args) {
        System.out.println("=============================================");
        System.out.println("  Lab 5: Author and Book (Composition)");
        System.out.println("=============================================\n");

        // ----- Section 1: Author Objects -----
        System.out.println("--- Section 1: Creating Authors ---");

        // TODO 3: Create Author objects
        // Author author1 = new Author("Ali Sultan", "ali.sultan@suza.ac.tz", 'm');
        // Author author2 = new Author("Mwanaisha Bakari", "mwanaisha.b@suza.ac.tz", 'f');
        // Author author3 = new Author("Hamad Khamis", "hamad.k@gmail.com", 'm');
        //
        // System.out.println(author1);
        // System.out.println(author2);
        // System.out.println(author3);

        // ----- Section 2: Book Objects (Composition) -----
        System.out.println("\n--- Section 2: Creating Books ---");

        // TODO 4: Create Book objects - note how Book CONTAINS an Author
        // Book book1 = new Book("Introduction to Java", author1, 35000, 50);
        // Book book2 = new Book("Data Structures in Java", author2, 42000, 30);
        //
        // System.out.println(book1);
        // System.out.println(book2);

        // ----- Section 3: Accessing Composed Object -----
        System.out.println("\n--- Section 3: Accessing Through Composition ---");

        // TODO 5: Access author details through the book
        // System.out.println("Book: " + book1.getName());
        // System.out.println("Author name: " + book1.getAuthorName());     // Delegated
        // System.out.println("Author email: " + book1.getAuthorEmail());   // Delegated
        //
        // // You can also get the Author object directly
        // Author bookAuthor = book1.getAuthor();
        // System.out.println("Author object: " + bookAuthor);

        // ----- Section 4: Shared References -----
        System.out.println("\n--- Section 4: Shared Author References ---");

        // TODO 6: Same author can write multiple books
        // Book book3 = new Book("Advanced Java Programming", author1, 55000, 20);
        // System.out.println("Book 1 author: " + book1.getAuthorName());
        // System.out.println("Book 3 author: " + book3.getAuthorName());
        // System.out.println("Same author? " + (book1.getAuthor() == book3.getAuthor()));
        //
        // // What happens when we change the shared author's email?
        // author1.setEmail("ali.sultan.new@suza.ac.tz");
        // System.out.println("\nAfter changing author1's email:");
        // System.out.println("Book 1 author email: " + book1.getAuthorEmail());
        // System.out.println("Book 3 author email: " + book3.getAuthorEmail());
        // System.out.println("Both changed! Because they share the same Author object.");

        // ----- Section 5: Anonymous Author -----
        System.out.println("\n--- Section 5: Creating Book with Anonymous Author ---");

        // TODO 7: Create a Book with an anonymous Author (inline construction)
        // Book book4 = new Book(
        //     "Python for Beginners",
        //     new Author("Salma Haji", "salma.h@suza.ac.tz", 'f'),
        //     28000,
        //     100
        // );
        // System.out.println(book4);
        // System.out.println("Author: " + book4.getAuthorName());

        // ----- Section 6: Book Inventory -----
        System.out.println("\n--- Section 6: Book Inventory ---");

        // TODO 8: Manage a collection of books
        // Book[] inventory = {book1, book2, book3, book4};
        //
        // System.out.println("SUZA Bookshop Inventory:");
        // System.out.println(String.format("%-30s %-25s %10s %5s",
        //     "Title", "Author", "Price(TZS)", "Qty"));
        // System.out.println("-".repeat(75));
        //
        // double totalValue = 0;
        // for (Book book : inventory) {
        //     System.out.println(String.format("%-30s %-25s %,10.0f %5d",
        //         book.getName(), book.getAuthorName(),
        //         book.getPrice(), book.getQty()));
        //     totalValue += book.getPrice() * book.getQty();
        // }
        // System.out.println("-".repeat(75));
        // System.out.println(String.format("Total inventory value: TZS %,.0f", totalValue));

        System.out.println("\n=============================================");
        System.out.println("  End of Lab 5");
        System.out.println("=============================================");
    }
}

/*
 * EXPECTED OUTPUT (after completing all TODOs):
 *
 * =============================================
 *   Lab 5: Author and Book (Composition)
 * =============================================
 *
 * --- Section 1: Creating Authors ---
 * Author[name=Ali Sultan, email=ali.sultan@suza.ac.tz, gender=m]
 * Author[name=Mwanaisha Bakari, email=mwanaisha.b@suza.ac.tz, gender=f]
 * Author[name=Hamad Khamis, email=hamad.k@gmail.com, gender=m]
 *
 * --- Section 2: Creating Books ---
 * Book[name=Introduction to Java, Author[name=Ali Sultan, email=ali.sultan@suza.ac.tz, gender=m], price=35000.0, qty=50]
 * Book[name=Data Structures in Java, Author[name=Mwanaisha Bakari, email=mwanaisha.b@suza.ac.tz, gender=f], price=42000.0, qty=30]
 *
 * --- Section 3: Accessing Through Composition ---
 * Book: Introduction to Java
 * Author name: Ali Sultan
 * Author email: ali.sultan@suza.ac.tz
 * Author object: Author[name=Ali Sultan, email=ali.sultan@suza.ac.tz, gender=m]
 *
 * --- Section 4: Shared Author References ---
 * Book 1 author: Ali Sultan
 * Book 3 author: Ali Sultan
 * Same author? true
 *
 * After changing author1's email:
 * Book 1 author email: ali.sultan.new@suza.ac.tz
 * Book 3 author email: ali.sultan.new@suza.ac.tz
 * Both changed! Because they share the same Author object.
 *
 * --- Section 5: Creating Book with Anonymous Author ---
 * Book[name=Python for Beginners, Author[name=Salma Haji, ...], ...]
 * Author: Salma Haji
 *
 * --- Section 6: Book Inventory ---
 * SUZA Bookshop Inventory:
 * Title                          Author                        Price(TZS)   Qty
 * ---------------------------------------------------------------------------
 * Introduction to Java           Ali Sultan                     35,000    50
 * Data Structures in Java        Mwanaisha Bakari               42,000    30
 * Advanced Java Programming      Ali Sultan                     55,000    20
 * Python for Beginners           Salma Haji                     28,000   100
 * ---------------------------------------------------------------------------
 * Total inventory value: TZS 6,810,000
 *
 * QUESTIONS TO ANSWER:
 * 1. What is the difference between composition (has-a) and inheritance (is-a)?
 * 2. Why is it better for Book to HAVE an Author rather than EXTEND Author?
 * 3. In Section 4, why does changing author1's email affect both books?
 *    How could you prevent this? (Hint: think about deep copy vs shallow copy)
 * 4. Can an Author exist without a Book? Can a Book exist without an Author?
 *    What does this tell you about the strength of the composition relationship?
 *
 * SUBMISSION:
 * - Complete all TODO sections (create Author and Book classes + uncomment test code)
 * - Answer all questions above in comments
 * - Submit the completed .java file
 */