Question: can you explain this java code and how it works and why was hash set and hash maps used: package javaapplication 3 0 ; import

can you explain this java code and how it works and why was hash set and hash maps used: package javaapplication30; import java.util.*; /**** @author */ public class JavaApplication30{/*** @param args the command line arguments */ public static void main(String[] args){// TODO code application logic here Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of frames: "); int capacity = scanner.nextInt(); System.out.print("Enter the page string (comma-separated): "); scanner.nextLine(); // Consume newline String[] pages = scanner.nextLine().split(","); int pageFaultsLRU = simulateLRU(pages, capacity); System.out.println("Number of page faults: "+ pageFaultsLRU); int pageFaultsFifo = simulateFIFO(pages, capacity); System.out.println("Number of page faults: "+ pageFaultsFifo); } public static int simulateLRU(String[] pages, int capacity){ Set frameSet = new LinkedHashSet<>(capacity); Map pageLastUsed = new HashMap<>(); int pageFaults =0; for (String page : pages){ if (!frameSet.contains(page)){ pageFaults++; if (frameSet.size()== capacity){ String leastRecentlyUsedPage = null; int minLastUsed = Integer.MAX_VALUE; for (String framePage : frameSet){ if (pageLastUsed.get(framePage)< minLastUsed){ leastRecentlyUsedPage = framePage; minLastUsed = pageLastUsed.get(framePage); }} frameSet.remove(leastRecentlyUsedPage); pageLastUsed.remove(leastRecentlyUsedPage); } frameSet.add(page); } pageLastUsed.put(page, pageFaults); System.out.println("Memory State: "+ frameSet); } return pageFaults; } public static int simulateFIFO(String[] pages, int capacity){ Queue frameQueue = new LinkedList<>(); Set frameSet = new HashSet<>(); int pageFaults =0; for (String page : pages){ if (!frameSet.contains(page)){ pageFaults++; if (frameQueue.size()== capacity){ String removedPage = frameQueue.poll(); frameSet.remove(removedPage); } frameQueue.offer(page); frameSet.add(page); } System.out.println("Memory State: "+ frameQueue); } return pageFaults; }}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!