Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java For this assignment we will create a program that implements an insertion s

ID: 3685848 • Letter: J

Question

java

For this assignment we will create a program that implements an insertion sort. You are required to write two methods, and you can write additional helper methods if needed. Use class name "Main".

In the main method:

Ask the user to input names, and as the names are input they will be added in alphabetical order to an ArrayList.

The names should be added in Title case, that is the first letter capitalized, and all other letters lower case. So if they enter "bob" it should be added as "Bob".

The user should enter the word "STOP" in any combination of lowercase and uppercase letters to stop entering names.

After all the names are entered, print the contents of the sorted ArrayList using ArrayList.toString().

In the titleCase(String s) method:

Return the parameter s as a String in Title case

If the string passed in is "rapunzel" it becomes "Rapunzel", "GRETEL" becomes "Gretel", and "little red riding hood" becomes "Little red riding hood".

Important notes:

This assignment can be completed without importing any classes besides ArrayList and Scanner. In addition, do not use any of the methods provided by the class java.util.Collections.

The word "Stop" should not be entered into the ArrayList. You can assume at least one name will be entered before the "STOP" in the program we run to grade your assignment. You may also assume that any name entered has a length of 2 or greater.

Before submitting your program, run your code and input the names listed in the Sample Run that follows. Verify that your program prints the same output as the Sample Run. Be sure to test that your code works with other names, and with alternate cases for “stop”. We will grade your program with other inputs to verify that it meets all the assignment requirements.

Sample Run:

Enter the next name:
zeb
Enter the next name:
rita
Enter the next name:
SUE
Enter the next name:
adele
Enter the next name:
BarBara
Enter the next name:
StoP
[Adele, Barbara, Rita, Sue, Zeb]

Explanation / Answer

Main.java
import java.util.Scanner;
import java.util.ArrayList;
public class Main {

   static ArrayList<String> names = new ArrayList<String>();

   public static void main(String[] args){
       Scanner s = new Scanner(System.in);
       String in = "";
       while(!in.toUpperCase().equals("STOP")){
           System.out.println("Enter the next name:");
           in = s.nextLine();
           if(!in.toUpperCase().equals("STOP")){
               names.add(titleCase(in));
           }
       }
       System.out.print(sort(names).toString());
   }

   public static String titleCase(String s){
       return s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase();
   }


   public static ArrayList<String> sort(ArrayList<String> in){
       for (int pointer = 1; pointer<in.size(); pointer ++){
           String currentString = in.get(pointer);

           for(int i= 1; i<=pointer; i++){

               if((currentString.compareTo(in.get(i-1)) < 0 && currentString.compareTo(in.get(i)) <= 0 && in.get(i-1).compareTo(in.get(i))<=0)) {
                   in.remove(pointer);
                   in.add(i-1, currentString);
                   break;
               }else if (currentString.compareTo(in.get(0))<=0 ){
                   in.remove(pointer);
                   in.add(0, currentString);
                   break;
               }

           }
       }

       return in;
   }

}

sample output
Enter the next name:                                                                                                                                        
zeb                                                                                                                                                         
Enter the next name:                                                                                                                                        
rita                                                                                                                                                        
Enter the next name:                                                                                                                                        
adele                                                                                                                                                       
Enter the next name:                                                                                                                                        
STOP                                                                                                                                                        
[Adele, Rita, Zeb]sh-4.3$