Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Copy and paste the below starter code into a file called ArrayAddDrop.java Complete the lines of code where noted in bold. When your program is
- Copy and paste the below starter code into a file called ArrayAddDrop.java
- Complete the lines of code where noted in bold.
- When your program is working as shown in the sample output, submit ArrayAddDrop.java to Canvas.
/** * @author * CIS 36B */ import java.util.Scanner; public class ArrayAddDrop { public static void main(String[] args) { Scanner input = new Scanner(System.in); int indexToDrop; 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(" Enter the number of the student to drop: "); indexToDrop = input.nextInt(); remove(roster, numStudents, indexToDrop); //decrement numStudents variable System.out.println(" Current Roster:"); printArray(roster, numStudents); System.out.println("Total enrolled in the class: " + numStudents + " students"); } /** * Removes an 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) { for (int i = indexToRemove; i < numElements - 1; i++) { array[i] = //you fill in here! } return; } /** * Print an array of Strings to the console * @param array the list of String values * @param numElements the current number of elements stored */ public static void printArray(String[] array, int numElements) { for (int i = 0; i < numElements; i++) { System.out.println(i + ". " + array[i]); } } }
Sample Output:
Current Roster: 0. Ali 1. Bao 2. Chloe 3. Danika 4. Eduardo Enter the number of the student to drop: 2 Current Roster: 0. Ali 1. Bao 2. Danika 3. Eduardo Total enrolled in the class: 4 students
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started