Question

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.text.ParseException;

import java.util.*;

public class SJF {

public static void readFromFile() throws IOException {

BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt"));

ArrayList<String> listOfLines = new ArrayList<>();

String line = bufReader.readLine();

while (line != null) {

listOfLines.add(line);

line = bufReader.readLine();

}

bufReader.close();

System.out.println("Content of ArrayLiList:");

// split by new line

int num = 0;

for (String line1 : listOfLines) {

String line2[] = line1.split(",");

// int burstTime = Integer.parseInt(line2[3].trim());

// String retrival = listOfLines.get(0);

System.out.println(" <process ID>, <arrival time>, <priority>, <CPU burst>");

System.out.println(line1);

System.out.println("At time <" + Integer.parseInt(line2[1].trim()) + "> ms, CPU starts running process < " + Integer.parseInt(line2[3].trim()) + "ID>,");

//System.out.println(burstTime);

}

// int num =0;

// for (int i = 0; i < listOfLines.size(); i++) {

// String retrival = listOfLines.get(num), line2[] = retrival.split(",");

// int num1 = Integer.parseInt(line2[3].trim());

// String retrival1 = listOfLines.get(num), line3[] = retrival1.split(",");

// int num2 = Integer.parseInt(line3[3].trim());

// if (num1 < num2) {

// //System.out.println("You got it :)!!!!!!");

// Collections.swap(listOfLines, num + 1, num + 2);

// }

//

// System.out.println(listOfLines.get(i));

// }

// System.out.println(retrival);

// System.out.println(retrival1);

}

void sort(int arr[]) {

int n = arr.length;

// One by one move boundary of unsorted subarray

for (int i = 0; i < n - 1; i++) {

// Find the minimum element in unsorted array

int min_idx = i;

for (int j = i + 1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

// Swap the found minimum element with the first

// element

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

}

}

// Driver code

public static void main(String[] args) throws ParseException, IOException {

// process id's

readFromFile();

}

}

Can someone sort the output from that Arraylist in ascending order according to the <CPU burst>

OUTPUT

Content of ArrayLiList:

<process ID>, <arrival time>, <priority>, <CPU burst>

P3, 5, 8, 20

At time <5> ms, CPU starts running process < 20ID>,

<process ID>, <arrival time>, <priority>, <CPU burst>

P4, 9, 3, 50

At time <9> ms, CPU starts running process < 50ID>,

<process ID>, <arrival time>, <priority>, <CPU burst>

P1, 9, 7, 35

At time <9> ms, CPU starts running process < 35ID>,

<process ID>, <arrival time>, <priority>, <CPU burst>

P5, 13, 9, 20

At time <13> ms, CPU starts running process < 20ID>,

<process ID>, <arrival time>, <priority>, <CPU burst>

P6, 23, 4, 50

At time <23> ms, CPU starts running process < 50ID>,

<process ID>, <arrival time>, <priority>, <CPU burst>

P7, 25, 1, 60

At time <25> ms, CPU starts running process < 60ID>,

Homework Answers

Answer #1

hey,try this...

the code sorts an arrayList based on the CPU burst time

the sorting algorithm is same used by you

after the code is executed you get the arrayList "listOfLines" sorted in ascending order

as well as an integer array "arr" having sorted burst times.

Again copy in any ide to test and get better grasp of the code..

Hope this helps.Good luck!


import java.io.*;

import java.text.ParseException;
import java.util.*;

class example {
   public static void main (String[] args) {
  
   ArrayList<String> listOfLines = new ArrayList<>();
   listOfLines.add("P1, 5, 8, 40");
   listOfLines.add("P1, 5, 8, 70");
   listOfLines.add("P1, 5, 8, 20");
   listOfLines.add("P1, 5, 8, 30");
   listOfLines.add("P1, 5, 8, 90");
   listOfLines.add("P1, 5, 8, 55");
  
  
   int len=listOfLines.size();
   int arr[]=new int[len];
   for(int i=0;i<len;i++){
   String line1=listOfLines.get(i);
   String line2[] = line1.split(",");
arr[i] = Integer.parseInt(line2[3].trim());
   }
  


//Sorting the arrayList using the integer values in array

  
   for(int i=0;i<len;i++){
   int min_index=i;
   for(int j=i+1;j<len;j++){
   if(arr[min_index]>arr[j]){
   min_index=j;
   }
  
   }
   if(min_index!=i){
   int temp=arr[i];
   arr[i]=arr[min_index];
   arr[min_index]=temp;
  
  

//swapping the elements of the arrayList


   String temp1=listOfLines.get(i);
   listOfLines.set(i,listOfLines.get(min_index));
   listOfLines.set(min_index,temp1);
   }

   }
  
   for (int i=0;i<len;i++ ){
   System.out.println(listOfLines.get(i)+" "+arr[i]);
   }
  
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT