i have error on line 29 please help | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

i have error on line 29 please help

import java.util.ArrayList; import java.util.List; public class Book { private String title; private boolean borrowed; public Book(String title) { this.title = title; this.borrowed = false; } public String getTitle() { return title; } public boolean isAvailable() { return !borrowed; } public void borrowBook() { borrowed = true; } public void returnBook() { borrowed = false; } } import java.util.List; public class Library { private String address; private List<Book> books; public Library(String address) { this.address = address; this.books = new ArrayList<>(); } public void printAvailableBooks() { System.out.println("Available books at " + address + ":"); for (Book book : books) { if (book.isAvailable()) { System.out.println(book.getTitle()); } } } public Book borrowBook(String title) { for (Book book : books) { if (book.getTitle().equals(title) && book.isAvailable()) { book.borrowBook(); return book; } } System.out.println("Error: Book not available."); return null; } public void returnBook(Book book) { if (books.contains(book)) { book.returnBook(); } else { System.out.println("Error: Book not in collection."); } } }

18th Apr 2024, 9:00 AM
kedibone babra
7 ответов
+ 7
The code provided seems mostly correct, but there are a few improvements and debugging points: 1. **Book Class:** - It's generally a good practice to add `@Override` annotations when overriding methods, like `toString()`. - Ensure that the `Book` class overrides the `toString()` method to provide a meaningful string representation of the book. - The `borrowed` field should be set to `true` when the book is borrowed and `false` when it's returned. 2. **Library Class:** - In the `borrowBook` method, it should also check if the book is available before attempting to borrow it. - Ensure that the `printAvailableBooks()` method handles the case when there are no available books. Here's the corrected and improved code: ```java import java.util.ArrayList; import java.util.List; public class Book { private String title; private boolean borrowed; public Book(String title) { this.title = title; this.borrowed = false; } public String getTitle() { return ti
19th Apr 2024, 3:32 AM
EVANS JOHN NATHANIEL ENDE
EVANS JOHN NATHANIEL ENDE - avatar
+ 4
kedibone babra , Kamran Mushtaq , please do not paste the code into the text area. it is better to create a link to the code in the playground, and post this link. so people have direct access to the code and do not need to copy/ paste to see what the issue is.
18th Apr 2024, 3:54 PM
Lothar
Lothar - avatar
+ 4
Kamran Mushtaq , both of your codes are not complete, but truncated.
18th Apr 2024, 6:05 PM
Lothar
Lothar - avatar
0
The error on line 29 is because you are calling a method on a null object reference. Make sure to add the main method in a separate class or in one of your existing classes. This code could be your guideline⤵️ https://sololearn.com/compiler-playground/c1L3NDi1SNTD/?ref=app
18th Apr 2024, 9:11 AM
`нттየ⁴⁰⁶
`нттየ⁴⁰⁶ - avatar
0
import java.util.ArrayList; import java.util.List; public class Book { private String title; private boolean borrowed; public Book(String title) { this.title = title; this.borrowed = false; } public String getTitle() { return title; } public boolean isAvailable() { return !borrowed; } public void borrowBook() { borrowed = true; } public void returnBook() { borrowed = false; } } public class Library { private String address; private List<Book> books; public Library(String address) { this.address = address; this.books = new ArrayList<>(); } public void addBook(Book book) { books.add(book); } public void printAvailableBooks() { System.out.println("Available books at " + address + ":"); for (Book book : books) { if (book.isAvailable()) { System.out.println(book.getTitle()); } } } public
18th Apr 2024, 2:40 PM
Kamran Mushtaq
Kamran Mushtaq - avatar
0
public class Main { public static void main(String[] args) { // Create a library Library library = new Library("Main Street Library"); // Add books to the library library.addBook(new Book("book 1")); library.addBook(new Book("book 2")); library.addBook(new Book("book 3")); // Print available books library.printAvailableBooks(); } } public void addBook(Book book) { books.add(book); } Use this code
19th Apr 2024, 2:24 AM
Vidhya Tharan
Vidhya Tharan - avatar