<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.util.Vector;
import java.util.Enumeration;

/* This class provides an interface for main memory.
 * The size of the memory is specified by number of page frames.
 * map, unmap, and swap operations are provides by this interface.
 */
public class MemoryInterface {
    private int numFrames; // total number of page frames in memory.
    private Vector map;    // mapping from page frame to pages. 
    
    /* This constructor creates a memory with number of page frames = numFrames. 
     */ 
    public MemoryInterface (int numFrames) {
		this.numFrames = numFrames;
		map = new Vector(numFrames);
    }

    public void reset () {
		map.removeAllElements();
    }

    /* This method maps a page into memory.  
     * Info specifies information associated with this page. (ex: frequency of access.) 
     * Returns false when no unmapped page frames are found. 
     */
    public boolean map (Info info) {
		if (map.size() &lt; numFrames) {
		    map.addElement(info);
			return true;
		}

		return false;
    }

    /* This method unmaps a page in the memory.
     * Returns info associated with that object.
     * Returns null if page is not found.
     */
    public Info unmap (int pageNumber) {
		for (int i=0; i&lt;map.size(); i++) {
			try {
				Info info = (Info)map.elementAt(i);
				if (info.pageNumber == pageNumber) {
					map.removeElementAt(i);
					return info;
				}
			}	
			catch (ArrayIndexOutOfBoundsException e) {
				return null;
			}				
		}
		return null;
	}
		

    /* This method checks if the page is mapped to the memory.
     * Returns false when the page is not found in memory.
     */
    public boolean isMapped (int pageNumber) {
		for (int i=0; i&lt;map.size(); i++) {
			try {
				Info info = (Info)map.elementAt(i);
				if (info.pageNumber == pageNumber) {
					return true;
				}
			}	
			catch (ArrayIndexOutOfBoundsException e) {
				return false;
			}				
		}
		return false;
    }

    /* This method swaps the oldPage out of the memory and maps the newPage in.
     * Returns info associated with the old page. 
     * Returns null if the oldPage is not found in memory.
     */ 
    public Info swap (int oldPageNumber, Info newInfo) {
		for (int i=0; i&lt;map.size(); i++) {
			try {
				Info info = (Info)map.elementAt(i);
				if (info.pageNumber == oldPageNumber) {
					map.removeElementAt(i);
					map.addElement(newInfo);
					return info;
				}
			}	
			catch (ArrayIndexOutOfBoundsException e) {
				return null;
			}				
		}
		return null;
    }
    
    /* This method returns the number of pages currently mapped to the memory.
     */
    public int numPages () {
		return map.size();
    }

    /* This method returns an enumeration of the pages in the memory.
     * The enumeration consists of only the page numbers.
     * Use getInfo to get the associated page info.
     */
    public Enumeration getMap () {
		return map.elements();
    }

    /* This method returns the info associated with the page. 
     * Returns null if the page is not found.
     */
    public Info getInfo (int pageNumber) {
		for (int i=0; i&lt;map.size(); i++) {
			try {
				Info info = (Info)map.elementAt(i);
				if (info.pageNumber == pageNumber) {
					return info;
				}
			}	
			catch (ArrayIndexOutOfBoundsException e) {
				return null;
			}				
		}
		return null;
    }
}


</pre></body></html>