import java.util.Scanner;
public class Main {
private static int n;
private static int[] referenceString;
private static int[] physicalFrames;
private static int[] victimPages;
private static int pageFaults;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("0 Exit");
System.out.println("1 Input N");
System.out.println("2 Input the reference string");
System.out.println("3 Simulate the OPT algorithm");
System.out.println("4 Simulate the NEW algorithm");
System.out.print("Select option: ");
choice = input.nextInt();
switch (choice) {
case 0:
System.out.println("Exiting the program...");
break;
case 1:
System.out.print("Enter the value of N (2-8): ");
n = input.nextInt();
if (n < 2 || n > 8) {
System.out.println("Invalid value of N!");
n = 0;
} else {
physicalFrames = new int[n];
victimPages = new int[n];
System.out.println("N set to " + n + ".");
}
break;
case 2:
if (n == 0) {
System.out.println("Please enter the value of N first!");
} else {
System.out.print("Enter the reference string (length at least " + n + ", max length 20): ");
String[] strArr = input.next().split("");
int len = strArr.length;
if (len < n || len > 20) {
System.out.println("Invalid length of reference string!");
} else {
referenceString = new int[len];
for (int i = 0; i < len; i++) {
int page = Integer.parseInt(strArr[i]);
if (page < 0 || page > 9) {
System.out.println("Invalid page number!");
referenceString = null;
break;
}
referenceString[i] = page;
}
if (referenceString != null) {
System.out.println("Reference string set to " + input.next() + ".");
}
}
}
break;
case 3:
if (n == 0) {
System.out.println("Please enter the value of N first!");
} else if (referenceString == null) {
System.out.println("Please enter the reference string first!");
} else {
System.out.println("Simulating the OPT algorithm...");
simulateOPT();
}
break;
case 4:
if (n == 0) {
System.out.println("Please enter the value of N first!");
} else if (referenceString == null) {
System.out.println("Please enter the reference string first!");
} else {
System.out.println("Simulating the NEW algorithm...");
simulateNEW();
}
break;
default:
System.out.println("Invalid choice!");
break;
}
System.out.println();
} while (choice != 0);
}
private static void simulateOPT() {
pageFaults = 0;
int len = referenceString.length;
System.out.println("Reference String\tPhysical Frames\tPage Faults\tVictim Pages");
for (int i = 0; i < len; i++) {
int page = referenceString[i];
boolean found = false;
for (int j = 0; j < n; j++) {
if (physicalFrames[j] == page) {
found = true;
break;
}
}
if (!found) {
pageFaults++;
int maxDist = 0;
int victimPage = -1;
for (int j = 0; j < n; j++) {
int dist = 0;
for (int k = i + 1; k < len; k++) {
dist++;
if (physicalFrames[j] == referenceString[k]) {
break;
}
}
if (dist > maxDist) {
maxDist = dist;
victimPage = j;
}
}
physicalFrames[victimPage] = page;
victimPages[i] = physicalFrames[victimPage];
} else {
victimPages[i] = -1;.
import java.util.Scanner;public class Main {private static i.pdf
1. import java.util.Scanner;
public class Main {
private static int n;
private static int[] referenceString;
private static int[] physicalFrames;
private static int[] victimPages;
private static int pageFaults;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("0 Exit");
System.out.println("1 Input N");
System.out.println("2 Input the reference string");
System.out.println("3 Simulate the OPT algorithm");
System.out.println("4 Simulate the NEW algorithm");
System.out.print("Select option: ");
choice = input.nextInt();
switch (choice) {
case 0:
System.out.println("Exiting the program...");
break;
case 1:
System.out.print("Enter the value of N (2-8): ");
n = input.nextInt();
if (n < 2 || n > 8) {
System.out.println("Invalid value of N!");
n = 0;
} else {
physicalFrames = new int[n];
victimPages = new int[n];
System.out.println("N set to " + n + ".");
2. }
break;
case 2:
if (n == 0) {
System.out.println("Please enter the value of N first!");
} else {
System.out.print("Enter the reference string (length at least " + n + ", max length 20): ");
String[] strArr = input.next().split("");
int len = strArr.length;
if (len < n || len > 20) {
System.out.println("Invalid length of reference string!");
} else {
referenceString = new int[len];
for (int i = 0; i < len; i++) {
int page = Integer.parseInt(strArr[i]);
if (page < 0 || page > 9) {
System.out.println("Invalid page number!");
referenceString = null;
break;
}
referenceString[i] = page;
}
if (referenceString != null) {
System.out.println("Reference string set to " + input.next() + ".");
}
}
}
break;
case 3:
if (n == 0) {
System.out.println("Please enter the value of N first!");
} else if (referenceString == null) {
System.out.println("Please enter the reference string first!");
} else {
System.out.println("Simulating the OPT algorithm...");
simulateOPT();
3. }
break;
case 4:
if (n == 0) {
System.out.println("Please enter the value of N first!");
} else if (referenceString == null) {
System.out.println("Please enter the reference string first!");
} else {
System.out.println("Simulating the NEW algorithm...");
simulateNEW();
}
break;
default:
System.out.println("Invalid choice!");
break;
}
System.out.println();
} while (choice != 0);
}
private static void simulateOPT() {
pageFaults = 0;
int len = referenceString.length;
System.out.println("Reference StringtPhysical FramestPage FaultstVictim Pages");
for (int i = 0; i < len; i++) {
int page = referenceString[i];
boolean found = false;
for (int j = 0; j < n; j++) {
if (physicalFrames[j] == page) {
found = true;
break;
}
}
if (!found) {
pageFaults++;
int maxDist = 0;
4. int victimPage = -1;
for (int j = 0; j < n; j++) {
int dist = 0;
for (int k = i + 1; k < len; k++) {
dist++;
if (physicalFrames[j] == referenceString[k]) {
break;
}
}
if (dist > maxDist) {
maxDist = dist;
victimPage = j;
}
}
physicalFrames[victimPage] = page;
victimPages[i] = physicalFrames[victimPage];
} else {
victimPages[i] = -1;
}
System.out.print(page + "ttt");
for (int j = 0; j < n; j++) {
if (physicalFrames[j] == -1) {
System.out.print("- ");
} else {
System.out.print(physicalFrames[j] + " ");
}
}
System.out.print("tt" + pageFaults + "tt");
for (int j = 0; j < len; j++) {
if (victimPages[j] == -1) {
System.out.print("- ");
} else {
System.out.print(victimPages[j] + " ");
}
}
System.out.println();
5. }
System.out.println("nTotal Page Faults: " + pageFaults);
}
private static void simulateNEW() {
pageFaults = 0;
int len = referenceString.length;
int[] lastUsed = new int[10];
for (int i = 0; i < len; i++) {
int page = referenceString[i];
boolean found = false;
for (int j = 0; j < n; j++) {
if (physicalFrames[j] == page) {
found = true;
break;
}
}
if (!found) {
pageFaults++;
int victimIndex = -1;
for (int j = 0; j < n; j++) {
int p = physicalFrames[j];
if (lastUsed[p] == 0) {
victimIndex = j;
break;
}
if (lastUsed[p] < lastUsed[physicalFrames[victimIndex]]) {
victimIndex = j;
}
}
int victimPage = physicalFrames[victimIndex];
victimPages[i] = victimPage;
physicalFrames[victimIndex] = page;
lastUsed[page] = i + 1;
lastUsed[victimPage] = 0;
} else {
6. victimPages[i] = -1;
}
System.out.print(page + "tt");
for (int j = 0; j < n; j++) {
System.out.print(physicalFrames[j] + "t");
}
System.out.print(pageFaults + "tt");
for (int j = 0; j < len; j++) {
if (victimPages[j] != 0) {
System.out.print(victimPages[j] + " ");
}
}
System.out.println();
}
}
}
2 - Input the reference string The user is prompted for a series of [positive integer] numbers that
will constitute the reference string. The maximum length of the reference string should be 20 .
This is normally one of the first options that must be selected by the user. The number of
elements in the reference string must be at least N (otherwise the algorithms don't make any
sense) but should not be larger than 20; each element of the reference string must be an integer
number between 0 and 9 (there are 10 virtual pages). The program must verify these constraints
and issue an error message whenever necessary. After performing several operations from the
menu, the user should be able to select another reference string and work with the new reference
string just as easily as before.