Here is the complete book class, before adding the constructor.
/**
* Book models the behaviour of a book
*/
class Book {
// instance variables
private int numCopies;
/**
* sign out this book from the library
*/
public void checkOut() {
numCopies = numCopies - 1;
}
/**
* buy copies for the collection
* @param copies - the number of total copies purchased
*/
public void buyCopies(int copies) {
numCopies = copies;
}
/**
* bring a book back to the library
*/
public void checkIn() {
numCopies = numCopies + 1;
}
/**
* get number of copies available at this time
* @return numCopies
*/
public int numCopies() {
return numCopies;
}
}
Here is a sample main method that could be used to create a Book object and to call some methods on it.
// second version of the library program
// Notice that this is actually another class: Library will create
// Book objects. The two classes work together.
public class Library {
public static void main (String[] args) {
// Declare a variable, ready to hold a reference to a Book object.
Book b1;
// Create a new Book object and save the reference to it in our
// b1 variable
b1 = new Book();
System.out.println ("Buy 5 copies of the book");
b1.buyCopies(5);
System.out.println ("Sign out a copy");
b1.checkOut();
System.out.println ("Sign out another copy");
b1.checkOut();
}
}
[ Home | Outline | Announcements | Newsgroup | Assignments | Exams | Lectures | Links ]

© Copyright 2000. All Rights Reserved.