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

1) Write a Java program called “NameCollector” that contains an object invocatio

ID: 3640299 • Letter: 1

Question

1) Write a Java program called “NameCollector” that contains an object invocation for a class called “Collector”.

2) Write a separate Java “Collector” class (not contained within the“NameCollector” program code) that contains the following 2 methods:


a. Names – used to store student names in an array (i.e. void method). For the purposes of this exercise, assume no more than 10 names will be stored in the array.

b. SortRoutine – accepts an array of names and sorts them in alphabetical order (i.e. void method) and displays the sorted order of names to the screen.


3) Within your program code, invoke the object method to enable the input of student names and after all student names are entered in the array, invoke the objerct method to sort the array of students.

4) Link your Java program and class together in your IDE, compile your program and run.

Explanation / Answer

public class Collector {
    private String[] names = new String[10];

    void names() {
        java.util.Scanner sc = new java.util.Scanner(System.in);

        System.out.println("Enter the 10 names you like to sort.");
        for (int i = 0; i < 10; i++) {
            System.out.print(" names[" + i + "] : ");
            names[i] = sc.nextLine();
        }
    }

    void SortRoutine() {
        java.util.Arrays.sort(names);
        System.out.println(" After sorting.....");
        for (int i = 0; i < names.length; i++) {
            System.out.print(names[i] + " ");
        }
        System.out.println();
    }
}

public class NamesCollector {

    public static void main(String[] args) {
        Collector collect = new Collector();
        collect.names();
        collect.SortRoutine();
    }
}