Here is a complete, step-by-step guide to setting up and running all 11 of your Java practicals
in Eclipse, based on the files you provided.
🚀 Initial Eclipse Setup (One-Time Only)
First, let's create a single project to hold all your practicals.
1. Open Eclipse: Launch your Eclipse IDE.
2. Create a Java Project:
○ Go to the menu bar and click File > New > Java Project.
○ In the "Project name" field, type SposLp1Practicals.
○ Click Finish.
3. Your Project Structure: You will now see your project in the "Package Explorer" on the
left. It will have a src folder. This is where all your code will go.
## 1️⃣ & 2️⃣: Two-Pass Assembler (Prac 1 & 2)
These two programs work together. Pass 1 creates files that Pass 2 reads.
1. Create the Package
● In the "Package Explorer," right-click the src folder.
● Select New > Package.
● For the "Name," type assembler1111.
● Click Finish.
2. Practical 1: Pass 1 Assembler
This program will read a hard-coded assembly program and create symtab.txt and
intermediate.txt.
● Create Class: Right-click the new assembler package and select New > Class.
● Name: Pass1_Assembler
● Click Finish.
● Copy & Paste Code: Copy the code below and paste it into the Pass1_Assembler.java
file, replacing all existing text.
Java
// Source: Practical1.pdf
package assembler;
import java.io.*;
public class Pass1_Assembler {
public static void main(String[] args) throws IOException {
String[][] a = {
{"", "START", "101", ""},
{"", "MOVER", "BREG", "ONE"},
{"AGAIN", "MULT", "BREG", "TERM"},
{"", "MOVER", "CREG", "TERM"},
{"", "ADD", "CREG", "N"},
{"", "MOVEM", "CREG", "TERM"},
{"N", "DS", "2", ""},
{"RESULT", "DS", "2", ""},
{"ONE", "DC", "1", ""},
{"TERM", "DS", "1", ""},
{"", "END", "", ""}
};
int lc = Integer.parseInt(a[0][2]);
BufferedWriter symtab = new BufferedWriter(new FileWriter("symtab.txt"));
BufferedWriter inter = new BufferedWriter(new FileWriter("intermediate.txt"));
for (int i = 1; i < a.length; i++) {
if (!a[i][0].equals("")) {
symtab.write(a[i][0] + "\t" + lc + "\n");
}
inter.write(lc + "\t" + a[i][1] + "\t" + a[i][2] + "\t" + a[i][3] + "\n");
if (a[i][1].equals("DS")) {
lc += Integer.parseInt(a[i][2]);
} else if (a[i][1].equals("DC")) {
lc++;
} else if (!a[i][1].equals("END") && !a[i][1].equals("START")){
lc++;
}
}
symtab.close();
inter.close();
System.out.println("Pass-I complete. Symbol Table and Intermediate Code generated.");
System.out.println("Right-click your project 'SposLp1Practicals' and click 'Refresh'.");
System.out.println("You will see symtab.txt and intermediate.txt.");
}
}
● Run: Right-click anywhere in the code and select Run As > Java Application.
● Output: The Eclipse Console will print: Pass-I complete...2. Two new files, symtab.txt and
intermediate.txt, will be created in your main SposLp1Practicals project folder (not inside
src).
3. Practical 2: Pass 2 Assembler
This program reads the files you just created (symtab.txt and intermediate.txt) and produces
the final target.txt.
● Create Class: Right-click the assembler package again and select New > Class.
● Name: Pass2_Assembler
● Click Finish.
● Copy & Paste Code:
Java
// Source: Practical1.pdf
package assembler;
import java.io.*;
import java.util.*;
public class Pass2_Assembler {
public static void main(String[] args) throws IOException {
BufferedReader inter = new BufferedReader(new FileReader("intermediate.txt"));
BufferedReader symtab = new BufferedReader(new FileReader("symtab.txt"));
BufferedWriter target = new BufferedWriter(new FileWriter("target.txt"));
Map<String, String> symTable = new HashMap<>();
String line;
while ((line = symtab.readLine()) != null) {
String[] parts = line.trim().split("\t");
if (parts.length == 2) {
symTable.put(parts[0], parts[1]);
}
}
symtab.close();
List<String> inst = Arrays.asList("STOP", "ADD", "SUB", "MULT", "MOVER", "MOVEM", "COMP",
"BC", "DIV", "READ", "PRINT");
List<String> reg = Arrays.asList("AREG", "BREG", "CREG", "DREG");
while ((line = inter.readLine()) != null) {
String[] parts = line.trim().split("\t");
if (parts.length < 2) continue;
String lc = parts[0];
String opcode = parts[1];
String operand1 = (parts.length > 2) ? parts[2] : "";
String operand2 = (parts.length > 3) ? parts[3] : "";
if (opcode.equals("START") || opcode.equals("END") || opcode.equals("DS")) continue;
if (opcode.equals("DC")) {
target.write(lc + "\t" + "00\t00\t" + String.format("%03d", Integer.parseInt(operand1))
+ "\n");
continue;
}
int opCodeVal = inst.indexOf(opcode);
int regCodeVal = reg.indexOf(operand1);
String address = symTable.getOrDefault(operand2, "000");
target.write(lc + "\t" + String.format("%02d", opCodeVal) + "\t" +
String.format("%02d", regCodeVal + 1) + "\t" + address + "\n");
}
inter.close();
target.close();
System.out.println("Pass-II complete. Target code generated in target.txt.");
System.out.println("Refresh your project to see target.txt");
}
}
● Run: You must run Pass1_Assembler first. After that, run Pass2_Assembler.java (Run As
> Java Application).
● Output: The Console will print: Pass-II complete...3. A new file, target.txt4, will be created
in your project folder.
## 3️⃣ & 4️⃣: Two-Pass Macro Processor (Prac 3 & 4)
This is another two-pass system.
1. Create the Package
● Right-click the src folder, select New > Package.
● Name: macroprocessor5555.
● Click Finish.
2. Practical 3: Pass 1 Macro Processor
This program generates the Macro Name Table (MNT), Macro Definition Table (MDT), and
intermediate code.
● Create Class: Right-click the macroprocessor package, select New > Class.
● Name: Pass1_Macro
● Click Finish.
● Copy & Paste Code:
Java
// Source: Practical2.pdf
package macroprocessor;
import java.io.*;
import java.util.*;
public class Pass1_Macro {
public static void main(String[] args) throws IOException {
String[] code = {
"MACRO",
"INCR &ARG1",
"A 1,&ARG1",
"MEND",
"START 101",
"INCR TERM",
"MOVEM AREG, TERM",
"END"
};
List<String[]> MNT = new ArrayList<>();
List<String> MDT = new ArrayList<>();
BufferedWriter inter = new BufferedWriter(new FileWriter("intermediate.txt"));
BufferedWriter mntFile = new BufferedWriter(new FileWriter("mnt.txt"));
BufferedWriter mdtFile = new BufferedWriter(new FileWriter("mdt.txt"));
boolean isMacro = false;
int mdtIndex = 0;
for (int i = 0; i < code.length; i++) {
String line = code[i];
String[] parts = line.trim().split("\\s+", 2);
if (parts[0].equals("MACRO")) {
isMacro = true;
String[] defParts = code[i + 1].trim().split("\\s+", 2);
String macroName = defParts[0];
MNT.add(new String[] {macroName, String.valueOf(mdtIndex)});
MDT.add(code[i+1]); // Add the macro definition line (e.g., INCR &ARG1)
mdtIndex++;
i++; // skip macro name line
continue;
}
if (parts[0].equals("MEND")) {
MDT.add("MEND");
mdtIndex++;
isMacro = false;
continue;
}
if (isMacro) {
MDT.add(line);
mdtIndex++;
} else {
inter.write(line + "\n");
}
}
for (String[] entry : MNT) {
mntFile.write(entry[0] + "\t" + entry[1] + "\n");
}
for (int i = 0; i < MDT.size(); i++) {
mdtFile.write(i + "\t" + MDT.get(i) + "\n");
}
inter.close();
mntFile.close();
mdtFile.close();
System.out.println("Macro Pass-I complete. MNT, MDT and Intermediate Code generated.");
System.out.println("Refresh your project to see mnt.txt, mdt.txt, and intermediate.txt.");
}
}
● Run: Run the Pass1_Macro.java file.
● Output: The console will print: Macro Pass-I complete...6. Three files (mnt.txt, mdt.txt,
intermediate.txt) will be created in your project folder.
3. Practical 4: Pass 2 Macro Processor
This program expands the macros from the intermediate code.
Important Note: The code from your PDF 7 simulates reading the MNT and MDT.
It does not read the files created by Pass 1. It has the data hard-coded inside it
for demonstration.
● Create Class: Right-click the macroprocessor package, select New > Class.
● Name: Pass2_Macro
● Click Finish.
● Copy & Paste Code:
Java
// Source: Practical2.pdf
package macroprocessor;
import java.io.*;
import java.util.*;
public class Pass2_Macro {
private final Map<String, Integer> MNT = new HashMap<>();
private final List<String> MDT = new ArrayList<>();
private final List<String> intermediateCode = new ArrayList<>();
private final List<String> outputCode = new ArrayList<>();
public Pass2_Macro() {
// Simulating MNT, MDT, and intermediate code from Pass-I [cite: 123-130]
MNT.put("INCR", 0);
MDT.add("INCR &ARG1"); // Index 0
MDT.add("A 1,&ARG1"); // Index 1
MDT.add("MEND"); // Index 2
// Simulating intermediate code (from intermediate.txt)
intermediateCode.add("START 101");
intermediateCode.add("INCR TERM");
intermediateCode.add("MOVEM AREG, TERM");
intermediateCode.add("END");
}
public void expandMacros() {
for (String line : intermediateCode) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length == 0) continue;
String macroName = tokens[0];
if (MNT.containsKey(macroName)) { // Check if it's a macro call
int index = MNT.get(macroName);
String actualArg = tokens.length > 1 ? tokens[1] : "";
String header = MDT.get(index);
String formalArg = header.split("\\s+")[1]; // Gets "&ARG1"
index++; // Skip the header
while (!MDT.get(index).equalsIgnoreCase("MEND")) {
String defLine = MDT.get(index);
String expandedLine = defLine.replace(formalArg, actualArg);
outputCode.add(expandedLine);
index++;
}
} else {
outputCode.add(line);
}
}
}
public void displayOutput() {
System.out.println("=== Final Output Code ===");
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
for (String line : outputCode) {
System.out.println(line);
writer.write(line + "\n");
}
System.out.println("Output written to output.txt");
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
public static void main(String[] args) {
Pass2_Macro pass2 = new Pass2_Macro();
pass2.expandMacros();
pass2.displayOutput();
}
}
● Run: Run the Pass2_Macro.java file.
● Output: The console will print the expanded code and a success message 8. A new file,
output.txt9, will be created.
## 5️⃣, 6️⃣, 7️⃣, 8️⃣: CPU Scheduling (Prac 5-8)
These are four separate programs. They all run by taking input from the Eclipse console.
1. Create the Package
● Right-click the src folder, select New > Package.
● Name: cpu_scheduling
● Click Finish.
2. Practical 5: FCFS
● Create Class: Right-click cpu_scheduling, select New > Class.
● Name: FCFS
● Click Finish.
● Copy & Paste Code:
Java
// Source: SPOS OP.pdf (Pages 15-17)
package cpu_scheduling;
import java.util.Scanner;
public class FCFS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
int pid[] = new int[n]; int at[] = new int[n]; int bt[] = new int[n];
int ct[] = new int[n]; int tat[] = new int[n]; int wt[] = new int[n];
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
System.out.print("Enter Arrival Time for P" + pid[i] + ": ");
at[i] = sc.nextInt();
System.out.print("Enter Burst Time for P" + pid[i] + ": ");
bt[i] = sc.nextInt();
}
// Sort by Arrival Time [cite: 1220]
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (at[j] > at[j + 1]) {
int tmp = at[j]; at[j] = at[j + 1]; at[j + 1] = tmp;
tmp = bt[j]; bt[j] = bt[j + 1]; bt[j + 1] = tmp;
tmp = pid[j]; pid[j] = pid[j + 1]; pid[j + 1] = tmp;
}
}
}
ct[0] = at[0] + bt[0]; // [cite: 1243]
for (int i = 1; i < n; i++) {
ct[i] = Math.max(ct[i - 1], at[i]) + bt[i]; // [cite: 1247]
}
double avgTat = 0, avgWt = 0;
for (int i = 0; i < n; i++) {
tat[i] = ct[i] - at[i]; // [cite: 1254]
wt[i] = tat[i] - bt[i]; // [cite: 1256]
avgTat += tat[i];
avgWt += wt[i];
}
System.out.println("\nPID\tAT\tBT\tCT\tTAT\tWT");
for (int i = 0; i < n; i++) {
System.out.println(pid[i] + "\t" + at[i] + "\t" + bt[i] + "\t" + ct[i] + "\t" + tat[i] + "\t" + wt[i]);
}
System.out.println("Average TAT: " + (avgTat / n));
System.out.println("Average WT: " + (avgWt / n));
sc.close();
}
}
● Run: Run the FCFS.java file.
● Output: The console will ask you for input. Type the number of processes, then the AT
and BT for each 10. It will then print the results table 11.
3. Practical 6: SJF (Non-Preemptive)
● Create Class: Right-click cpu_scheduling, select New > Class.
● Name: SJF
● Click Finish.
● Copy & Paste Code:
Java
// Source: SPOS OP.pdf (Pages 9-11)
package cpu_scheduling;
import java.util.Scanner;
public class SJF {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
int pid[] = new int[n]; int at[] = new int[n]; int bt[] = new int[n];
int ct[] = new int[n]; int tat[] = new int[n]; int wt[] = new int[n];
boolean done[] = new boolean[n];
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
System.out.print("Enter Arrival Time for P" + pid[i] + ": ");
at[i] = sc.nextInt();
System.out.print("Enter Burst Time for P" + pid[i] + ": ");
bt[i] = sc.nextInt();
}
int time = 0, completed = 0;
while (completed < n) {
int idx = -1;
int minBT = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (!done[i] && at[i] <= time && bt[i] < minBT) { // [cite: 842]
minBT = bt[i];
idx = i;
}
}
if (idx == -1) {
time++; // [cite: 855]
} else {
time += bt[idx]; // [cite: 858]
ct[idx] = time; // [cite: 860]
tat[idx] = ct[idx] - at[idx]; // [cite: 862-863]
wt[idx] = tat[idx] - bt[idx]; // [cite: 865-866]
done[idx] = true; // [cite: 869]
completed++; // [cite: 870]
}
}
System.out.println("\nPID\tAT\tBT\tCT\tTAT\tWT");
double avgTat = 0, avgWt = 0;
for (int i = 0; i < n; i++) {
// Find original order if needed, but for output this is fine
for(int j=0; j<n; j++) {
if(pid[j] == (i+1)) {
System.out.println(pid[j] + "\t" + at[j] + "\t" + bt[j] + "\t" + ct[j] + "\t" + tat[j] + "\t" +
wt[j]);
avgTat += tat[j];
avgWt += wt[j];
}
}
}
System.out.println("Average TAT: " + (avgTat / n));
System.out.println("Average WT: " + (avgWt / n));
sc.close();
}
}
● Run: Run SJF.java.
● Output: The console will ask for input 12. Enter the AT and BT for each process. It will then
print the results table 13.
4. Practical 7: Priority (Non-Preemptive)
● Create Class: Right-click cpu_scheduling, select New > Class.
● Name: Priority
● Click Finish.
● Copy & Paste Code:
Java
// Source: SPOS OP.pdf (Pages 12-14)
package cpu_scheduling;
import java.util.Scanner;
public class Priority {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
int pid[] = new int[n]; int at[] = new int[n]; int bt[] = new int[n];
int pr[] = new int[n]; int ct[] = new int[n]; int tat[] = new int[n];
int wt[] = new int[n]; boolean done[] = new boolean[n];
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
System.out.print("Enter Arrival Time for P" + pid[i] + ": ");
at[i] = sc.nextInt();
System.out.print("Enter Burst Time for P" + pid[i] + ": ");
bt[i] = sc.nextInt();
System.out.print("Enter Priority for P" + pid[i] + " (lower = higher priority): ");
pr[i] = sc.nextInt(); // [cite: 1147-1149]
}
int time = 0, completed = 0;
while (completed < n) {
int idx = -1;
int minPr = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (!done[i] && at[i] <= time && pr[i] < minPr) { // [cite: 1058, 1162]
minPr = pr[i]; // [cite: 1059]
idx = i; // [cite: 1060]
}
}
if (idx == -1) {
time++; // [cite: 1062]
} else {
time += bt[idx]; // [cite: 1064]
ct[idx] = time; // [cite: 1066]
tat[idx] = ct[idx] - at[idx]; // [cite: 1068-1069]
wt[idx] = tat[idx] - bt[idx]; // [cite: 1071-1072]
done[idx] = true; // [cite: 1074]
completed++; // [cite: 1076]
}
}
System.out.println("\nPID\tAT\tBT\tPri\tCT\tTAT\tWT");
double avgTat = 0, avgWt = 0;
for (int i = 0; i < n; i++) {
for(int j=0; j<n; j++) {
if(pid[j] == (i+1)) {
System.out.println(pid[j] + "\t" + at[j] + "\t" + bt[j] + "\t" + pr[j] + "\t" + ct[j] + "\t" +
tat[j] + "\t" + wt[j]);
avgTat += tat[j];
avgWt += wt[j];
}
}
}
System.out.println("Average TAT: " + (avgTat / n));
System.out.println("Average WT: " + (avgWt / n));
sc.close();
}
}
● Run: Run Priority.java.
● Output: The console will ask for AT, BT, and Priority for each process 14. It will then print
the results table 15.
5. Practical 8: Round Robin
● Create Class: Right-click cpu_scheduling, select New > Class.
● Name: RoundRobin
● Click Finish.
● Copy & Paste Code:
Java
// Source: SPOS OP.pdf (Pages 1-3)
package cpu_scheduling;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class RoundRobin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
int pid[] = new int[n]; int at[] = new int[n]; int bt[] = new int[n];
int ct[] = new int[n]; int tat[] = new int[n]; int wt[] = new int[n];
int remBt[] = new int[n];
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
System.out.print("Enter Arrival Time for P" + pid[i] + ": ");
at[i] = sc.nextInt();
System.out.print("Enter Burst Time for P" + pid[i] + ": ");
bt[i] = sc.nextInt();
remBt[i] = bt[i];
}
System.out.print("Enter Time Quantum: ");
int tq = sc.nextInt(); // [cite: 539-540]
Queue<Integer> q = new LinkedList<>(); // [cite: 544]
boolean inQ[] = new boolean[n];
int time = 0;
int completed = 0;
// Find first process to arrive
int firstIdx = 0;
for(int i=1; i<n; i++) {
if(at[i] < at[firstIdx]) {
firstIdx = i;
}
}
q.add(firstIdx); // [cite: 547]
inQ[firstIdx] = true; // [cite: 549]
time = at[firstIdx]; // [cite: 551]
while (completed < n) {
if (q.isEmpty()) {
// Find the next process that arrives if CPU is idle
int nextIdx = -1;
int minAt = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (remBt[i] > 0 && at[i] < minAt && !inQ[i]) {
minAt = at[i];
nextIdx = i;
}
}
if (nextIdx != -1) {
q.add(nextIdx);
inQ[nextIdx] = true;
time = at[nextIdx];
} else {
break; // Should not happen if completed < n
}
}
int idx = q.poll();
if (remBt[idx] > tq) {
remBt[idx] -= tq;
time += tq; // [cite: 574]
} else {
time += remBt[idx]; // [cite: 578]
remBt[idx] = 0; // [cite: 579]
ct[idx] = time; // [cite: 581]
tat[idx] = ct[idx] - at[idx]; // [cite: 583-584]
wt[idx] = tat[idx] - bt[idx]; // [cite: 585-586]
completed++;
}
// Add processes that have arrived
for (int i = 0; i < n; i++) {
if (!inQ[i] && at[i] <= time && remBt[i] > 0) {
q.add(i); // [cite: 593]
inQ[i] = true; // [cite: 595]
}
}
// Re-add current process if not finished
if (remBt[idx] > 0) {
q.add(idx); // [cite: 610]
}
}
System.out.println("\nPID\tAT\tBT\tCT\tTAT\tWT");
double avgTat = 0, avgWt = 0;
for (int i = 0; i < n; i++) {
System.out.println(pid[i] + "\t" + at[i] + "\t" + bt[i] + "\t" + ct[i] + "\t" + tat[i] + "\t" + wt[i]);
avgTat += tat[i];
avgWt += wt[i];
}
System.out.println("Average TAT: " + (avgTat / n));
System.out.println("Average WT: " + (avgWt / n));
sc.close();
}
}
● Run: Run RoundRobin.java.
● Output: The console will ask for AT, BT, and Time Quantum 16. It will then print the results
table 17.
## 9️⃣, 🔟, 1️⃣1️⃣: Page Replacement (Prac 9-11)
These are three final, separate Java programs.
1. Create the Package
● Right-click the src folder, select New > Package.
● Name: page_replacement 18
● Click Finish.
2. Practical 9: FIFO
● Create Class: Right-click page_replacement, select New > Class.
● Name: FIFO 19191919
● Click Finish.
● Copy & Paste Code:
Java
// Source: LP 4 (1).pdf
package page_replacement; // [cite: 277]
import java.util.*;
public class FIFO { // [cite: 283]
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("FIFO Page Replacement Algorithm");
System.out.print("Enter number of frames: ");
int framesCount = sc.nextInt(); // [cite: 299]
System.out.print("Enter number of pages in reference string: ");
int n = sc.nextInt(); // [cite: 301, 294]
int[] pages = new int[n];
System.out.println("Enter page reference string:");
for (int i = 0; i < n; i++) {
pages[i] = sc.nextInt(); // [cite: 310]
}
fifo(framesCount, pages);
sc.close();
}
private static void fifo(int framesCount, int[] pages) {
Set<Integer> frames = new HashSet<>();
Queue<Integer> queue = new LinkedList<>(); // [cite: 342, 357]
int faults = 0;
System.out.println("\nSimulation:\n");
for (int i = 0; i < pages.length; i++) {
int page = pages[i];
System.out.printf("Step %2d Page %d: ", (i + 1), page);
if (!frames.contains(page)) { // Page Fault [cite: 347]
faults++; // [cite: 348]
System.out.print("MISS\tFrames: "); // [cite: 366]
if (frames.size() < framesCount) { // [cite: 349]
frames.add(page); // [cite: 350]
queue.add(page); // [cite: 351]
} else {
int toRemove = queue.poll(); // [cite: 353-354]
frames.remove(toRemove); // [cite: 355]
frames.add(page); // [cite: 356]
queue.add(page); // [cite: 359]
}
} else {
System.out.print("HIT \tFrames: "); // [cite: 367]
}
List<Integer> list = new ArrayList<>(queue);
while (list.size() < framesCount) list.add(null);
System.out.println(list); // [cite: 381-382]
}
System.out.println("\nTotal Page Faults: " + faults); // [cite: 383]
}
}
● Run: Run FIFO.java.
● Output: The console will ask for the number of frames and pages, then the page string
20
. It will print the step-by-step simulation 21and total faults22.
3. Practical 10: LRU
● Create Class: Right-click page_replacement, select New > Class.
● Name: LRU 23
● Click Finish.
● Copy & Paste Code:
Java
// Source: LP 4 (2).pdf
package page_replacement;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class LRU { //
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames, pointer = 0, hit = 0, fault = 0, ref_len;
boolean isFull = false;
int buffer[];
ArrayList<Integer> stack = new ArrayList<>(); // [cite: 1915]
int reference[];
int mem_layout[][];
System.out.println("Please enter the number of Frames: ");
frames = Integer.parseInt(br.readLine()); // [cite: 1919]
System.out.println("Please enter the length of the Reference string: ");
ref_len = Integer.parseInt(br.readLine()); // [cite: 1922]
reference = new int[ref_len];
mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for (int j = 0; j < frames; j++)
buffer[j] = -1; // [cite: 1933]
System.out.println("Please enter the reference string: ");
for (int i = 0; i < ref_len; i++) {
reference[i] = Integer.parseInt(br.readLine()); // [cite: 1942]
}
System.out.println();
for (int i = 0; i < ref_len; i++) {
if (stack.contains(reference[i])) { // [cite: 1952]
stack.remove(stack.indexOf(reference[i])); // [cite: 1956]
}
stack.add(reference[i]); // [cite: 1960]
int search = -1;
for (int j = 0; j < frames; j++) {
if (buffer[j] == reference[i]) { // [cite: 1968]
search = j;
hit++; // [cite: 1974]
break;
}
}
if (search == -1) { // Page Fault [cite: 1982]
if (isFull) { // [cite: 1987]
int min_loc = ref_len;
for (int j = 0; j < frames; j++) {
if (stack.contains(buffer[j])) { // [cite: 1998]
int temp = stack.indexOf(buffer[j]); // [cite: 2003]
if (temp < min_loc) { // [cite: 2005]
min_loc = temp; // [cite: 2009]
pointer = j; // [cite: 2011]
}
}
}
}
buffer[pointer] = reference[i]; // [cite: 2021]
fault++; // [cite: 2023]
if (!isFull) {
pointer++; // [cite: 2025]
if (pointer == frames) { // [cite: 2027]
pointer = 0; // [cite: 2031]
isFull = true; // [cite: 2033]
}
}
}
System.arraycopy(buffer, 0, mem_layout[i], 0, frames); // [cite: 2041]
}
for (int i = 0; i < frames; i++) {
for (int j = 0; j < ref_len; j++)
System.out.printf("%3d ", mem_layout[j][i]);
System.out.println();
}
System.out.println("The number of Hits: " + hit); // [cite: 2060]
System.out.println("Hit Ratio: " + (float) ((float) hit / ref_len)); // [cite: 2063]
System.out.println("The number of Faults: " + fault); // [cite: 2065]
}
}
● Run: Run LRU.java.
● Output: The console will ask for frames, length, and the reference string 24. It will print
the memory layout and total faults 25.
4. Practical 11: Optimal
● Create Class: Right-click page_replacement, select New > Class.
● Name: OptimalReplacement 26
● Click Finish.
● Copy & Paste Code:
Java
// Source: LP 4 (3).pdf
package page_replacement;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OptimalReplacement { //
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames, pointer = 0, hit = 0, fault = 0, ref_len;
boolean isFull = false;
int buffer[];
int reference[];
int mem_layout[][];
System.out.println("Please enter the number of Frames: ");
frames = Integer.parseInt(br.readLine()); // [cite: 1375]
System.out.println("Please enter the length of the Reference string: ");
ref_len = Integer.parseInt(br.readLine()); // [cite: 1383]
reference = new int[ref_len];
mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for (int j = 0; j < frames; j++)
buffer[j] = -1; // [cite: 1394]
System.out.println("Please enter the reference string: ");
for (int i = 0; i < ref_len; i++) {
reference[i] = Integer.parseInt(br.readLine()); // [cite: 1403]
}
System.out.println();
for (int i = 0; i < ref_len; i++) {
int search = -1;
for (int j = 0; j < frames; j++) {
if (buffer[j] == reference[i]) { // [cite: 1419]
search = j;
hit++; // [cite: 1425]
break;
}
}
if (search == -1) { // Page Fault [cite: 1433]
if (isFull) { // [cite: 1437]
int index[] = new int[frames];
boolean index_flag[] = new boolean[frames];
for (int j = i + 1; j < ref_len; j++) { // [cite: 1446]
for (int k = 0; k < frames; k++) {
if ((reference[j] == buffer[k]) && (!index_flag[k])) { // [cite: 1453]
index[k] = j; // [cite: 1457]
index_flag[k] = true; // [cite: 1459]
break;
}
}
}
int max = -1;
pointer = 0;
for (int j = 0; j < frames; j++) {
if (!index_flag[j]) { // Page not found in future
pointer = j;
max = ref_len + 1; // Mark as furthest
break;
}
}
if (max == -1) { // All pages found in future
max = index[0]; // [cite: 1469]
pointer = 0; // [cite: 1472]
for(int j=1; j < frames; j++) {
if(index[j] > max) { // [cite: 1486]
max = index[j]; // [cite: 1490]
pointer = j; // [cite: 1492]
}
}
}
}
buffer[pointer] = reference[i]; // [cite: 1500]
fault++; // [cite: 1502]
if (!isFull) { // [cite: 1504]
pointer++; // [cite: 1508]
if (pointer == frames) { // [cite: 1510]
pointer = 0; // [cite: 1515]
isFull = true; // [cite: 1517]
}
}
}
System.arraycopy(buffer, 0, mem_layout[i], 0, frames); // [cite: 1527]
}
for (int i = 0; i < frames; i++) {
for (int j = 0; j < ref_len; j++)
System.out.printf("%3d ", mem_layout[j][i]);
System.out.println();
}
System.out.println("The number of Hits: " + hit); // [cite: 1544]
System.out.println("Hit Ratio: " + (float) ((float) hit / ref_len)); // [cite: 1546]
System.out.println("The number of Faults: " + fault); // [cite: 1548]
}
}
● Run: Run OptimalReplacement.java.
● Output: The console will ask for frames, length, and the reference string 27. It will print
the memory layout and total faults 28.
## 1️⃣2️⃣ & 1️⃣3️⃣: Python / Raspberry Pi (Prac 12 & 13)
As you mentioned, "LED Blinking" and "Camera Module" are for Python on a Raspberry Pi, so
you cannot run them in your Java Eclipse environment. You will need to use a Python editor
(like Thonny, which is pre-installed on Raspberry Pi OS) on the Raspberry Pi itself.
Would you like me to provide the standard Python code for those last two practicals so you
can prepare for them?