Question: Open up ArrayAddDrop.java from the last activity. We are going to add onto this program. First we are going to offer the user the option
-
Open up ArrayAddDrop.java from the last activity. We are going to add onto this program. - First we are going to offer the user the option to either add or drop a student.
- Declare two new variables at the top of main
int indexToAdd; String choice;
- Next, add the following lines of code to your program beneath the first call to printArray in main:
System.out.print(" Would you like to add or drop a student?" + " Type 'ADD' or 'DROP': "); choice = input.next(); if(choice.equalsIgnoreCase("DROP")) { //add the code here to drop a student } else if (choice.equalsIgnoreCase("ADD")) { input.nextLine(); //clear ' ' from input System.out.print(" Enter the name of the student to add: "); String newName = input.nextLine(); System.out.print(" Enter the position on the roster: "); indexToAdd = input.nextInt(); //Call insert method here //increment numStudents variable } else { System.out.println(" Invalid input. Please re-run program " + "to try again."); } //code for print out of current roster should go here- The contents of your main method should now look identical to the following:
- Next, cut and paste the code for dropping a student (except the part where you print the updated roster) and place it inside the if statement's curly braces.
- Your if statement should now look like this:
if(choice.equalsIgnoreCase("DROP")) { System.out.print(" Enter the number of the student to drop: "); indexToDrop = input.nextInt(); remove(roster, numStudents, indexToDrop); numStudents--; }- Then, copy and paste the below method and add it to your program:
/** * Inserts an 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 boolean insert(String array[], int numElements, int indexToInsert, String newValue) { if (array.length == numElements) { System.out.println("Array is full. No room to insert."); return false; } //start at end and work backwards shifting names down for (int i = numElements; i > indexToInsert; i--) { array[i] = //you fill in here } array[indexToInsert] = newValue; return true; }- It is your job to finish writing the insert method by filling in the missing line.
- You will also need to add the two missing lines to the else if statement in main
- Hint: Create a boolean variable and assign the result of the method call to this variable
- Only if the method returns true, should you increment the numStudents variable - do you understand why?
- Note that the insert method should insert into an array whose length is larger than its current size.
- Therefore, the method begins by performing some error checking.
- Next, the method needs to shift down all of the items in the array starting from the index of the new value that you wish to insert all the way down to the last element in the array.
- In other words, the method needs to create a hole inside the array to insert into
- It does so by shifting elements down to create space to insert a new element at the desired index.
- When you are finished writing your method, run your program and make sure the new element was inserted into the correct location.
- Once everything is running properly as shown in the sample output, upload your AddDropArray.java file to Canvas.
Sample Output:
Current Roster: 0. Ali 1. Bao 2. Chloe 3. Danika 4. Eduardo Would you like to add or drop a student? Type 'ADD' or 'DROP': ADD Enter the name of the student to add: Chris Enter the position on the roster: 3 Current Roster: 0. Ali 1. Bao 2. Chloe 3. Chris 4. Danika 5. Eduardo Total enrolled in the class: 6 students
MIC CUTTLETICS Ur your than THICOU STIUUIU TOW TUUN TUCricicar cu HC TUTTOVITIS. Scanner input = new Scanner(System.in); int indexToAdd, indexToDrop; String choice; int numStudents = 5; //current number on roster String roster[] = new String[10]; //guesstimate of length will need roster [0] = "Ali"; roster[1] = "Bao"; roster [2] = "Chloe"; roster [3] = "Danika"; roster[4] = "Eduardo"; System.out.println("Current Roster:"); printArray(roster, numStudents); System.out.print(" Would you like to add or drop a student?" + " Type 'ADD' or 'DROP': "); choice = input.next(); if(choice.equalsIgnoreCase("DROP")) { //add code here to drop a student } else if (choice.equalsIgnoreCase("ADD")) { input.nextLine(); //clear ' ' from input System.out.print(" Enter the name of the student to add: "); String newName = input.nextLine(); System.out.print(" Enter the position on the roster: "); //Call insert method here //increment numStudents variable } else { System.out.println(" Invalid input. Please re-run program " + "to try again."); } System.out.println(" Current Roster:"); printArray(roster, numStudents); System.out.println(" Total enrolled in the class: " + numStudents + " students"); Most cutandarta torcode for dronninatudant lavent thanartwbare vorint thoudated rectorlandbacitincid
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
