Add a class Borrower that extends User. The constructor of the Borrower class takes a name and a number of books borrowed by the borrower. If the number of books given as argument is strictly less than zero, then the constructor must throw a NotALenderException with the message "A new borrower cannot lend books.". The borrower class does not have any instance variable.java代码
时间: 2024-01-20 11:02:18 浏览: 204
JAVA借书系统代码
Sure, here's your Java code:
```java
public class Borrower extends User {
public Borrower(String name, int numBooksBorrowed) throws NotALenderException {
super(name);
if(numBooksBorrowed < 0) {
throw new NotALenderException("A new borrower cannot lend books.");
}
}
}
```
Here, we have defined a `Borrower` class that extends the `User` class. The constructor of the `Borrower` class takes a name and a number of books borrowed by the borrower. If the number of books given as argument is strictly less than zero, then the constructor throws a `NotALenderException` with the message "A new borrower cannot lend books.".
Note that we are calling the constructor of the parent `User` class using the `super` keyword, passing in the `name` argument. The `Borrower` class does not have any instance variables.
阅读全文