Question: I need help adding to the following program , the instructions are below it. Sorry for the long question, it is due by 11:55 PM

I need help adding to the following program , the instructions are below it. Sorry for the long question, it is due by 11:55 PM Central ST.

Here is the code that needs to be worked.

/* Assignment: Program 7: Create array of students and sort it Author: Course : Programming I Created : Description : This program uses an array with static methods to organize student names. This program also uses a method to give the user an option to exit the program.*/ import java.util.Scanner; public class Program5A { //declared the three required variables private static int maxNumberStudents = 0; private static int actualNumberStudents = 0; private static String[] studentNames; //main method public static void main(String[] args) { //get the number of students System.out.println("Please enter the maximum number of students: "); Scanner sc = new Scanner(System.in); maxNumberStudents = sc.nextInt(); //initialize the student array studentNames = new String[maxNumberStudents]; //call the required methods enterStudentNames(); printStudentNames(); sortStudentNames(); printStudentNames(); } //get the students name public static void enterStudentNames(){ Scanner sc = new Scanner(System.in); for(actualNumberStudents = 0;actualNumberStudents < maxNumberStudents;actualNumberStudents++){ System.out.println("First name (enter period . to quit):"); String firsName = sc.next(); if(firsName.equals(".")) break; System.out.println("Last name: "); String lastName=sc.next(); //concatinate the name String fullStudentName=lastName+", "+firsName; //saving the name to student array studentNames[actualNumberStudents] = fullStudentName; } sc.close(); } //printing the students name public static void printStudentNames(){ for(int i = 0;i0){ //swap in array String temp=studentNames[i]; studentNames[i] = studentNames[j]; studentNames[j] = temp; } } } } } 

And here are the instructions.

Program 9A: Gradebook Program Using a Class with Public Attributes

Remember to include your Student.java source code file in your zipped file to turn in (unless you choose to make the Student class an internal class). Copy your code from Program 7 and put it into 9A as your starting point.

(If you were unable to get this to work, request a copy from your instructor. You will create a Student class with public attributes (or fields or instance variables) firstName and lastName. It is not conventional to use public, but you need to know the difference between public and private attributes, and we will fix this in 9B.

Objectives:

? Create and use classes and attributes.

? Create and use public attributes. (Note: this is not usually a good idea in the real world; we will fix it in Program 9B.)

? Instantiate objects.

? Use the default constructor.

? Demonstrate use of the dot operator.

? Use the this modifier in a class.

? Create a toString method and a use it. Instructions: 1) Call your class Program9A, so your filename will be Program9A.java. It is essential for grading purposes that everyone have the same class name. Please dont stay completely stuck for much over an hour without contacting the instructor or the tutor.

2) Copy the entire program 7 and paste it into 9A. Be sure to change the class name to 9A.

3) Create several lines of comments of identification and description at the top of the file (it doesnt have to look exactly like this, in particular, if your editor wants to put * in front of every line, thats fine): /* Assignment : Program 9A Student Sort Program with Public Attributes Author : First Last Course : Programming I, RSU Created : nn/nn/2017 Description : [You will need to replace this with your own description.] */

4) Create a new class called Student. You may do this either by creating a new file called Student.java with a class called Student, or you may make an internal class that is inside your Program9A class. Remember, by convention, class names should 2 Revised:4/30/2018 12:30:00 PM always start with a capital letter; however, the compiler will accept a lowercase letter, so lowercase is legal, but it violates agreed-upon conventions.

5) Create several lines of comments of identification and description at the top of the Student file similar to those at the top of the Program9A file.

6) Create public attributes of type String called firstName and lastName. These by convention should start with lowercase. It violates one of the main purposes of classes to have public attributes, but we will fix this in Program 9B. The reason to do this is so that you understand the differences between public and private attributes.

7) Create a method in Student class called toString. You should read more about this special method in Chapter 11 Section 6. The toString method converts an object with attributes of any type into a String so that it can be printed. Most classes that will be output should have a toString. Also debuggers use the toString to help you debug your program, so its usually a good idea to have one unless your class has tons of attributes. Your toString method should look something like this: public String toString () { return lastName + ", " + firstName; }

8) In Program9 class, change your array from type String to type Student.

9) If you called your array something like names or studentNames, you should change it because now it is of type Student, so something like students would be appropriate; I will be assuming that it is now called students in the rest of my instructions. Array names usually are plural because you have more than one element. In IntelliJ, you can right click on studentNames in the line where you declared it, click on Refactor, then Rename, then type students, and hit Enter; it will rename that variable everywhere. Notice that the array name students starts with lowercase and is plural.

