Library Management System
This program models a simple library management system.
Problem Statement
Write a Java program to model Book and Library classes with association.
Source Code
| 1 | class Book { |
| 2 | String title; |
| 3 | Book(String t){ title=t; } |
| 4 | } |
| 5 | class Library { |
| 6 | Book book; |
| 7 | Library(Book b){ book=b; } |
| 8 | void showBook(){ System.out.println("Library has book: "+book.title); } |
| 9 | } |
| 10 | public class LibraryManagement { |
| 11 | public static void main(String[] args) { |
| 12 | System.out.println("Eduinq Library Management"); |
| 13 | Book b=new Book("Java Programming"); |
| 14 | Library l=new Library(b); |
| 15 | l.showBook(); |
| 16 | } |
| 17 | } |
Program Output
Eduinq Library Management Library has book: Java Programming
Explanation
Book class models individual book entity. - Library class associates with Book, showing composition/association. - This demonstrates how OOP models real world systems by linking objects together. - Cohesion is maintained as each class has a clear responsibility. Conclusion: Shows how to model library management system using OOP.