public class Dynamicassign {
public static void Main (string[] args) {
area[] area = new AREA[5];
AREA[0] = new Area (0, 20);
AREA[1] = new Area (1, 30);
AREA[2] = new Area (2, 40);
AREA[3] = new Area (3, 50);
AREA[4] = new Area (4, 150);
memory[] Memory = new Memory[4];
MEMORY[0] = new Memory (0, 100);
MEMORY[1] = new Memory (1, 30);
MEMORY[2] = new Memory (2, 50);
MEMORY[3] = new Memory (3, 40);
FIRSTADAPT (memory, area);
BESTADAPT (memory, area);
BADADAPT (Memory,area);
}
First adaptive algorithm
public static void Firstadapt (memory[] Memory, area[] area) {
int k;
for (int i = 0; i < memory.length; i++) {
K = Area.length;
for (int j = 0; J < Area.length; J + +) {
if ((Memory[i].getvalue ()) <= (Area[j].getvalue ())) {
Area[j].setvalue (Area[j].getvalue ()-memory[i].getvalue ());
System.out.println (Memory[i] + "--->" + area[j]);
Break
} else
k--;
if (K < 1) {
System.out.println (Memory[i] + "Failed!");
}
}
}
}
Optimal adaptive algorithm
public static void Bestadapt (memory[] Memory, area[] area) {
int I, J;
for (i = 0; i < memory.length; i++) {
Boolean flag = true;
int min = 0;
for (j = 0; J < Area.length; J + +) {
if (flag) {
if (Memory[i].getvalue () <= area[j].getvalue ()) {
min = Area[j].getvalue ();
Flag = false;
}
else if ((Memory[i].getvalue () <= area[j].getvalue ())
&& (min >= area[j].getvalue ()) {
min = Area[j].getvalue ();
}
}
if (min = 0) {
System.out.println (Memory[i] + "Failed!");
} else {
for (j = 0; J < Area.length; J + +) {
if (area[j].getvalue () = min) {
System.out.println (Memory[i] + "--->" + area[j]);
Area[j].setvalue (Min-memory[i].getvalue ());
Break
}
}
}
}
}
Worst Fit algorithm
public static void Badadapt (memory[] Memory, area[] area) {
int I, J;
for (i = 0; i < memory.length; i++) {
Boolean flag = true;
int max = 0;
for (j = 0; J < Area.length; J +) {
if (flag) {
if (Memory[i].getvalue () <= area[j].getvalue ()) {
max = Area[j].getvalue ();
flag = false;
}
} else if ((Memory[i].getvalue () <= area[j].getvalue ())
&& (Max <= area[j].getvalue ()) {
max = Area[j].getvalue ();
&NBSP;&NBSP;&NBSP;&NBSP}
}
if (max = 0) {
System.out.println (Memory[i] + "Failed!");
} else {
for (j = 0; J < Area.length; J + +) {
if (area[j].getvalue () = max) {
System.out.println (Memory[i] + "--->" + area[j]);
Area[j].setvalue (Max-memory[i].getvalue ());
Break
}
}
}
}
}
}
Define a memory Job area
Class Area {
private int id;
private int value;
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
public int GetValue () {
return value;
}
public void SetValue (int value) {
This.value = value;
}
Area (int id) {
This.id = ID;
}
Area (int id, int value) {
This.id = ID;
This.value = value;
}
Public String toString () {
Return "area:" + ID;
}
}
Define a memory job
Class Memory {
area[] area = new AREA[5];
memory[] Memory = new Memory[4];
private int id;
private int value;
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
public int GetValue () {
return value;
}
public void SetValue (int value) {
This.value = value;
}
Memory (int id) {
This.id = ID;
}
Memory (int id, int value) {
This.id = ID;
This.value = value;
}
Public String toString () {
Return "Memory:" + ID;
}
}