SlideShare a Scribd company logo
1 of 6
Download to read offline
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 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();
}
}
}
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.

More Related Content

Similar to import java.util.Scanner;public class Main {private static i.pdf

Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfarishmarketing21
 
Code javascript
Code javascriptCode javascript
Code javascriptRay Ray
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfRohitkumarYadav80
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfkokah57440
 
Java Question---------------------------FacebookApp(Main).jav.pdf
Java Question---------------------------FacebookApp(Main).jav.pdfJava Question---------------------------FacebookApp(Main).jav.pdf
Java Question---------------------------FacebookApp(Main).jav.pdfambersushil
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docxdavinci54
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdfapexelectronices01
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdfkavithaarp
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfeyewatchsystems
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfkostikjaylonshaewe47
 

Similar to import java.util.Scanner;public class Main {private static i.pdf (20)

Studyx4
Studyx4Studyx4
Studyx4
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
Code javascript
Code javascriptCode javascript
Code javascript
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
 
Java Question---------------------------FacebookApp(Main).jav.pdf
Java Question---------------------------FacebookApp(Main).jav.pdfJava Question---------------------------FacebookApp(Main).jav.pdf
Java Question---------------------------FacebookApp(Main).jav.pdf
 
Lab4
Lab4Lab4
Lab4
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
 
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
PrintDiamond.javaimport java.util.Scanner;class PrintDiamond.pdf
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
 
Java
JavaJava
Java
 
Oot practical
Oot practicalOot practical
Oot practical
 
JAVA.pdf
JAVA.pdfJAVA.pdf
JAVA.pdf
 
54240326 (1)
54240326 (1)54240326 (1)
54240326 (1)
 

More from stopgolook

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

More from stopgolook (14)

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

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 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.