Reference no: EM132157015
Use comments liberally to document exactly what you are doing in the program.
Use descriptive variable names in camel-case with the first letter in lowercase. If the instructions tell you to use a certain variable or method name, use that specific name.
Create private static int variables for maxNumberStudents and actualNumberStudents; these must be placed after the class statement and before the main method. Initialize both to zero.
Create a private static String [] variable for studentNames.
In the main method, ask the user for the maximum number of students and read it into maxNumberStudents, and instantiate the studentNames array by setting it equal to new String[maxNumberStudents].
You previously declared it as a private static String array, but it didn't equal anything at that point or even have an address in memory, in other words, it was null (in fact, you could have instantiated it in the same statement where you declared it, but at that time you had no idea of what the maximum number of students was). Now each of the array's elements is still null, but the array itself exists and has an address.
In a new method called enterStudentNames, use a for-loop with loop control variable actualNumberStudents (this was created as a static var earlier) to ask for all of the students' names by firstName and lastName into separate Strings. Tell the user to enter a period in firstName to signal that they are done entering students' names.
In a real application, you would have made an object called studentName and have the first and last names in separate fields, but you don't know how to do that yet, so you will build a full name called fullStudentName using concatenation with lastName, then comma, then blank, then firstName so that the entire name will be stored in a local String called fullStudentName.
Set the element at the position actualNumberStudents of studentNames to fullStudentName, that is:
studentNames[actualNumberStudents] = fullStudentName;
Test firstName to see if it is a period. Remember that you cannot use == with objects, and Strings are objects; see Notes below. If firstName is a period ".", then you are done accepting names (and don't enter a studentName of "."), and break out of the loop. When you get out of the loop, actualNumberStudents will reflect the actual number of students, which is why we called it that.
Call enterStudentNames from the main method.
Test this and get any bugs out before moving on.
Write a method called printStudentNames that will print out the array of students.
Call this method from your main method. Print one name per line at the beginning of each line. You should test that the element is not null before doing things with it.
You should NOT print blank lines (or anything else) for the empty/null elements at the end. If you hit a null element, you can break out of the loop because everything after that will be null also.
Call printStudentNames from the main method.
Test this and get any bugs out before moving on. Be sure to make a backup at this point.
In printStudentNames, also print the next counting number in front of each student's name along with a period and a space. Format this so that they all take up 2 spaces and the periods will all line up (also that will make the last names all line up). Test this and get any bugs out before moving on. Be sure to make a backup at this point.
Write another method called sortStudentNames that will sort the array by the student's names. Do NOT use any of the built-in sort methods; you MUST write your own.
I don't care which kind you write, but I would suggest that the bubble sort is easiest even though it's not very efficient. However, on short lists, efficiency is not usually a factor; even 1000 elements would be considered short for these purposes. Remember to test to see whether you've hit the end of actually used elements by testing whether the element == null. If so, you can treat it as if you hit the end of the array. Do NOT sort the empty elements; leave them at the end of the array. Call the sort method from your main method.
Then call the print method again. Test this and get all the bugs out. Contact me if you are having trouble. Be sure to make a backup at this point.
Test the program with the data that I use in the Sample Runs.
10% Extra Credit: In printStudentNames, make the first names line up by making the last names all take the same amount of space. Make the last names stay left-justified. You'll have to figure out how to split the names into first and last name again. This will involve using this statement:
int indexOfComma = studentNames[i].indexOf(',');
Then use the substr method with indexOfComma to split names[i] into the first and last names. You'll need to use the printf statement with the proper formatters. It might be a good idea to create another method to do this and call it from printStudentNames. That will help keep your code clean and readable.
Sample Runs: Enter this data exactly and make screen-prints to paste into a Word document that you will turn in to prove that your program works correctly. On the final first name, just hit the Enter key without entering anything:
Please enter the maximum number of students: 8
First name (enter period . to quit): Luke
Last name: Skywalker
First name (enter period . to quit): Darth
Last name: Vader
First name (enter period . to quit): Han
Last name: Solo
First name (enter period . to quit): R2D2
Last name: Droid
First name (enter period . to quit): 3CPO
Last name: Droid
First name (enter period . to quit): .
1. Skywalker , Luke
2. Vader , Darth
3. Solo , Han
4. Droid , R2D2
5. Droid , 3CPO
1. Droid , 3CPO
2. Droid , R2D2
3. Skywalker , Luke
4. Solo , Han
5. Vader , Darth