Question: Copy the entire program 9A and paste it into this one. Be sure to change the class name to 9B. Use descriptive variable names in

Copy the entire program 9A and paste it into this one. Be sure to change the class name to 9B.

Use descriptive variable names in camel-case with the first letter in lowercase.

In the Student class, change the public attributes firstName and lastName to private ones.

In the Student class, add getters and setters called getFirstName, getLastName, setFirstName, setLastName for the two attributes.

In the Student class, add a constructor for the Student class that accepts two Strings, one parameter that you will assign to firstName and the other parameter that you will assign to lastName. If you want, you can call your parameters firstName and lastName just like the attribute names, but if you do, you will have to call your attribute names this.firstName and this.lastName within the constructor so that the compiler will know which firstName is the parameter and which is the attribute. If you call the parameters by some other names like fn and ln, or firstNameInit and lastNameInit, then you dont have to use the this modifier within your constructor. Normally, fn is NOT a good variable name because its not very descriptive, but in a constructor, there are only a few lines of code, so it should be obvious what these short parameter names stand for, and this is common in the real world. I personally prefer something like firstNameInit, but Im a purist.

You could set each attribute here, but it would be better to call both of your setters. Here is one of my setter calls in my constructor:

setFirstName(firstNameInit); 

In main, where you called the default constructor, call your new constructor and pass firstName and lastName as arguments, then you wont need the next two lines where you made the assignments to the attributes.

In your print method, you wont be able to access the attributes directly anymore, so you need to comment out that line with the print statement and redo it by calling the getter methods:

System.out.printf("%2d. %s, %s ", i+1, students[i].getLastName(), students[i].getFirstName()); 

Get all this working before moving on.

Now comment out the printf lines you just made and replace with this line:

System.out.printf("%2d. %s ", i+1, students[i].toString()); 

Note that you only need ONE %s in the format specifier now.

Get this working before moving on.

Now comment out those lines you just made and replace with this line:

System.out.printf("%2d. %s ", i+1, students[i]); 

The reason this still works is that when you try to print something into a String, the compiler automatically checks to see whether there is a toString method for this class, and if so, it replaces it there for you.

Run it with the Sample Data and make your screen prints and the other sections of the Word file.

Please dont stay completely stuck for much over an hour without contacting the instructor.

Sample Runs:

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

Here's 9A

public class Student { public String firstName; // first name public String lastName; // last name

public Student(String first, String last) { firstName = firstName; lastName = lastName; }

public String toString() {

return String.format ("%-12s, %-12s", firstName, lastName);

}

}

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!