Java
Putting it together
Combine the library class with a menu loop.
Integration
The final lesson brings all classes together. You'll define a class named Runner and in its main method create and print the library, book, member and loan.
Example: In Runner's main method, calls like new Library("Central"), new Book("Java", "Gosling"), new Member("Alex", 101) and new Loan(b, m) are made. Each object is printed on a separate line. This shows how all parts of the project come together. You now have a working library program; lists and menus can be added later.
Program output
The integration lesson produces four lines of output: the library, the book, the member and the loan. Each object is printed by its own toString method. The output shows all parts of the project working together.
Expected output: Library: Central, Java - Gosling, Alex (101), Alex <- Java. These lines come from the Library, Book, Member and Loan objects respectively. Each class's toString method converts the object into a meaningful string. When the program runs, the library's status is visible at a glance.
Exercise Define the Runner class and print everything
Add a Runner class to the file. In its main method create a Library, a Book, a Member and a Loan instance. Print each object on a separate line with println: first Library, then Book, then Member, last Loan.
Show example solution
class Library {
String name;
Library(String n) {
name = n;
}
public String toString() {
return "Library: " + name;
}
}
class Book {
String title;
String author;
Book(String t, String a) {
title = t;
author = a;
}
public String toString() {
return title + " - " + author;
}
}
class Member {
String name;
int id;
Member(String n, int i) {
name = n;
id = i;
}
public String toString() {
return name + " (" + id + ")";
}
}
class Loan {
Book book;
Member member;
Loan(Book b, Member m) {
book = b;
member = m;
}
public String toString() {
return member.name + " <- " + book.title;
}
}
class Runner {
public static void main(String[] args) {
Library lib = new Library("Central");
Book b = new Book("Java", "Gosling");
Member m = new Member("Alex", 101);
Loan loan = new Loan(b, m);
System.out.println(lib);
System.out.println(b);
System.out.println(m);
System.out.println(loan);
}
}
Test yourself
-
Which class holds the main method in the integration lesson?
Show answer
Runner — Runner — a new Runner class is defined in the integration lesson.
-
Match classes to their roles
- Library
- Library
- Book
- Book
- Member
- Member
- Loan
- Loan
-
Complete the Runner main body
class Runner {
public static void main(String[] args) {
Library lib = new Library("Merkez");
Book b = new Book("Java", "Gosling");
Member m = new Member("Ayşe", 101);
Loan loan = new Loan(b, m);
System.out.println(lib);
System.out.println(b);
System.out.println(m);
System.out.println(____);
}
}
Show answer
class Runner {
public static void main(String[] args) {
Library lib = new Library("Merkez");
Book b = new Book("Java", "Gosling");
Member m = new Member("Ayşe", 101);
Loan loan = new Loan(b, m);
System.out.println(lib);
System.out.println(b);
System.out.println(m);
System.out.println(loan);
}
}
Next lesson: private fields