Assignment 2
due Thursday, January 17 at 11:59pm on Canvas

Assignment 2: My Portal Roster (10 pts)
  • For this assignment, we are going to work with adding and removing data from arrays, linear search, and File I/O.
  • This program will act as a class roster program for an instructor
  • This program will read a file containing a list of names and another file with a list of scores. 
  • The names are names on the roster, and the scores are scores students achieved on Exam 1
  • The names and scores need to be stored in two separate but parallel arrays.
  • In other words, the indices of the names array will match the indices of the scores array.
  • Thus the name at index 0, will match the score at index 0, etc
  • Open up two new text files inside of Eclipse:
  • Name these text files names.txt and scores.txt
  • Copy and paste the following list of names into your names.txt file:
Jiming Wu
James Brown
Leanna Perez
Xing Li
Stacey Cahill
Mohammed Abbas
Kumari Chakrabarti
Shakil Smith
Jung Ahrin
Pedro Martinez
Ally Gu
Tamara White
Alvin Ngo
Abir Fadel
Brad Feinman
Xiaohang Yue

  • Copy and paste the following list of scores into your scores.txt file:

100
99
98
98
97
96
95
94
94
93
91
88
85
83
75
64

  • Read in the contents of each file and store them in a separate arrays, one array called names and one array called scores.
  • You should declare arrays of length 30, and separately store information about the current number of values stored inside of the two arrays.
  • Hint: you can use a variable to count how many values are in each file as you are reading in the data, and use this information to save the number of values currently stored in each array.
  • Next, you will need to write a menu driven program to allow the user to complete 3 tasks:
    • Add Students
    • Drop Students
    • Write the roster to a file when the user has finished adding and dropping.
  • For the add menu option, the program should do the following:
    • Prompt the user for a name and score to insert, and a position in the arrays at which to insert.
    • Call the insert method twice to insert the name and score into the respective arrays at the indicated position (index).
  • For the drop menu option, the program should do the following:
    • Prompt the user for the name of a student to drop.
    • Next, call linearSearch to determine at which index in the array the student is placed
    • Confirm that it has located the correct name in the array by verifying the user wishes to drop the found name
    • Finally, call the remove method to remove the name from the names array and the score from the scores array
  • For the exit menu option, the program should do the following:
    • Prompt the user to enter the name of a file in which to print the roster
    • call the printRoster method passing in the name of the indicated file
  • To begin, copy and paste the starter code into a file called Roster.java
  • Make sure that you implement and call all of the methods whose signatures are specified below
  • When your program is working correctly, upload it to Canvas.


Starter Code
/**
* @author
* CIS 36B
*/
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class Roster {
   
    public static void main(String[] args) throws IOException {
     
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to MyPortal Roster!");

        System.out.println("\nGoodbye!");    
    }

    /**
     * Inserts an integer element into an array at a specified index
     * @param array the list of integer values
     * @param numElements the current number of elements stored
     * @indexToInsert the location in the array to insert the new element
     * @param newValue the new String value to insert in the array
     */
  
    public static void insert(int array[], int numElements,
            int indexToInsert, int newValue) {
     
    }

    /**
     * Inserts a String element into an array at a specified index
     * @param array the list of String values
     * @param numElements the current number of elements stored
     * @indexToInsert the location in the array to insert the new element
     * @param newValue the new String value to insert in the array
     */
  
    public static void insert(String array[], int numElements,
            int indexToInsert, String newValue) {
     
    }
   
    /**
     * Removes a String element from an array at a specified index
     * @param array the list of String values
     * @param numElements the current number of elements stored
     * @param indexToRemove where in the array to remove the element
     */
   
    public static void remove(String array[], int numElements, int indexToRemove) {
       
    }
   
    /**
     * Removes an integer element from an array at a specified index
     * @param array the list of int values
     * @param numElements the current number of elements stored
     * @param indexToRemove where in the array to remove the element
     */
   
    public static void remove(int array[], int numElements, int indexToRemove) {
       
    }

    /**
     * Print an array of Strings either to the console
     * or a file
     * @param names the list of student names
     * @param scores the list of student scores
     * @param numElements the current number of elements stored
     * @param file the file name or "console" to write to the console
     */
    public static void printRoster(String[] names, int scores[],
            int numElements, String file) throws IOException {
       
    }
   
   /**
    * Searches for a specified String in a list
    * @param names the array of Strings
    * @param value the String to serach for
    * @param numNames the number of elements in the list
    * @return the index where value is located in the array
    */
   public static int linearSearch(String names[], String value, int numNames) {
    
       return -1;
   }

}


