I'm having an issue with the simulateOPT() method
this is the purpose:
this is my code:
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 " + String.join("", strArr) + ".");
}
}
}
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 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;
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();
}
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 {
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();
}
}
} 3-Simulate the OPT algorithm This option must first verify that a correct N and a correct
reference string were already provided by the user; otherwise an error message should be issued
and the program returns to the menu. If all the input data ( N and the reference string) is available
and correct, the simulation begins by printing the initial (empty) simulation table, for example:
Hint 1: Instead of the above reference string, the table will contain the ACTUAL reference string
that was entered by the user; of course, everyone can use their reference strings for testing the
correctness of the implementation. Using the reference strings from the readings to test your
code is a great idea. Hint 2: Instead of having 4 physical frames in the simulation table, we must
actually have N physical frames. So if the user has entered the value 6 for N using option 1 of the
menu, then the simulation table should look like this: Hint 3: Being a text only interface, clearly
we cannot display such a nice table, but rather an approximation of it; the table could instead
look like this or similar: The user must proceed with the STEP by STEP simulation of the OPT
algorithm by pressing a key after each step of the algorithm; just as in the examples from the
module 5 readings, each step of the simulation will fill a new column of the simulation table
(previous steps must also be visible) and the table is re-printed on the screen after each step. This
way, the user has an overview of all the previous steps and also of the current step. In the end,
the table will be completely filled with information, and the user will have the complete view of
the simulation. The total number of faults must also be displayed. In other words, the simulation
must follow very closely the examples of Demand Paging algorithms from Module 5 of this
course. Printing the explanations is optional.

More Related Content

