Robstersgaming

Untitled

Oct 28th, 2021
997
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Robby Sodhi
  2.  * J. Bains
  3.  * ICS3U0
  4.  * Oct. 28 2021
  5.  * A program to take in a student's class information and display a neatly organized time table
  6.  */
  7. import java.util.Scanner; //import scanner for user input
  8. import java.util.Vector; //java dynamic array to hold our courses and other information
  9.  
  10.  
  11. public class first_java_summative {
  12.   public static void main(String[] args)  {
  13.    System.out.println("What is your full name?");
  14.    String full_name = getUserInput(); //get the full name from user, using below defined getUserInput()
  15.    
  16.    System.out.println("What is the school year?(Eg. 2021-2022");
  17.    String school_year = getUserInput(); //get the school year from the user
  18.    
  19.    Vector<String[]> courseName_courseCode_courseGrade_semester = new Vector<String[]>(4); //creating a vector of arrays of strings. (Allows us to keep our information organized) 2D array (it initially has a size of 4 but can expand as we add elements)
  20.    
  21.    System.out.println("Enter all of your courses, course codes, and grades for a year");
  22.  
  23.    int count = 1; //count variable to keep track of how many courses the user is inputting
  24.  
  25.    while (true){ //loop allows the user to decide how many courses they have in each semester (useful when number of courses change)
  26.      System.out.println("Enter name of course #" + count + " enter EXIT to exit");
  27.      String course = getUserInput(); //get course name
  28.    
  29.      if (course.equals("EXIT")){ //if the input was EXIT exit the loop
  30.        break; //end the while loop
  31.      }
  32.      
  33.      System.out.println("Enter the course code");
  34.      String course_code = getUserInput(); //get course code
  35.      
  36.      System.out.println("Enter course grade");
  37.      String course_grade = getUserInput(); //get grade
  38.      
  39.      System.out.println("What semester is this? (Type \"1\", \"2\", \"3\", or \"4\")"); // \ escapes our quotes
  40.      String semester = getUserInput(); //get semester
  41.      
  42.      if (!(semester.equals("1") || semester.equals("2") || semester.equals("3") || semester.equals("4"))){ //check if input is valid (1,2,3,4)
  43.        System.out.println("error try again");
  44.        continue; //loop back to the start and make the user re-enter all of the course information
  45.      }
  46.      
  47.      String[] temp_arr = {course, course_code, course_grade, semester}; //This temporary array lets us organise our data in a format our previously defined vector can take
  48.      courseName_courseCode_courseGrade_semester.addElement(temp_arr); //put our temp_arr with all of the course data into the semester 1 vector
  49.      
  50.      count++; //increment the number of courses by one
  51.    }
  52.    for (int i = 1; i < 5; i++){ //loop over our 4 semesters starting with semester 1
  53.     System.out.println("\t\t\t\t\t" + full_name + " semester " + i + " " + school_year); //display the semester, school year, and name
  54.      System.out.println("\t\t\t\tCourse_name course_code course_grade");
  55.     //variables to hold the sum of our numbers and number of numbers (To calculate the average) (for just one semester it resets after each iteration)
  56.     double average_sum = 0.0;
  57.     double average_count = 0.0;
  58.    
  59.      for (String[] course_info : courseName_courseCode_courseGrade_semester){ //iterate through all of the string arrays within our data vector
  60.       //0 = courseName
  61.       //1 = courseCode
  62.       //2 = courseGrade
  63.       //3 = semester
  64.        
  65.       //String.valueOf converts an integer to a string, this allows us to compare the semester string to it (We could have also turned the semster into an int)
  66.       if (!(course_info[3].equals(String.valueOf(i)))){ //if the course is not in the semester we're looping over, skip it
  67.         continue;
  68.       }
  69.      
  70.       System.out.println("\t\t\t\t" + course_info[0] + " " + course_info[1] + " " + course_info[2]); //display course info
  71.      
  72.       //information used to calculate the average
  73.       average_sum += Double.valueOf(course_info[2]); //add course grade to the average_sum (Valueof converts our string grade into an double)
  74.       average_count++; //increase the number of numbers so we can later use it to calculate the average
  75.     }
  76.      
  77.     System.out.println("\t\tSemester average: " + (average_sum / average_count)); //calculate and print out the average
  78.    
  79.   }
  80.    
  81.    
  82.   }
  83.  
  84.  
  85.   static String getUserInput(){ //a method that takes a users input as a string and returns it to the caller
  86.     Scanner console = new Scanner(System.in); //Initialize scanner (console) allowing us to take user input
  87.     String input = console.nextLine();
  88.    
  89.     if (input.equals("")){//if string is empty (User typed nothing)
  90.      System.out.println("error try again");
  91.      input = getUserInput(); //recursivly keep asking for user input until valid
  92.     }
  93.    
  94.     console.close(); //close console to free memory
  95.    
  96.     return input;  //return the input back to the caller once it is confirmed valid
  97.   }
  98. }
RAW Paste Data