attached it down below
Programming Assignment 2 © 2022 Dr. Robert Castañeda 1 IS 2063 Programming Lang II with Java Programming Assignment # 2 100 points Due date July 21, 2022 On your flash drive, laptop, or on your home PC create a folder for your PA. Name your folder in the following manner: lastNameFirstInitialOfYourFirstNameCourseSectionPA2. So, for example if I were creating this folder for my PA and given that my name is Robert Castaneda and let’s say that I was in section 001, my folder name would be CastanedaR001PA2 Next, do the following: Within DrJava, you are to create a Java class for your PA. Like for your folder (above), use the same procedure in naming your PA class. So again, for example, if I were creating the class for my PA and given that my name is Robert Castaneda and let’s say that I was in section 001, my class name for my PA would be CastanedaR001PA2 Now, your PA will show student’s summer schedule. Your program should enroll students into courses (no more than two) and, if needed, drop a course for a student. Student information will be read in from a file (Names.txt). 1) You will be entering input from the keyboard so you must import the Scanner class into your program. You will also be using a random number generator in your program, as well as throwing an exception in your code. So please import the following as shown below: import java.util.Scanner; import java.io.*; import java.util.Random; © 2022 Dr. Robert Castañeda 2 The following are variables and constants that you will need to declare in your program (within the main() method): fName, lName, and classification will be strings id, index, and drop will be integers numStudents will also be an integer an assigned the value 0 The following three will be constants of type int. They must be assigned their respective values as shown. NUM_STUDENTS with value 4 CHANCE_OF_DROPPING with value 3 DROP_A_COURSE with value 1 allStudents will be an array of Student of size NUM_STUDENTS The class Student is described later in this document. As mentioned, your program will need a random number generator. To set this up, please use the following declaration: Random rand = new Random(); You will be reading from a file that is called Names.txt. So please set up the file as shown in Chapter 4. You may want to specifically look at pages 237 through 244 as a guidance. 2) You will need to read the information from the file Names.txt and store them inside your array allStudents. You will need a while loop to do this. There are four items per record (a student). They are: the student’s first name, last name, id, and classification (freshman, sophomore, junior, senior). For each item, you will need to invoke a separate method that will read in the © 2022 Dr. Robert Castañeda 3 appropriate item (all will be located in the PA2 class). The outline of the four methods that you will need are shown in UML diagram below PA2 – main() +readFirstName(inputFile : Scanner) : static String +readLastName(inputFile : Scanner) : static String +readID(inputFile : Scanner) : static int +readClassification(inputFile : Scanner) : static String +goodBye() : static void To code the method headers, you will need to look at each item in the UML diagram. For example, the first method on the list is called readFirstName(). The + symbol means that this access modifier is public, next is the name of the method which is of course readFirstName() and it has a single parameter called inputFile that is of type Scanner. The keyword static is also shown and indicates that this method should be declared within the class PA2. Finally, the String is shown that indicates that this method will be returning a value of type String. Putting it all together you should have the header method as follows: public static String readFirstName(Scanner inputFile) In addition to what is shown in the above UML diagram, here are brief descriptions of each of these methods and what you need to know to code them: readFirstName() – has a local variable called firstName that is a String. Reads in from the file the first name of the student (use the next() method) and assigns it to the local variable. This local variable is then returned from this method readLastName() – has a local variable called lastName that is a String. Reads in from the file the last name of the student (use the next() method) and assigns it to the local variable. This local variable is then returned from this method © 2022 Dr. Robert Castañeda 4 readID() – has a local variable called id that is an int. Reads in from the file the id of the student (use the nextInt() method) and assigns it to the local variable. This local variable is then returned from this method readClassification() – has a local variable called classification that is a String. Reads in from the file the student’s classification (use the next() method) and assigns it to the local variable. This local variable is then returned from this method goodBye() – simply prints out that you have written this program and that it is the end of the program (i.e., the last two lines that we always print out of a program) As mentioned above, you will need a while loop to read in each item for a student. Once you have read in the items for a given student (and are still within the while loop), you will have to represent these items as a student within the array allStudents that you declared earlier. Recall that allStudents is an array of Student (class described below). So, the important thing to remember is to use the proper subscript for this array (i.e., numStudents) and to send to the constructor these four pieces of information for this student. Of course, you should update your subscript by adding 1 to it. All of this should occur within the while loop until there are no more student records to be read from the file. After you exit the while loop you should close the file. 3) At this point we will turn to the class Student but we will come back to the main() method later. So now, create a class called Student. The UML diagram for Student is shown below: Student -COURSE_NUM : final int /* assigned the number 6 */ -FIRST_OR_SECOND : final int /* assigned the number 2 */ © 2022 Dr. Robert Castañeda 5 -POSSIBLE_COURSES : final int /* assigned the number 2 */ -firstName, lastName, classification : String -id : int -courses : String[COURSE_NUM] - studentCourses : String[POSSIBLE_COURSES] -firstCourse, secondCourse : String +Student (fNameIn : String , lNameIn : String , idIn : int , classificationIn : String) +setFirstName(fNameIn : String) : void +setLastName(String lNameIn) : void +setID(int idIn) : void +setClassification(String classificationIn) : void +setCourses() : void +getFirstName() : String +getLastName() : String +getID() : int +getClassification() : String +enrollStudent() : void +printStudentCourses() : void +dropCourse() : void You will also have to import the following in your Student class. import java.util.Random; To code the instance fields and method headers, you will need to look at each item in the UML diagram. For example, the first one (COURSE_NUM) is a constant and the – symbol represents that its access modifier is private and that it is of type int. Further, the comment next to it says that it should be assigned the number 6 to it. So, it would look like the following: private final int COURSE_NUM = 6; There is also an array called courses in the UML diagram which is an array of String that is of size COURSE_NUM. Deducing from it, you should get the following: private String[] courses = new String[COURSE_NUM]; © 2022 Dr. Robert Castañeda 6 For the methods, you will notice that they all have the symbol + which means that they all have the accessor modifier public. So, looking at setFirstName, you will see that it has one parameter called fNameIn and that it is of data type String. Further, you will notice that it does not return anything (shown as void in the UML diagram). From this, you should code the method header as: public void setFirstName(String fNameIn) In addition to what is shown in the above UML diagram, here are brief descriptions of each of these methods (and the constructor) and what you need to know to code them: Student – for each incoming parameter to this constructor you should invoke the appropriate mutator/set method to give the appropriate instance field an initial value. It should also invoke the method called setCourses (described further below) setFirstName – will assign the incoming parameter to the appropriate instance field and return nothing (void) setLastName – will assign the incoming parameter to the appropriate instance field and return nothing (void) setID – will assign the incoming parameter to the appropriate instance field and return nothing (void) setClassification – will assign the incoming parameter to the appropriate instance field and return nothing (void) © 2022 Dr. Robert Castañeda 7 setCourses – will assign the 6 strings below to the array courses. You may assign them one by one to courses. Recall that courses is an array of size COURSE_NUM, thus the 6 strings are: “MAT 1093 Precalculus” “ENG 2413 Editing” “CHE 1073 Basic Chemistry” “PHI 2043 Introductory Logic” “BIO 1404 Biosciences I” “AST 1013 Introduction to Astronomy” getFirstName – will return the value/state of the appropriate instance field of type String getLastName – will return the value/state of the appropriate instance field of type String getID – will return the value/state of the appropriate instance field of type int getClassification – will return the value/state of the appropriate instance field of type String enrollStudent – will have two local variables called first and second.