Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have provided my code and screenshots of the errors I am receiving, as well as screenshots of the assignment I am working on: I
I have provided my code and screenshots of the errors I am receiving, as well as screenshots of the assignment I am working on:
I am running into an issue with the OPT and NEW algorithm. Here are some screenshots:
I'm not sure what's causing the errors or how to get the program to run as it should. This is what it should look like:
Here's my code so far:
import java.util.Scanner; public class DemandPagingSimulator { 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; } System.out.print(page + "\t\t\t"); for (int j = 0; j < n; j++) { if (physicalFrames[j] == -1) { System.out.print("- "); } else { System.out.print(physicalFrames[j] + " "); } } System.out.print("\t\t" + pageFaults + "\t\t"); for (int j = 0; j < len; j++) { if (victimPages[j] == -1) { System.out.print("- "); } else { System.out.print(victimPages[j] + " "); } } System.out.println(); } System.out.println(" Total 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 { victimPages[i] = -1; } System.out.print(page + "\t\t"); for (int j = 0; j < n; j++) { System.out.print(physicalFrames[j] + "\t"); } System.out.print(pageFaults + "\t\t"); for (int j = 0; j < len; j++) { if (victimPages[j] != 0) { System.out.print(victimPages[j] + " "); } } System.out.println(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started