Question
Java ***PLEASE FOLLOW INSTRUCTIONS EXACTLY*** For this assignment we will create a program that implements an insertion sort. You are required to write two methods,
Java
***PLEASE FOLLOW INSTRUCTIONS EXACTLY***
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.
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 "END" 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 "END" should not be entered into the ArrayList. You can assume at least one name will be entered before the "END" 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 end. 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: eND [Adele, Barbara, Rita, Sue, Zeb]
***AGAIN PLEASE FOLLOW INSTRUCTIONS EXACTLY***
This question was answered earlier with this response:
import java.util.ArrayList;
import java.util.Scanner;
public class TitleCase {
public static String titleCase(String title){
String lower = title.toLowerCase();
lower = Character.toUpperCase(lower.charAt(0))+lower.substring(1);
return lower;
}
public static void insertionSortList(ArrayList
if(! aryList.contains(element)){
// edge case: Size one list, number coming in is smaller.
if(aryList.size() == 0 || aryList.get(0).compareTo(element) > 0) {
aryList.add(0, element);
} else if(aryList.get(aryList.size()-1).compareTo(element) < 0){
aryList.add(element);
}
else {
//System.out.println(element);
for(int i = 0; i < aryList.size()-1; i++) {
if(aryList.get(i).compareTo(element) < 0 && element.compareTo(aryList.get(i+1)) < 0) {
aryList.add(i+1, element);
break;
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList
while(true){
System.out.print("Enter the next name: ");
String name = sc.next();
if(name.equalsIgnoreCase("end"))
break;
insertionSortList(aryList, titleCase(name));
}
System.out.println(aryList);
}
}
/*
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: prVeah
Enter the next name: BarBara
Enter the next name: eNd
[Adele, Barbara, Prveah, Rita, Sue, Zeb]
*/
When this response was submitted, I got these errors:
Your code has been evaluated against a set of test data. You had 2 out of 5 tests pass correctly. Your score is 40%. The tests that failed were: Test 1: Calling your main method with 11 names in title case Incorrect: Sorted Output Test 2: Calling your main method with 11 names in mixed case form Incorrect: Sorted Output Test 4: Calling your main method with names having 2 or more words Incorrect: Sorted Output
Please help me!!! Thank you!
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