Advertisement
Ocha_DSH

Untitled

Jun 18th, 2019
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.73 KB | None | 0 0
  1. // Import the Scanner class
  2. import java.util.Scanner;
  3.  
  4. // Import the packages from the java.util library
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7.  
  8. public class Task_3 {
  9.  
  10.     public static void main(String[] args) {
  11.        
  12.         // Declare a boolean variable named 'inputStudent' with a value 'true'
  13.         boolean inputStudent = true;
  14.        
  15.         // Place while loop's condition to check whether user inputs less than 20 students
  16.         while (inputStudent) {
  17.        
  18.         // Prompt user to enter number of students between 1 to 20
  19.         System.out.print("Enter number of students between 1 to 20: ");
  20.        
  21.         // Create the first Scanner object
  22.         Scanner firstscan = new Scanner(System.in);
  23.         int numOfStudents = firstscan.nextInt();
  24.  
  25.         // Display result
  26.         if (numOfStudents > 20) {
  27.             System.out.println("Please enter not more than 20 students.");
  28.             System.out.println();
  29.            
  30.         } else {
  31.            
  32.             // Declare an integer variable named 'avg' and 'choice' with zero value
  33.             int avg = 0;
  34.             int choice = 0;
  35.            
  36.             // Declare a string variable named 'grade' with a value 'null'
  37.             String grade = null;
  38.  
  39.             // Create arraylist of 'students' and 'average'
  40.             ArrayList<String[]> students = new ArrayList<String[]>();
  41.             ArrayList<Integer> average = new ArrayList<Integer>();
  42.            
  43.             // Create an array of 'names' and 'averageMark'
  44.             String[] names = new String[numOfStudents];
  45.             int[] averageMark = new int[numOfStudents];
  46.            
  47.             // Create a two-dimensional array with a given number of rows and 6 columns
  48.             String[][] table = new String[numOfStudents][6];
  49.            
  50.             // Declare a boolean variable named 'menuLoop' with a value 'true'
  51.             boolean menuLoop = true;
  52.  
  53.             // Insert a new line
  54.             System.out.println();
  55.  
  56.             // Display result
  57.             for (int n = 0; n < numOfStudents; n++) {
  58.                
  59.                 // Prompt user to enter student name and 3 marks
  60.                 System.out.println("Enter the name and 3 marks for Student " + (n + 1) + ": ");
  61.  
  62.                 // Create the second Scanner object
  63.                 Scanner secondscan = new Scanner(System.in);
  64.                 String studentName = secondscan.nextLine();
  65.                
  66.                 // Capitalize the first letter of the names given
  67.                 studentName = studentName.toUpperCase().charAt(0) + studentName.substring(1, studentName.length());
  68.  
  69.                 String mark1 = secondscan.nextLine();
  70.                 String mark2 = secondscan.nextLine();
  71.                 String mark3 = secondscan.nextLine();
  72.                
  73.                 // Add elements to the array
  74.                 String[] array = {studentName, mark1, mark2, mark3};
  75.                 students.add(array);
  76.             }
  77.            
  78.             // Display result
  79.             System.out.println();
  80.             System.out.println("Student" + " \tAverage" + " \tGrade");
  81.             System.out.println("-------------------------------------");
  82.  
  83.             for (int n = 0; n < numOfStudents; n++) {
  84.                
  85.                 // Calculate the average mark
  86.                 avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  87.  
  88.                 // Calculate the grade based on average mark
  89.                 if (avg >= 90 && avg <= 100) {
  90.                     grade = "A";
  91.                 } else if (avg >= 80 && avg <= 89) {
  92.                     grade = "B";
  93.                 } else if (avg >= 70 && avg <= 79) {
  94.                     grade = "C";
  95.                 } else if (avg >= 60 && avg <= 69) {
  96.                     grade = "D";
  97.                 } else if (avg >= 0 && avg <= 59) {
  98.                     grade = "E";
  99.                 }
  100.                
  101.                 // Display result
  102.                 System.out.println(students.get(n)[0] + " \t" + " \t" + avg + " \t" + " \t" + grade);
  103.             }
  104.            
  105.             //  Place while loop's condition to display menu
  106.             while (menuLoop) {
  107.                 System.out.println();
  108.                 System.out.println("====================================================================================");
  109.                 System.out.println("1: Print the entire list");
  110.                 System.out.println("2: Sort and print the list alphabetically");
  111.                 System.out.println("3: Sort and print the list in descending order based on the average.");
  112.                 System.out.println("4: Ask the user to enter an average and search for the student who has that average");
  113.                 System.out.println("5: Find the student who has the minimum average");
  114.                 System.out.println("6: Print the grade distribution");
  115.                 System.out.println("0: Exit");
  116.                 System.out.println("====================================================================================");
  117.                 System.out.println();
  118.                 System.out.print("Enter your choice? ");
  119.  
  120.                 // Create the third Scanner object
  121.                 Scanner thirdscan = new Scanner(System.in);
  122.                 choice = thirdscan.nextInt();
  123.                
  124.                 // Place switch statement
  125.                 switch (choice) {
  126.                
  127.                 // Place case statement
  128.                 case 1:
  129.                     System.out.println();
  130.                     System.out.println("Student" + " \t" + "Mark 1" + " \t" + " \t" + "Mark 2" + " \t" + " \t" + "Mark 3" + " \t" + " \t" + "Average" + " \t" + "Grade");
  131.                     System.out.println("----------------------------------------------------------------------------------------------");
  132.  
  133.                     for (int n = 0; n < numOfStudents; n++) {
  134.                         avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  135.  
  136.                         if (avg >= 90 && avg <= 100) {
  137.                             grade = "A";
  138.                         } else if (avg >= 80 && avg <= 89) {
  139.                             grade = "B";
  140.                         } else if (avg >= 70 && avg <= 79) {
  141.                             grade = "C";
  142.                         } else if (avg >= 60 && avg <= 69) {
  143.                             grade = "D";
  144.                         } else if (avg >= 0 && avg <= 59) {
  145.                             grade = "E";
  146.                         }
  147.                        
  148.                         // Display result
  149.                         System.out.println(students.get(n)[0] + "\t" + "\t" + students.get(n)[1] + "\t" + "\t" + students.get(n)[2] + "\t" + "\t" + students.get(n)[3] + "\t" + "\t" + avg + "\t" + "\t" + grade);
  150.                     }
  151.                     break;
  152.  
  153.                 case 2:
  154.                     System.out.println();
  155.                     System.out.println("Student" + " \t" + "Mark 1" + " \t" + " \t" + "Mark 2" + " \t" + " \t" + "Mark 3" + " \t" + " \t" + "Average" + " \t" + "Grade");
  156.                     System.out.println("----------------------------------------------------------------------------------------------");
  157.                    
  158.                     for (int n = 0; n < numOfStudents; n++) {
  159.                         names[n] = students.get(n)[0];
  160.                     }
  161.                    
  162.                     // Implement Selection Sort
  163.                     for (int j = 0; j < names.length-1; j++) {
  164.                         int min = j;
  165.                        
  166.                         for (int k = j+1; k < names.length; k++)
  167.                         if (names[k].compareTo(names[min]) < 0 ) min = k;  
  168.  
  169.                       String temp = names[j];
  170.                       names[j] = names[min];
  171.                       names[min] = temp;
  172.                      
  173.                     }
  174.                     for (String value : names) {
  175.                         for (int n = 0; n < numOfStudents; n++) {
  176.                             avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  177.  
  178.                             if (avg >= 90 && avg <= 100) {
  179.                                 grade = "A";
  180.                             } else if (avg >= 80 && avg <= 89) {
  181.                                 grade = "B";
  182.                             } else if (avg >= 70 && avg <= 79) {
  183.                                 grade = "C";
  184.                             } else if (avg >= 60 && avg <= 69) {
  185.                                 grade = "D";
  186.                             } else if (avg >= 0 && avg <= 59) {
  187.                                 grade = "E";
  188.                             }
  189.                            
  190.                             // Display result
  191.                             if (students.get(n)[0] == value) {
  192.                                 System.out.println(students.get(n)[0] + "\t" + "\t" + students.get(n)[1] + "\t" + "\t" + students.get(n)[2] + "\t" + "\t" + students.get(n)[3] + "\t" + "\t" + avg + "\t" + "\t" + grade);
  193.                             }
  194.                         }
  195.                     }
  196.                     break;
  197.  
  198.                 case 3:
  199.                     System.out.println();
  200.                     System.out.println("Student" + " \t" + "Mark 1" + " \t" + " \t" + "Mark 2" + " \t" + " \t" + "Mark 3" + " \t" + " \t" + "Average" + " \t" + "Grade");
  201.                     System.out.println("----------------------------------------------------------------------------------------------");
  202.                    
  203.                     for (int n = 0; n < numOfStudents; n++) {
  204.                         avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  205.                        
  206.                         if (avg >= 90 && avg <= 100) {
  207.                             grade = "A";
  208.                         } else if (avg >= 80 && avg <= 89) {
  209.                             grade = "B";
  210.                         } else if (avg >= 70 && avg <= 79) {
  211.                             grade = "C";
  212.                         } else if (avg >= 60 && avg <= 69) {
  213.                             grade = "D";
  214.                         } else if (avg >= 0 && avg <= 59) {
  215.                             grade = "E";
  216.                         }
  217.                        
  218.                         // Fill in the rows and columns of the two-dimensional array with elements
  219.                         table[n][0] = students.get(n)[0];
  220.                         table[n][1] = students.get(n)[1];
  221.                         table[n][2] = students.get(n)[2];
  222.                         table[n][3] = students.get(n)[3];
  223.                         table[n][4] = Integer.toString(avg);
  224.                         table[n][5] = grade;
  225.                     }
  226.  
  227.                     for (int n = 0; n < numOfStudents; n++) {
  228.                         avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  229.                        
  230.                         // Adding object in average arraylist  
  231.                         average.add(avg);
  232.                     }
  233.                    
  234.                     // Implement Insertion Sort
  235.                     InsertionSort is = new InsertionSort(average);
  236.                     is.sortGivenArray();
  237.                     for (int i : InsertionSort.getInputArray()) {
  238.  
  239.                         for (int n = 0; n < numOfStudents; n++) {
  240.                             if (Integer.parseInt(table[n][4]) == i) {
  241.                                 System.out.println(table[n][0] + "\t" + "\t" + table[n][1] + "\t" + "\t" + table[n][2] + "\t" + "\t" + table[n][3] + "\t" + "\t" + table[n][4] + "\t" + "\t" + table[n][5]);
  242.                             }
  243.                         }
  244.                     }
  245.                     break;
  246.  
  247.                 case 4:
  248.                     for (int n = 0; n < numOfStudents; n++) {
  249.                         avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  250.                         averageMark[n] = avg;
  251.                     }
  252.                    
  253.                     // Prompt user to enter an average
  254.                     System.out.println();
  255.                     System.out.print("Enter an average: ");
  256.                    
  257.                     // Create the fourth Scanner object
  258.                     Scanner fourthscanner = new Scanner(System.in);
  259.                     int key = fourthscanner.nextInt();
  260.                    
  261.                     // Perform a Binary Search on the given average
  262.                     int result = Arrays.binarySearch(averageMark, key);
  263.                    
  264.                     // Display result
  265.                     if (result < 0) {
  266.                         System.out.println();
  267.                         System.out.println("There is no student with that average.");
  268.                     } else {
  269.                         System.out.println();
  270.                         System.out.println("Student" + " \t" + "Average");
  271.                         System.out.println("-----------------------");
  272.                    
  273.                     for (int n = 0; n < numOfStudents; n++) {
  274.                         avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  275.                    
  276.                         table[n][0] = students.get(n)[0];
  277.                         table[n][1] = Integer.toString(avg);
  278.                        
  279.                         if (Integer.parseInt(table[n][1]) == key) {
  280.                             System.out.println(table[n][0] + "\t" + "\t" + table[n][1]);
  281.                         }
  282.                     }
  283.                     }
  284.                     break;
  285.  
  286.                 case 5:
  287.                     for (int n = 0; n < numOfStudents; n++) {
  288.                         avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  289.                         averageMark[n] = avg;
  290.                         table[n][0] = students.get(n)[0];
  291.                         table[n][1] = Integer.toString(avg);
  292.                     }
  293.                    
  294.                     // Perform a Sequential Search to find the minimum average
  295.                     int minvalue = averageMark[0];
  296.                     for (int i = 0; i < averageMark.length; i++) {
  297.                         if (averageMark[i] < minvalue) {
  298.                             minvalue = averageMark[i];
  299.                         }
  300.                     }
  301.                    
  302.                     // Display result
  303.                     for (int n = 0; n < numOfStudents; n++) {
  304.                         if (Integer.parseInt(table[n][1]) == minvalue) {
  305.                             System.out.println();
  306.                             System.out.println("Student with the mininum average is: ");
  307.                             System.out.println();
  308.                             System.out.println("Student" + " \t" + "Average");
  309.                             System.out.println("-----------------------");
  310.                             System.out.println(table[n][0] + "\t" + "\t" + minvalue);
  311.                         }
  312.                     }
  313.                     break;
  314.  
  315.                 case 6:
  316.                    
  317.                     // Declare integer variables with zero value
  318.                     int a = 0, b = 0, c = 0, d = 0, e = 0;
  319.  
  320.                     for (int n = 0; n < numOfStudents; n++) {
  321.                         avg = (Integer.parseInt(students.get(n)[1]) + Integer.parseInt(students.get(n)[2]) + Integer.parseInt(students.get(n)[3])) / 3;
  322.                        
  323.                         if (avg >= 90 && avg <= 100) {
  324.                             grade = "A";
  325.                             a++;
  326.                         } else if (avg >= 80 && avg <= 89) {
  327.                             grade = "B";
  328.                             b++;
  329.                         } else if (avg >= 70 && avg <= 79) {
  330.                             grade = "C";
  331.                             c++;
  332.                         } else if (avg >= 60 && avg <= 69) {
  333.                             grade = "D";
  334.                             d++;
  335.                         } else if (avg >= 0 && avg <= 59) {
  336.                             grade = "E";
  337.                             e++;
  338.                         }
  339.                     }
  340.                    
  341.                     // Display result
  342.                     System.out.println();
  343.                     System.out.println("Grade distribution: ");
  344.                     System.out.println("A = " + a + "\t B = " + b + " \t C = " + c + "\t D = " + d + " \t E = " + e);
  345.                     break;
  346.  
  347.                 case 0:
  348.                     System.out.println();
  349.                     System.out.println("This is the end of the program. Thank you.");
  350.                    
  351.                     // Terminate the program
  352.                     System.exit(0);
  353.                     break;
  354.                
  355.                 // Set a default statement for when user inputs invalid menu choice
  356.                 default:
  357.                     System.out.println();
  358.                     System.out.println("This is not a valid menu choice. Please select another!");
  359.                     break;
  360.  
  361.                 }
  362.  
  363.                 }
  364.  
  365.             }
  366.  
  367.         }
  368.     }
  369.  
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement