I need the output to look like what is below but it needs to be from this input.
ID: 3571372 • Letter: I
Question
I need the output to look like what is below but it needs to be from this input. Thanks
This is the Input:
8 4 12
4
3
4
6
1
6
4
5
2
4
6
1
This is the Output:
FIFO
Page 4 loaded into Frame 0
Page 3 loaded into Frame 1
Page 4 already in Frame 0
Page 6 loaded into Frame 2
Page 1 loaded into Frame 3
Page 6 already in Frame 2
Page 4 already in Frame 0
Page 4 unloaded from Frame 0, Page 5 loaded into Frame 0
Page 3 unloaded from Frame 1, Page 2 loaded into Frame 1
Page 6 unloaded from Frame 2, Page 4 loaded into Frame 2
Page 1 unloaded from Frame 3, Page 6 loaded into Frame 3
Page 5 unloaded from Frame 0, Page 1 loaded into Frame 0
LRU
Page 4 loaded into Frame 0
Page 3 loaded into Frame 1
Page 4 already in Frame 0
Page 6 loaded into Frame 2
Page 1 loaded into Frame 3
Page 6 already in Frame 2
Page 4 already in Frame 0
Page 3 unloaded from Frame 1, Page 5 loaded into Frame 1
Page 1 unloaded from Frame 3, Page 2 loaded into Frame 3
Page 4 already in Frame 0
Page 6 loaded in Frame 2
Page 5 unloaded from Frame 1, Page 1 loaded into Frame 1
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class pgReplace {
public static void main(String[] args) throws IOException {
ArrayList<Integer> ref = new ArrayList<Integer>();//creating new HashSet
try {
Scanner filein = new Scanner(new File("input.txt")); //open file
while(filein.hasNext()) {
String frame = filein.next();
int frm = Integer.parseInt(frame);
ref.add(frm);// add the integers to HashSet.
}//end of while
filein.close();//close the file
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}
//display the size if it the set is not empty
if(!ref.isEmpty()){
int size = ref.size();
System.out.println("The length of the Reference String is: " + size);
}
//printing the read reference string using an iterator
Iterator<Integer> iter = ref.iterator();
while (iter.hasNext())
System.out.print(iter.next() +" ");
//converting the arraylist to an array
Integer frame[] = new Integer[ref.size()];
frame = ref.toArray(frame);
System.out.printf(" Select the Page Replacement Algorithm: ");
System.out.printf(" 1.FIFO 2.LRU ? ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next(); // discard next token, which isn't a valid int
}
int ch = sc.nextInt();
switch(ch){
case 1: fifo(frame,ref.size());
break;
case 2: lru(frame,ref.size());
break;
default: System.out.printf(" Invlaid Choice");
}
}
/**Simulates LRU page replacement for given reference string
* @throws IOException **/
public static void lru(Integer[] page, int n) throws IOException {
int [] frame = new int[10];
int []used = new int[10];
int index = 0;
int i,j,k,temp;
int flag=0,pf=0;
BufferedWriter write = new BufferedWriter(new FileWriter("output.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf(" LRU Page Replacement");
System.out.printf(" Enter number of Frames: ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next(); // discard next token, which isn't a valid int
}
int nf = sc.nextInt();
for(i=0;i<nf;i++)
frame[i]= -1;
for(i=0;i<n;i++){
flag=0;
for(j=0;j<nf;j++){
if(frame[j]==page[i]){//no fault
System.out.printf(" %d: ", page[i]);
out.printf(" %d: ", page[i]);
flag=1;
break;
}
}
if(flag==0){//fault occurs
for(j=0;j<nf;j++)
used[j]=0;//all unused initially
//moving through pages and searching recently used pages
try{
for(j = 0,temp= i-1;j < nf-1;j++,temp--){
for(k = 0;k < nf;k++){
if(frame[k]==page[temp])
used[k]=1;
//mark the recently used pages
}
}
}
catch(ArrayIndexOutOfBoundsException e){
}
for(j=0;j<nf;j++)
if(used[j]==0)
index=j;
//replace the lru page with new page
frame[index]=page[i];
System.out.printf(" %d: ", page[i]);
System.out.printf("--->F ");
out.printf(" %d: ", page[i]);
out.printf("--->F ");
pf++;//no of page faults
}
for(k= nf-1;k>=0;k--)
if(frame[k] != -1){
System.out.printf(" %d",frame[k]);//print frames
out.printf(" %d",frame[k]);
}
}
System.out.printf(" Number of page faults is: %d ",pf);
out.printf(" Number of page faults is: %d ",pf);
out.close();
write.close();
}
/**Simulates FIFO page replacement for given reference string
* @throws IOException **/
public static void fifo(Integer[] pages, int pg) throws IOException {
int [] frame = new int[25];
int i,k,avail,count=0;
BufferedWriter write = new BufferedWriter(new FileWriter("output.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf(" FIFO Page Replacement");
System.out.printf(" Enter number of Frames: ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next(); // discard next token, which isn't a valid int
}
int nof = sc.nextInt();
for(i=0;i<nof;i++)
frame[i]= -1;
int j=0;
System.out.printf(" ");
out.printf(" ");
for(i=0;i<pg;i++){
System.out.printf("%d ",pages[i]);
out.printf("%d ",pages[i]);
avail=0;
for(k=0;k<nof;k++)
if(frame[k]==pages[i])
avail=1;
if (avail==0){
frame[j]=pages[i];
j=(j+1) % nof;
count++;
for(k=0;k<nof;k++)
if(frame[k]!=-1){
System.out.printf("%d",frame[k]);
out.printf("%d",frame[k]);
}
System.out.printf("-->F");
out.printf("-->F");
}
if(avail==1){
for(k=0;k<nof;k++)
if(frame[k]!=-1){
System.out.printf("%d",frame[k]);
out.printf("%d",frame[k]);
}
}
System.out.printf(" ");
out.printf(" ");
}
System.out.printf(" No of Faults: %d",count);
out.printf(" No of Faults: %d",count);
out.close();
write.close();
}
/**
* Lets the user select an input file using a standard file
* selection dialog box. If the user cancels the dialog
* without selecting a file, the return value is null.
*/
// static File getInputFileNameFromUser() {
// JFileChooser fileDialog = new JFileChooser();
// fileDialog.setDialogTitle("Select File for Input");
// int option = fileDialog.showOpenDialog(null);
// if (option != JFileChooser.APPROVE_OPTION)
// return null;
// else
// return fileDialog.getSelectedFile();
// }//end of choosing file
}
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class pgReplace {
public static void main(String[] args) throws IOException {
ArrayList<Integer> ref = new ArrayList<>();//creating new HashSet
try {
Scanner filein = new Scanner(new File("input.txt")); //open file
while(filein.hasNext()) {
String frame = filein.next();
int frm = Integer.parseInt(frame);
ref.add(frm);// add the integers to HashSet.
}//end of while
filein.close();//close the file
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}
//display the size if it the set is not empty
if(!ref.isEmpty()){
int size = ref.size();
System.out.println("The length of the Reference String is: " + size);
}
//printing the read reference string using an iterator
Iterator<Integer> iter = ref.iterator();
while (iter.hasNext())
System.out.print(iter.next() +" ");
//converting the arraylist to an array
Integer frame[] = new Integer[ref.size()];
frame = ref.toArray(frame);
System.out.printf(" Select the Page Replacement Algorithm: ");
System.out.printf(" 1.FIFO 2.LRU ? ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next(); // discard next token, which isn't a valid int
}
int ch = sc.nextInt();
switch(ch){
case 1: fifo(frame,ref.size());
break;
case 2: lru(frame,ref.size());
break;
default: System.out.printf(" Invlaid Choice");
}
}
/**Simulates LRU page replacement for given reference string
* @throws IOException **/
public static void lru(Integer[] page, int n) throws IOException {
int [] frame = new int[10];
int []used = new int[10];
int index = 0;
int i,j,k,temp;
int flag=0,pf=0;
BufferedWriter write = new BufferedWriter(new FileWriter("output.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf(" LRU Page Replacement");
System.out.printf(" Enter number of Frames: ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next(); // discard next token, which isn't a valid int
}
int nf = sc.nextInt();
for(i=0;i<nf;i++)
frame[i]= -1;
for(i=0;i<n;i++){
flag=0;
for(j=0;j<nf;j++){
if(frame[j]==page[i]){//no fault
//System.out.printf(" %d: ", page[i]);
//out.printf(" %d: ", page[i]);
flag=1;
int l = (j+1)%nf;
System.out.printf(" Page %d already in frame %d", page[i], l);
out.printf(" Page %d already in frame %d", page[i], j);
break;
}
}
if(flag==0){//fault occurs
for(j=0;j<nf;j++)
used[j]=0;//all unused initially
//moving through pages and searching recently used pages
try{
for(j = 0,temp= i-1;j < nf-1;j++,temp--){
for(k = 0;k < nf;k++){
if(frame[k]==page[temp])
used[k]=1;
//mark the recently used pages
}
}
}
catch(ArrayIndexOutOfBoundsException e){
}
for(j=0;j<nf;j++)
if(used[j]==0)
index=j;
int l = nf - 1 - index;
if(frame[index] != -1){
System.out.printf(" Page %d unloaded from Frame %d, ", frame[index], l);
out.printf(" Page %d unloaded from Frame %d, ", frame[index], l);
}
else{
System.out.printf(" ");
out.printf(" ");
}
//replace the lru page with new page
frame[index]=page[i];
/*System.out.printf(" %d: ", page[i]);
System.out.printf("--->F ");
out.printf(" %d: ", page[i]);
out.printf("--->F ");
*/
System.out.printf("Page %d loaded into Frame %d", page[i], l);
out.printf("Page %d loaded into Frame %d", page[i], l);
pf++;//no of page faults
}
/*for(k= nf-1;k>=0;k--)
if(frame[k] != -1){
System.out.printf(" %d",frame[k]);//print frames
out.printf(" %d",frame[k]);
}*/
}
System.out.printf(" Number of page faults is: %d ",pf);
out.printf(" Number of page faults is: %d ",pf);
out.close();
write.close();
}
/**Simulates FIFO page replacement for given reference string
* @throws IOException **/
public static void fifo(Integer[] pages, int pg) throws IOException {
int [] frame = new int[25];
int i,k,avail,count=0;
BufferedWriter write = new BufferedWriter(new FileWriter("output.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf(" FIFO Page Replacement");
System.out.printf(" Enter number of Frames: ");
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
sc.next(); // discard next token, which isn't a valid int
}
int nof = sc.nextInt();
for(i=0;i<nof;i++)
frame[i]= -1;
int j=0;
System.out.printf(" ");
out.printf(" ");
for(i=0;i<pg;i++){
//System.out.printf("%d ",pages[i]);
//out.printf("%d ",pages[i]);
avail=0;
for(k=0;k<nof;k++)
/*if(frame[k]==pages[i])
avail=1;*/
if(frame[k] == pages[i]){
avail = 1;
System.out.printf("Page %d already in Frame %d", pages[i], k);
out.printf("Page %d already in Frame %d", pages[i], k);
}
if (avail==0){
if(frame[j] != -1){
System.out.printf("Page %d unloaded from Frame %d, ", frame[j], j);
out.printf("Page %d unloaded from Frame %d, ", frame[j], j);
}
frame[j]=pages[i];
int n = j;
j=(j+1) % nof;
count++;
/*for(k=0;k<nof;k++)
if(frame[k]!=-1){
System.out.printf("%d",frame[k]);
out.printf("%d",frame[k]);
}
System.out.printf("-->F");
out.printf("-->F");*/
System.out.printf("Page %d loaded into Frame %d", pages[i], n);
out.printf("Page %d loaded into Frame %d", pages[i], n);
}
/*if(avail==1){
for(k=0;k<nof;k++)
if(frame[k]!=-1){
System.out.printf("%d",frame[k]);
out.printf("%d",frame[k]);
}
}*/
System.out.printf(" ");
out.printf(" ");
}
System.out.printf(" No of Faults: %d",count);
out.printf(" No of Faults: %d",count);
out.close();
write.close();
}
/**
* Lets the user select an input file using a standard file
* selection dialog box. If the user cancels the dialog
* without selecting a file, the return value is null.
*/
// static File getInputFileNameFromUser() {
// JFileChooser fileDialog = new JFileChooser();
// fileDialog.setDialogTitle("Select File for Input");
// int option = fileDialog.showOpenDialog(null);
// if (option != JFileChooser.APPROVE_OPTION)
// return null;
// else
// return fileDialog.getSelectedFile();
// }//end of choosing file
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.