University of Toronto - Fall 2000
Department of Computer Science
Week 5 - toString() Method
Book class Example
class Book {
private String title;
private int numCopies;
public void setTitle (String t) {
title = t;
}
public String getTitle () {
return title;
}
public String toString() {
return title + ": " + numCopies + " copies available.";
}
}
public class Library {
public static void main (String[] args) {
Book b = new Book ();
b.setTitle("Angela's Ashes");
b.buyCopies(2);
System.out.println(b.toString()); // or just b in parenthesis
}
}