Free University of Bozen-Bolzano
Faculty of Computer Science - Bachelor in Applied Computer Science
Introduction to Programming - A.A. 2004/2005

Exam exercise
Class Buffer


A Buffer is an object that represents a zone of memory that temporarily stores data items. A Buffer has a limited capacity, i.e., it can store data only as long as there is still free memory in the zone represented by the buffer. For simplicity, we assume that each data item in a Buffer is represented by a string and that the amount of memory that it uses corresponds to the length of the string. Buffer objects support the following functionalities:


Part 1. Write a Java class Buffer to represent Buffer objects.

Solution: representation of the objects, skeleton of the class, class Buffer


Part 2. Realize a static method client of the class Buffer that, given a string f that represents the name of a file containing a line with an integer c and a certain number of successive lines, each containing a string, returns a Buffer object with initial capacity c and containing as data items the strings present in the file in the order in which they are read from the file.

Solution


Part 3. Describe the notions of local variable and of instance variable, and illustrate specifically the concepts of scope and lifetime.