Have can I simplify this Java program?

QuestionsCategory: Computer ScienceHave can I simplify this Java program?
William asked 1 year ago

Have can I simplify this Java program?
The Code:
package codejudge42;
import java.util.ArrayList;
import java.util.Scanner;
public class computernetwork {
public static void main(String[] args) {
   Scanner scan = new Scanner(System.in);
  
   int m = scan.nextInt();
   int n = scan.nextInt();
  
   Union pcNet = new Union(m);
   for(int counter = 0; counter < n; counter++){
   String newline = scan.nextLine();
   if (newline.equals(“”)){newline = scan.nextLine();}
   char c = newline.charAt(0);
   if (c == ‘A’){
       Scanner intScan = new Scanner(newline.substring(1));
       int i = intScan.nextInt();
       int j = intScan.nextInt();
       pcNet.unionLink(i, j);
   }
   else {
       Scanner intScan = new Scanner(newline.substring(1));
       int i = intScan.nextInt();
       int j = intScan.nextInt();
       if((pcNet.isConnected(i, j))== true){
       System.out.println(“YES”);
       }
       else{
       System.out.println(“NO”);
       }
   }
   }
}
  
}
class Union{
ArrayList<Integer> unionList;
ArrayList<Integer> rootList;
Union(int n) {
   unionList = new ArrayList<>();
   rootList = new ArrayList<>();
   for (int i = 0; i < n; i++){
   unionList.add(i);
   rootList.add(i, 1);   
   }
}
  
  
public int find(int i){
  
   int rootI = i;
   while(rootI != unionList.get(rootI)){
   rootI = unionList.get(rootI);
   }
  
   while(i != rootI){
   int nextRoot = unionList.get(i);
   unionList.set(i, rootI);
   i=nextRoot;
   }
  
   return rootI;
}
  
public void unionLink(int i, int j){
   int i1 = find(i);
   int j1 = find(j);
   if (i1 == j1){
       return;
   }
   if (rootList.get(i1) < rootList.get(j1)){
       rootList.set(j1, rootList.get(j1) + rootList.get(i1));
       unionList.set(i1, j1);
   }
   else{
       rootList.set(i1, rootList.get(j1) + rootList.get(i1));
       unionList.set(j1, i1);
   }   
}
  
public boolean isConnected(int i, int j){
   int iID = find(i);
   int jID = find(j);
   return iID == jID;
}
}
I need it to run the following tests
FIRST TEST
INPUT (stdin)
4 5
A 0 1
C 0 3
C 0 1
A 1 3
C 0 3
Expected Output
NO
YES
YES
SECOND TEST
INPUT (stdin)
10 20
A 5 1
C 7 5
C 7 2
C 5 3
A 2 5
C 8 5
A 1 0
A 8 5
C 7 1
A 6 5
C 0 3
Expected Output
NO
NO
NO
NO
NO
NO
YES
NO

1 Answers
Techtuna Staff answered 1 year ago
Explanation::
  • Code in JAVA is given below
  • First simplification is to separate the two class and create two java files
  • Union.java and computernetwork.java files are created inside the package named codejudge42

Code in JAVA::
Union.java Code::
package codejudge42;
import java.util.ArrayList;
class Union{
ArrayList<Integer> unionList;
ArrayList<Integer> rootList;
Union(int n) {
unionList = new ArrayList<>();
rootList = new ArrayList<>();
for (int i = 0; i < n; i++){
unionList.add(i);
rootList.add(i, 1);
}
}
public int find(int i){
int rootI = i;
while(rootI != unionList.get(rootI)){
rootI = unionList.get(rootI);
}
while(i != rootI){
int nextRoot = unionList.get(i);
unionList.set(i, rootI);
i=nextRoot;
}
return rootI;
}
public void unionLink(int i, int j){
int i1 = find(i);
int j1 = find(j);
if (i1 == j1){
return;
}
if (rootList.get(i1) < rootList.get(j1)){
rootList.set(j1, rootList.get(j1) + rootList.get(i1));
unionList.set(i1, j1);
}
else{
rootList.set(i1, rootList.get(j1) + rootList.get(i1));
unionList.set(j1, i1);
}
}
public boolean isConnected(int i, int j){
int iID = find(i);
int jID = find(j);
return iID == jID;
}
}
computernetwork.java Code::
package codejudge42;
import java.util.ArrayList;
import java.util.Scanner;
public class computernetwork {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
int n = scan.nextInt();
Union pcNet = new Union(m);
for(int counter = 0; counter < n; counter++){
String newline = scan.nextLine();
if (newline.equals(“”)){newline = scan.nextLine();}
Scanner intScan = new Scanner(newline.substring(1));
int i,j;
char c = newline.charAt(0);
if (c == ‘A’){
i = intScan.nextInt();
j = intScan.nextInt();
pcNet.unionLink(i, j);
}
else {
i = intScan.nextInt();
j = intScan.nextInt();
if((pcNet.isConnected(i, j))== true){
System.out.println(“YES”);
}
else{
System.out.println(“NO”);
}
}
}
}
}
OUTPUT::
4 5
A 0 1
C 0 3
NO
C 0 1
YES
A 1 3
C 0 3
YES