Answer it
P2 b134 library encapsulation
6 marksYou mark it yourself against the marking points
A school library program has a Book class. Here it is in Python and in Java.
class Book:
def __init__(self, title, copies):
self.title = title
self.__available = copies
def get_available(self):
return self.__available
def borrow(self):
if self.__available > 0:
self.__available -= 1
return True
return False
public class Book {
private String title;
private int available;
public Book(String title, int copies) {
this.title = title;
this.available = copies;
}
public int getAvailable() {
return available;
}
public boolean borrow() {
if (available > 0) {
available--;
return true;
}
return false;
}
}
(a) Identify the method in Book that is an accessor. [1]
(b) The number of available copies is private (in Python, its name starts with two underscores). Explain why it is private, and why code outside the class changes it only by calling borrow. [3]
(c) Another part of the program runs Book("Dune", 2) (in Java, new Book("Dune", 2)) and Book("Emma", 1). Distinguish between the class Book and the objects these lines create. [2]
[6]