Question: I have to do an assignment for my computer sci class using java, and I have my code finished, but when I submit it to
I have to do an assignment for my computer sci class using java, and I have my code finished, but when I submit it to the code runner, it says that I have the following errors:
The tests that failed were: 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 Test 5: Calling your main method with names having numbers and letters Incorrect: Sorted Output
Here is the original assignment description:
"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]"
Here is my Code:
import java.util.ArrayList;import java.util.List;import java.util.Scanner;class Main {static ListlistOfNames = new ArrayList (); public static void main(String args[]) {Scanner scanner = new Scanner(System.in);String name;while (true) {System.out.println("Enter the next name:");name = scanner.next();if (name.toLowerCase().equals("end")) {break;} else {insertionSort(titleCase(name));}}System.out.println(listOfNames.toString());}/*** It converts the given sting in titleCase.* @param name* @return*/public static String titleCase(String name) {StringBuilder s1 = new StringBuilder(name);s1.replace(0, s1.length(), s1.toString().toLowerCase());s1.setCharAt(0, Character.toTitleCase(s1.charAt(0)));return s1.toString();}/*** Insertion sort for String arraylist.* @param name*/public static void insertionSort(String name) {for (int i = 0; i < listOfNames.size(); ++i) {String str = listOfNames.get(i);if (name.compareTo(str) <= 0) {listOfNames.add(i, name);return; // done}}// not found a greater one in the arrayListlistOfNames.add(name); // so append to the end}}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
