
Fix the Java code below, make sure it can make the same outputs as the sample:
import java.util.ArrayList;
import java.util.Scanner;
class Main
{
//Returns index of name if found otherwise -1
public static int findName(ArrayList<String> arr, String name)
{
for(int i = 0; i < arr.size(); i++)
if(arr.get(i).equals(name))
return i;
return -1;//Name not found
}
//Deletes a name from ArrayList
public static void deleteName(ArrayList<String> arr, String name)
{
int index = findName(arr, name);//Get index of name to be deleted
for(int i = index; i < arr.size() - 1; i++){
arr.set(i, arr.get(i + 1));
}
arr.remove(arr.size() - 1);//Remove last element
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of students(upto 10): ");
int n = scan.nextInt();
ArrayList<String> arr = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
System.out.print("Enter name of student " + (i+1) + ": ");
arr.add(scan.next());
}
System.out.print("Enter name of student to delete: ");
String name = scan.next();
while(findName(arr, name) == -1)//Keep asking user for a valid name to delete
{
System.out.print("Name not found. Try a new name: ");
name = scan.next();
}
deleteName(arr, name);//Delete name from array
System.out.println("Revised list of students: " + arr);//Print revised list
}
}
Sample outputs in the picture:


Step by stepSolved in 2 steps with 1 images

- Question 16 What is output? import java.util.ArrayList; public class SimpleCar { @Override public String toString(){ return "I drive fast"; } public static void main(String[] args) { ArrayList myStuff; myStuff = new ArrayList(); myStuff.add(new String("Greetings")); myStuff.add(new Object()); myStuff.add(new SimpleCar()); for(Object item : myStuff) { System.out.println(item.toString()); } String Object SimpleCar java.lang.Object@19cc java.lang.Object@23fb java.lang.Object@ab79 java.lang.String@169b java.lang.Object@23fb java.lang.SimpleCar@a42b Greetings java.lang.Object@169b I drive fast }arrow_forwardIT security is a serious and growing problem because A. more and more potentially sensitive data about people are being stored and transmitted B. IT systems often have vulnerabilities that can be exploited to steal information or cause harm C. careless or disaffected employees can compromise the security of a firm's systems and data D. all of the above E. none of the abovearrow_forwardPlease use the template provided below and make sure the output matches exactly. import java.util.Scanner;import java.util.ArrayList; public class PhotoLineups { // TODO: Write method to create and output all permutations of the list of names. public static void printAllPermutations(ArrayList<String> permList, ArrayList<String> nameList) { } public static void main(String[] args) { Scanner scnr = new Scanner(System.in); ArrayList<String> nameList = new ArrayList<String>(); ArrayList<String> permList = new ArrayList<String>(); String name; // TODO: Read in a list of names; stop when -1 is read. Then call recursive method. }}arrow_forward
- Code debug JAVA import java.util.ArrayList;public class ArrayList {public static ArrayList<Integer> reverse(ArrayList<Integer> list) {for (int i = 0; i < list.size(); i++) {System.out.println(list);}public static ArrayList<Integer> getReverse(ArrayList<Integer> list){for (int index = 0; index < list.size() / 2; index++) {int temp = list.get(index);list.set(index, list.get(list.size() - index - 1));//swaplist.set(list.size() - index - 1, temp); //swap}return list;}}public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);System.out.println("Original elements : " + list);System.out.println("Reversed elements : " + getReverse(list));}}arrow_forwardNeed help with menu loop pleasearrow_forwardWrite in Java What is this program's exact outputarrow_forward
- import java.util.ArrayList;import java.util.Scanner;public class CIS231A4JLeh {public static void main(String[] args) {Scanner scnr = new Scanner(System.in);ArrayList<Integer> integers = new ArrayList<>();String input = scnr.nextLine();while (input.isEmpty()) {String[] parts = input.split("\\s+");for (String part : parts) {try {integers.add(Integer.parseInt(part));} catch (NumberFormatException ignored) {}}input = scnr.nextLine();}System.out.println("Jakob");System.out.println("Number of integers input: " + integers.size());System.out.println("All values input, in ascending order:");printIntegers(integers);System.out.println("Lowest value input: " + integers.get(0));System.out.println("Highest value input: " + integers.get(integers.size() - 1));System.out.printf("Average of all values input: %.2f%n", calculateAverage(integers));System.out.println("Mode of the data set and its frequency: " + calculateMode(integers));}private static void printIntegers(ArrayList<Integer>…arrow_forwardExercise 2: Consider the following class: public class Sequence { private ArrayList values; public Sequence() { values = new ArrayList(); } public void add(int n) { values.add(n); } public String toString() { return values.toString(); } } Add a method public Sequence merge(Sequence other) that merges two sequences, alternating ele- ments from both sequences. If one sequence is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer sequence. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then a.merge(b) returns the sequence 1 9 4 7 9 4 16 9 11 without modifying a or b.arrow_forwardimport java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int n = scnr.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scnr.nextInt(); } for (int i = n - 1; i >= 0; i--) { System.out.print(arr[i]); if (i > 0) { System.out.print(","); } } }}arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