PDF
import java.util.Scanner;public class Main {private static i.pdf
PDF
The java code works, I just need it to display the results as in t.pdf
PDF
Create a menu-driven program that will accept a collection of non-ne.pdf
PDF
Sharable_Java_Python.pdf
DOCX
Java Practical1 based on Basic assignment
PDF
I am constantly getting errors and cannot figure this out. Please he.pdf
PPT
07-Basic-Input-Output.ppt
DOCX
DS LAB RECORD.docx
import java.util.Scanner;public class Main {private static i.pdf
The java code works, I just need it to display the results as in t.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
Sharable_Java_Python.pdf
Java Practical1 based on Basic assignment
I am constantly getting errors and cannot figure this out. Please he.pdf
07-Basic-Input-Output.ppt
DS LAB RECORD.docx

Similar to Im having an issue with the simulateOPT() methodthis is the p.pdf (20)

PPT
Algorithms with-java-1.0
DOCX
cs3381-object oriented programming-ab-manual
PPTX
Lab01.pptx
PPT
ch05-program-logic-indefinite-loops.ppt
DOCX
Java file
DOCX
Java file
PDF
Simple Java Program for beginner with easy method.pdf
PDF
Sam wd programs
PDF
Simple 27 Java Program on basic java syntax
PPT
Ch5(loops)
PDF
Refer to my progress on this assignment belowIn this problem you w.pdf
PDF
Java Unit 1 Project
DOCX
PDF
JAVA PRACTICE QUESTIONS v1.4.pdf
PDF
VTU DSA Lab Manual
PDF
Sorting_Algoritm-computee-scienceggn.pdf
PDF
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
PPTX
Lab101.pptx
PDF
Java_Programming_by_Example_6th_Edition.pdf
PDF
import java.util.;public class Program{public static void.pdf
Algorithms with-java-1.0
cs3381-object oriented programming-ab-manual
Lab01.pptx
ch05-program-logic-indefinite-loops.ppt
Java file
Java file
Simple Java Program for beginner with easy method.pdf
Sam wd programs
Simple 27 Java Program on basic java syntax
Ch5(loops)
Refer to my progress on this assignment belowIn this problem you w.pdf
Java Unit 1 Project
JAVA PRACTICE QUESTIONS v1.4.pdf
VTU DSA Lab Manual
Sorting_Algoritm-computee-scienceggn.pdf
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
Lab101.pptx
Java_Programming_by_Example_6th_Edition.pdf
import java.util.;public class Program{public static void.pdf
Ad

More from stopgolook (14)

PDF
Jordano Food Products Supply Chain Profile Jordano Foods Tracie Shan.pdf
PDF
Java Code The traditional way to deal with these in Parsers is the .pdf
PDF
Javai have to make a method that takes a linked list and then retu.pdf
PDF
J.M. Baker worked as a traditional land use researcher and consultan.pdf
PDF
IT Project Management homework Identify any project of your choice.pdf
PDF
INSTRUCTIONSDevelop, and present a plan and business case for an a.pdf
PDF
In the realm of professional dynamics, understanding and appreciating .pdf
PDF
import React, { useEffect } from react;import { BrowserRouter as.pdf
PDF
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
PDF
Im trying to define a class in java but I seem to be having a bit o.pdf
PDF
in c languageTo determine the maximum string length, we need to .pdf
PDF
In 2011, the head of the Presidential Protection Force (for purposes.pdf
PDF
If a taxpayer has investment income that exceeds a certain threshold.pdf
PDF
I. Naive Robot Navigation ProblemDesign a program that uses the B.pdf
Jordano Food Products Supply Chain Profile Jordano Foods Tracie Shan.pdf
Java Code The traditional way to deal with these in Parsers is the .pdf
Javai have to make a method that takes a linked list and then retu.pdf
J.M. Baker worked as a traditional land use researcher and consultan.pdf
IT Project Management homework Identify any project of your choice.pdf
INSTRUCTIONSDevelop, and present a plan and business case for an a.pdf
In the realm of professional dynamics, understanding and appreciating .pdf
import React, { useEffect } from react;import { BrowserRouter as.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
Im trying to define a class in java but I seem to be having a bit o.pdf
in c languageTo determine the maximum string length, we need to .pdf
In 2011, the head of the Presidential Protection Force (for purposes.pdf
If a taxpayer has investment income that exceeds a certain threshold.pdf
I. Naive Robot Navigation ProblemDesign a program that uses the B.pdf
Ad

Recently uploaded (20)

PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
Computer Architecture Input Output Memory.pptx
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
20th Century Theater, Methods, History.pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
advance database management system book.pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Trump Administration's workforce development strategy
PDF
IGGE1 Understanding the Self1234567891011
PDF
International_Financial_Reporting_Standa.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
History, Philosophy and sociology of education (1).pptx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
My India Quiz Book_20210205121199924.pdf
Computer Architecture Input Output Memory.pptx
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
20th Century Theater, Methods, History.pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Share_Module_2_Power_conflict_and_negotiation.pptx
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
advance database management system book.pdf
Empowerment Technology for Senior High School Guide
FORM 1 BIOLOGY MIND MAPS and their schemes
Trump Administration's workforce development strategy
IGGE1 Understanding the Self1234567891011
International_Financial_Reporting_Standa.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc

Im having an issue with the simulateOPT() methodthis is the p.pdf

  • 1. I'm having an issue with the simulateOPT() method this is the purpose: this is my code: 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:
  • 2. 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 " + String.join("", strArr) + "."); } } }
  • 3. 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 StringtPhysical FramestPage FaultstVictim Pages");
  • 4. 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 + "ttt"); for (int j = 0; j < n; j++) { if (physicalFrames[j] == -1) { System.out.print("- ");
  • 5. } 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(); } 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++) {
  • 6. 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 + "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(); } } } 3-Simulate the OPT algorithm This option must first verify that a correct N and a correct reference string were already provided by the user; otherwise an error message should be issued and the program returns to the menu. If all the input data ( N and the reference string) is available and correct, the simulation begins by printing the initial (empty) simulation table, for example: Hint 1: Instead of the above reference string, the table will contain the ACTUAL reference string
  • 7. that was entered by the user; of course, everyone can use their reference strings for testing the correctness of the implementation. Using the reference strings from the readings to test your code is a great idea. Hint 2: Instead of having 4 physical frames in the simulation table, we must actually have N physical frames. So if the user has entered the value 6 for N using option 1 of the menu, then the simulation table should look like this: Hint 3: Being a text only interface, clearly we cannot display such a nice table, but rather an approximation of it; the table could instead look like this or similar: The user must proceed with the STEP by STEP simulation of the OPT algorithm by pressing a key after each step of the algorithm; just as in the examples from the module 5 readings, each step of the simulation will fill a new column of the simulation table (previous steps must also be visible) and the table is re-printed on the screen after each step. This way, the user has an overview of all the previous steps and also of the current step. In the end, the table will be completely filled with information, and the user will have the complete view of the simulation. The total number of faults must also be displayed. In other words, the simulation must follow very closely the examples of Demand Paging algorithms from Module 5 of this course. Printing the explanations is optional.