Putting it together
- Java course
- Intermediate
- Lesson 23/116
- 6 min
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.
Practice 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.
Starter code
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;
}
}
// Add the Runner class
// Create all objects and print in mainSample 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);
}
}Expected output
Library: Central Java - Gosling Alex (101) Alex <- Java
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which class holds the main method in the integration lesson?
- Library
- Loan
- Book
- Runner Correct answer
Explanation: Runner — a new Runner class is defined in the integration lesson.
2 Match classes to their roles
| Term | Meaning |
|---|---|
Library |
Library |
Book |
Book |
Member |
Member |
Loan |
Loan |
3 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(____);
}
}
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);
}
}
Explanation: The last line prints the loan record.