Han

Han Ferik

Home | Projects | AP CSA

CEMC 2022 Junior / ConstraintViolations

Tools: VSCode | Source Code: GitHub

This program is a Java application that checks whether certain grouping constraints for students are violated. It first takes input specifying which students must be together ("together constraints") and which students must not be together ("not together constraints"). Then, it processes the student groups and determines how many violations occur, finally printing the total number of violations.

The program begins by using a Scanner object to read input from the user. It first reads the number of "together constraints" and stores them in a two-dimensional array, where each constraint consists of a pair of students who must be in the same group. Similarly, it reads the "not together constraints" and stores them in another two-dimensional array, where each constraint consists of a pair of students who should not be in the same group.

Next, the program reads the student groups. Each group consists of three students, and these groups are stored in a two-dimensional array. At this point, the program is ready to check for violations based on the given constraints.

The first check is for "together constraints." For each pair of students in this category, the program verifies if they are in the same group. If a pair is not found together in any group, it is considered a violation, and the violations counter is incremented.

The second check is for "not together constraints." Here, the program ensures that no pair of students from this category appears in the same group. If a pair is found together in any group, it is counted as a violation, and the violations counter is incremented.

Finally, the program prints the total number of violations. At the end of the program, there is a helper method called isInGroup, which checks if a given student is part of a specific group. This method returns true if the student is in the group and false otherwise. It is used to simplify the constraint-checking logic and make the code more readable.


ConstraintViolations Java Code

ConstraintViolations Java Code


  import java.util.Scanner;
  
  public class ConstraintViolations {
      public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          
          int numTogetherConstraints = scanner.nextInt();
          String[][] togetherConstraints = new String[numTogetherConstraints][2];
          for (int i = 0; i < numTogetherConstraints; i++) {
              togetherConstraints[i][0] = scanner.next();
              togetherConstraints[i][1] = scanner.next();
          }
          
          int numNotTogetherConstraints = scanner.nextInt();
          String[][] notTogetherConstraints = new String[numNotTogetherConstraints][2];
          for (int i = 0; i < numNotTogetherConstraints; i++) {
              notTogetherConstraints[i][0] = scanner.next();
              notTogetherConstraints[i][1] = scanner.next();
          }
          
          int numGroups = scanner.nextInt();
          String[][] groups = new String[numGroups][3];
          for (int i = 0; i < numGroups; i++) {
              for (int j = 0; j < 3; j++) {
                  groups[i][j] = scanner.next();
              }
          }
          
          int violations = 0;
          
          for (int i = 0; i < numTogetherConstraints; i++) {
              String student1 = togetherConstraints[i][0];
              String student2 = togetherConstraints[i][1];
              boolean found = false;
              
              for (int j = 0; j < numGroups; j++) {
                  if (isInGroup(groups[j], student1) && isInGroup(groups[j], student2)) {
                      found = true;
                      break;
                  }
              }
              
              if (!found) {
                  violations++;
              }
          }
          
          for (int i = 0; i < numNotTogetherConstraints; i++) {
              String student1 = notTogetherConstraints[i][0];
              String student2 = notTogetherConstraints[i][1];
              boolean found = false;
              
              for (int j = 0; j < numGroups; j++) {
                  if (isInGroup(groups[j], student1) && isInGroup(groups[j], student2)) {
                      found = true;
                      break;
                  }
              }
              
              if (found) {
                  violations++;
              }
          }
          
          System.out.println(violations);
      }
      
      public static boolean isInGroup(String[] group, String student) {
          for (int i = 0; i < group.length; i++) {
              if (group[i].equals(student)) {
                  return true;
              }
          }
          return false;
      }
  }

Find me on the interwebs!

Github LinkedIn Instagram Facebook