10) In enterStudentNames, you will need to create a Student object using the default constructor since we didnt put an explicit constructor into our Student class as we should always do (dont worry, well rectify this in Program B). You will set an element of the students array to this new student object (see code below). When you dont supply your class with a constructor, it creates one for you called the default constructor. This default constructor takes no parameters and sets all reference type attributes (like String) to null and all primitive numeric types to zero. (This is an exam question.) You will set an element of your array to store that new object. Then you will need to set that objects attributes to the first and last names that you got from the user; the dot operator accesses an attribute or method name from the class. Mine looks like this: students[actualNumberStudents] = new Student (); students[actualNumberStudents].firstName = firstName; students[actualNumberStudents].lastName = lastName; 3 Revised:4/30/2018 12:30:00 PM These statements will replace the one where you assigned your arrays element in 7 and you no longer need the variable fullStudentName. I called my array students and this is in the for loop where Im incrementing actualNumberStudents. My local String variables that hold my user input are called firstName and lastName just like the attribute names, but they do NOT have to match the attribute names. I might have called them firstNameInput and lastNameInput or anything else that is descriptive. It would be legal to call them x and y, but that would not be meaningful, readable, or maintainable. Note that this is setting the lastName and firstName attributes in the Student class. We shouldnt be allowed to do this directly, but well fix it in Program B. The reason we can do it here is that you declared them public. 11) Your if statement in your sort method will no longer work because the compareTo expects a String, not a type Student. So you need to replace that line with something like this: if (students[i].toString().compareTo(students[i - 1].toString()) < 0) We could write a compareTo function for our Student class, but thats getting beyond the scope of this course (youll do that next semester). The call to toString will turn students[i] into a String of with lastName concatenated to a comma and a blank and concatenated to firstName. This is not as efficient as just using last and first names, but it will do the job and we already needed a toString method to make printing easier.

12) Make any other changes you need to in main because of this change using an array of type Student. You will now be passing an array of type Student to your sort and print methods, so you will need to change your parameters to type Student[] instead of String[]. Fix anything in the methods that this change requires. (For example, in the sort method, you will probably have a String temp variable that needs to be changed to Student temp.)

13) You should change sortStudentNames to sortStudents since you are now not just sorting names but objects called Student. If you want, you could also change printStudentNames to printStudents, but there is an argument to be made either way on that method name. Again, you can use IntelliJs Refactor/Replace to change the name of the method(s) like you did the name of the variable.

14) In the print method, you will need to comment out your 7 printf statement and put something like this: System.out.printf("%2d. %s, %s ", i+1, students[i].lastName, students[i].firstName); Note that this is getting the lastName and firstName attributes from the Student class. We shouldnt be allowed to do this directly, but well fix it in Program B.

15) That should do it for changing everything to use the Student class unless you did some of the extra credit methods in the previous assignment. If so, you should fix them to use the Student class as well. 4 Revised:4/30/2018 12:30:00 PM

16) Get all that running, then well tackle some other things regarding toString.

17) Call toString by commenting out the printf statement in your print method and using this line instead (if your for loop variable is not i, substitute your own variable name there): System.out.printf("%2d. %s ", i+1, students[i].toString());

18) Compile and get that working. Then eliminate .toString() . You dont need it because the compiler automatically knows to call toString when the object is not of type String. Recompile and test that; youll see it does the same thing.

19) Now, lets do one more thing to toString method in Student. Lets make the first names all line up by using the String.format method. Do this by commenting out the current return statement and replace it with this one: return String.format ("%-12s, %-12s", lastName, firstName);

20) The String.format method works like the printf method with the format specifier string followed by the variables whose values will be inserted into it. Youve already seen %s where any string variable will go there, but %12s means that it will take 12 characters even if it is shorter, and %-12s has a negative sign, which means to leftjustify. The comma and blank in the format specifier string will literally put a comma and blank into the string as required by the instructions in 7.

21) That last change does NOT require any changes to the calling of toString, so compile and fix any compile errors. Then when it compiles, run it, test it, and debug it.

22) Extra Credit: Note that, when we are using toString in the compareTo call in our if statement, this change to toString will still work, but it will make the compareTo much more inefficient since now it is comparing lots of blanks to each other. Like I mentioned before, the best way to handle this is to write our own compareTo method in our Student class, but thats beyond the scope of this chapter. If you would like 10% extra credit, research this on your own on the Internet (there might be something on it later in our book), and create a compareTo method in Student class. Then change that line to call your compareTo instead of Strings compareTo. Be sure to note at the top of your Word file that you did this extra credit.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!