Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a java class memory that has the following methods: A constructor that ta

ID: 3624068 • Letter: C

Question

Create a java class memory that has the following methods: A constructor that takes a single argument which is an integer that determines the initial size of the private array of integers the Memory manages. A method int allocate (int howmuch) that returns the offset into the array of how much integers's worth of storage in the array. Allocate should throw an out of memory exception if no such free storage is available. A method void free(int where) that returns the block of memory previously allocated that began at where. Test your class by allocating and freeing memory. You should force the exception to be thrown, then free some memory and try again. Describe your algorithms in detail in the program documentation.

Explanation / Answer

public class Memory

{

public Memory(int size)

{

int array[]=new int[size];

}

public void allocate(int howmuch)

{

// Step 1: get a Runtime object

Runtime r = Runtime.getRuntime();

// Step 2: determine the current amount of free memory

long freeMem = r.freeMemory();

int array[]=new int[ howmuch];

}

void free(int where)

{

Runtime r = Runtime.getRuntime();

// Step 5: run the garbage collector, then check freeMemory

r.gc();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote