You are on page 1of 4

Program:

Bully.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Bully {


static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter No. of Processes:- ");
int NoofPro = sc.nextInt();

ArrayList<Integer> ReadyPro = new ArrayList<>();


ArrayList<Integer> CrashedPro = new ArrayList<>();
for (int i = 1; i <= NoofPro; i++) {
ReadyPro.add(i);
}
System.out.println(ReadyPro);
while (true) {
System.out.println("Choose the below");
System.out.println("1. Current Co-ordinator");
System.out.println("2. Crash Process");
System.out.println("3. Revoke Process");
System.out.println("4. Exit");
System.out.println("Enter the Number:");
int value = sc.nextInt();

switch (value) {
case 1:
NewCo(ReadyPro);
break;
case 2:
CrashPro(ReadyPro, CrashedPro);
break;
case 3:
RevokePro(ReadyPro, CrashedPro);
break;
case 4:
sc.close();
System.exit(0);
break;
default:
System.out.println("Invalid input. Please enter a valid option.");
break;
}
}
}
public static void NewCo(ArrayList<Integer> ReadyPro) {
System.out.println("The New Co-ordinator is " + Collections.max(ReadyPro));
}
public static void CrashPro(ArrayList<Integer> ReadyPro, ArrayList<Integer> CrashedPro) {
System.out.println("Ready Processes :- " + ReadyPro);
System.out.print("Enter No. of Process to Crash:- ");
int NoCrashPro = sc.nextInt();
System.out.println(NoCrashPro);
ReadyPro.remove(Integer.valueOf(NoCrashPro));
CrashedPro.add(NoCrashPro);
NewCo(ReadyPro);
}
public static void RevokePro(ArrayList<Integer> ReadyPro, ArrayList<Integer> CrashedPro) {
System.out.println("Crashed Processes :- " + CrashedPro);
System.out.print("Enter No. of Process to Revoke :- ");
int NoRevokePro = sc.nextInt();
CrashedPro.remove(Integer.valueOf(NoRevokePro));
ReadyPro.add(NoRevokePro);
NewCo(ReadyPro);
}
}

Output:

E:\7th SEM\DC\PRACTICAL\DC-P6(EA)>javac Bully.java

E:\7th SEM\DC\PRACTICAL\DC-P6(EA)>java Bully


Enter No. of Processes:- 4
[1, 2, 3, 4]
Choose the below
1. Current Co-ordinator
2. Crash Process
3. Revoke Process
4. Exit
Enter the Number:
1
The New Co-ordinator is 4
Choose the below
1. Current Co-ordinator
2. Crash Process
3. Revoke Process
4. Exit
Enter the Number:
2
Ready Processes :- [1, 2, 3, 4]
Enter No. of Process to Crash:- 2
2
The New Co-ordinator is 4
Choose the below
1. Current Co-ordinator
2. Crash Process
3. Revoke Process
4. Exit
Enter the Number:
3
Crashed Processes :- [2]
Enter No. of Process to Revoke :- 2
The New Co-ordinator is 4
Choose the below
1. Current Co-ordinator
2. Crash Process
3. Revoke Process
4. Exit
Enter the Number:
4

You might also like