Sample Output

Welcome to MyPortal Roster!

Below is your current roster:

Exam 1: Name:

Score: 100 Jiming Wu
Score: 99 James Brown
Score: 98 Leanna Perez
Score: 98 Xing Li
Score: 97 Stacey Cahill
Score: 96 Mohammed Abbas
Score: 95 Kumari Chakrabarti
Score: 94 Shakil Smith
Score: 94 Jung Ahrin
Score: 93 Pedro Martinez
Score: 91 Ally Gu
Score: 88 Tamara White
Score: 85 Alvin Ngo
Score: 83 Abir Fadel
Score: 75 Brad Feinman
Score: 64 Xiaohang Yue


Please enter a menu option (A, D, X) below:
A. Add a Student
D. Drop a Student
X. Exit

Please enter your choice: A

Enter the name of the student to add: Orion Lavoine

Enter the position on the roster: 10

Enter Orion Lavoine's exam score: 93

Below is your current roster:

Exam 1: Name:

Score: 100 Jiming Wu
Score: 99 James Brown
Score: 98 Leanna Perez
Score: 98 Xing Li
Score: 97 Stacey Cahill
Score: 96 Mohammed Abbas
Score: 95 Kumari Chakrabarti
Score: 94 Shakil Smith
Score: 94 Jung Ahrin
Score: 93 Pedro Martinez
Score: 93 Orion Lavoine
Score: 91 Ally Gu
Score: 88 Tamara White
Score: 85 Alvin Ngo
Score: 83 Abir Fadel
Score: 75 Brad Feinman
Score: 64 Xiaohang Yue


Please enter a menu option (A, D, X) below:
A. Add a Student
D. Drop a Student
X. Exit

Please enter your choice: D

Please enter the name of the student to drop: Brad Feinman

Dropping ...Brad Feinman...
Is this correct (y/n): y

Brad Feinman has been dropped.

Below is your current roster:

Exam 1: Name:

Score: 100 Jiming Wu
Score: 99 James Brown
Score: 98 Leanna Perez
Score: 98 Xing Li
Score: 97 Stacey Cahill
Score: 96 Mohammed Abbas
Score: 95 Kumari Chakrabarti
Score: 94 Shakil Smith
Score: 94 Jung Ahrin
Score: 93 Pedro Martinez
Score: 93 Orion Lavoine
Score: 91 Ally Gu
Score: 88 Tamara White
Score: 85 Alvin Ngo
Score: 83 Abir Fadel
Score: 64 Xiaohang Yue


Please enter a menu option (A, D, X) below:
A. Add a Student
D. Drop a Student
X. Exit

Please enter your choice: A

Enter the name of the student to add: Natalya Akulova

Enter the position on the roster: 3

Enter Natalya Akulova's exam score: 98

Below is your current roster:

Exam 1: Name:

Score: 100 Jiming Wu
Score: 99 James Brown
Score: 98 Leanna Perez
Score: 98 Natalya Akulova
Score: 98 Xing Li
Score: 97 Stacey Cahill
Score: 96 Mohammed Abbas
Score: 95 Kumari Chakrabarti
Score: 94 Shakil Smith
Score: 94 Jung Ahrin
Score: 93 Pedro Martinez
Score: 93 Orion Lavoine
Score: 91 Ally Gu
Score: 88 Tamara White
Score: 85 Alvin Ngo
Score: 83 Abir Fadel
Score: 64 Xiaohang Yue


Please enter a menu option (A, D, X) below:
A. Add a Student
D. Drop a Student
X. Exit

Please enter your choice: X
Enter the name of a file to write your roster: roster.txt

Goodbye!

Contents of corresponding output file (roster.txt in my example):

Exam 1: Name:

Score: 100 Jiming Wu
Score: 99 James Brown
Score: 98 Leanna Perez
Score: 98 Natalya Akulova
Score: 98 Xing Li
Score: 97 Stacey Cahill
Score: 96 Mohammed Abbas
Score: 95 Kumari Chakrabarti
Score: 94 Shakil Smith
Score: 94 Jung Ahrin
Score: 93 Pedro Martinez
Score: 93 Orion Lavoine
Score: 91 Ally Gu
Score: 88 Tamara White
Score: 85 Alvin Ngo
Score: 83 Abir Fadel
Score: 64 Xiaohang Yue