Library in Java
In Java, a library is a collection of classes, interfaces, and methods that can be used in a program. It allows programmers to reuse code and avoid writing the same code repeatedly. In this tutorial, we will learn how to create a library in Java.
To make a library in Java, we need to create a new class with methods that can be used by other programs. Let's call this class "Library01". Within this class, we will define four methods: "addBooks()", "issueBooks()", "showBooks()", and "returnBooks()".
The "addBooks()" method will allow users to add new books to the library. The "issueBooks()" method will handle the process of issuing a book to a user. The "showBooks()" method will display all the books currently available in the library, and the "returnBooks()" method will handle the process of returning a book to the library.
Once the Library01 class is defined, we can compile and package it as a library file, which can be used in other programs. To use this library in a program, we need to import it and create an instance of the Library01 class. We can then call its methods to perform the desired operations.
Overall, creating a library in Java is a useful skill that allows programmers to modularize their code and create reusable components. It is a great way to save time and effort when developing new programs.
PROGRAM :-
class library01{
String[] books;
int num;
library01(){
books = new String[100];
num=0;
}
void addbooks(String book){
books[num]=book;
num++;
System.out.println(book+" has been added !");
}
void issuebooks(String book){
for(int i=0;i<books.length;i++){
if(books[i]==book){
System.out.println("The "+book+" has been issued !");
books[i]=null;
return;
}
}
System.out.println("The book does not exist.");
}
void showbooks(){
System.out.println("Available Books : ");
for(int i=0;i<books.length;i++){
if(books[i]==null){
continue;
}
System.out.println("* "+books[i]);
}
}
void returnbooks(String book){
addbooks(book);
}
}
public class library {
public static void main(String[] args) {
library01 lib=new library01();
lib.addbooks("Java");
lib.addbooks("Linux by Linus");
lib.addbooks("Programing in C");
lib.addbooks("Depth C++");
lib.showbooks();
lib.issuebooks("Java");
lib.showbooks();
lib.returnbooks("Java");
lib.showbooks();
}
}
0 Comments