SlideShare a Scribd company logo
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
stopgolook
 
PDF
The java code works, I just need it to display the results as in t.pdf
akaluza07
 
PDF
Create a menu-driven program that will accept a collection of non-ne.pdf
rajeshjangid1865
 
PDF
Sharable_Java_Python.pdf
ICADCMLTPC
 
DOCX
Java Practical1 based on Basic assignment
ashwinibhosale27
 
PDF
I am constantly getting errors and cannot figure this out. Please he.pdf
allystraders
 
PPT
07-Basic-Input-Output.ppt
Ajenkris Kungkung
 
DOCX
DS LAB RECORD.docx
davinci54
 
import java.util.Scanner;public class Main {private static i.pdf
stopgolook
 
The java code works, I just need it to display the results as in t.pdf
akaluza07
 
Create a menu-driven program that will accept a collection of non-ne.pdf
rajeshjangid1865
 
Sharable_Java_Python.pdf
ICADCMLTPC
 
Java Practical1 based on Basic assignment
ashwinibhosale27
 
I am constantly getting errors and cannot figure this out. Please he.pdf
allystraders
 
07-Basic-Input-Output.ppt
Ajenkris Kungkung
 
DS LAB RECORD.docx
davinci54
 

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

PPT
Algorithms with-java-1.0
BG Java EE Course
 
DOCX
cs3381-object oriented programming-ab-manual
karthikeyan411470
 
PPTX
Lab01.pptx
KimVeeL
 
PPT
ch05-program-logic-indefinite-loops.ppt
Mahyuddin8
 
DOCX
Java file
simarsimmygrewal
 
DOCX
Java file
simarsimmygrewal
 
PDF
Simple Java Program for beginner with easy method.pdf
ashwinibhosale27
 
PDF
Sam wd programs
Soumya Behera
 
PDF
Simple 27 Java Program on basic java syntax
ashwinibhosale27
 
PPT
Ch5(loops)
Uğurcan Uzer
 
PDF
Refer to my progress on this assignment belowIn this problem you w.pdf
arishmarketing21
 
PDF
Java Unit 1 Project
Matthew Abela Medici
 
DOCX
Java programs
Dr.M.Karthika parthasarathy
 
PDF
JAVA PRACTICE QUESTIONS v1.4.pdf
RohitkumarYadav80
 
PDF
VTU DSA Lab Manual
AkhilaaReddy
 
PDF
Sorting_Algoritm-computee-scienceggn.pdf
Mohammed472103
 
PDF
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
deepua8
 
PPTX
Lab101.pptx
KimVeeL
 
PDF
Java_Programming_by_Example_6th_Edition.pdf
JayveeCultivo
 
PDF
import java.util.;public class Program{public static void.pdf
optokunal1
 
Algorithms with-java-1.0
BG Java EE Course
 
cs3381-object oriented programming-ab-manual
karthikeyan411470
 
Lab01.pptx
KimVeeL
 
ch05-program-logic-indefinite-loops.ppt
Mahyuddin8
 
Java file
simarsimmygrewal
 
Java file
simarsimmygrewal
 
Simple Java Program for beginner with easy method.pdf
ashwinibhosale27
 
Sam wd programs
Soumya Behera
 
Simple 27 Java Program on basic java syntax
ashwinibhosale27
 
Ch5(loops)
Uğurcan Uzer
 
Refer to my progress on this assignment belowIn this problem you w.pdf
arishmarketing21
 
Java Unit 1 Project
Matthew Abela Medici
 
JAVA PRACTICE QUESTIONS v1.4.pdf
RohitkumarYadav80
 
VTU DSA Lab Manual
AkhilaaReddy
 
Sorting_Algoritm-computee-scienceggn.pdf
Mohammed472103
 
ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf
deepua8
 
Lab101.pptx
KimVeeL
 
Java_Programming_by_Example_6th_Edition.pdf
JayveeCultivo
 
import java.util.;public class Program{public static void.pdf
optokunal1
 
Ad

More from stopgolook (14)

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

Recently uploaded (20)

PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 

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.