For this Lab your task is to expand the Student class you created in Lab 7. In this expansion, we will add two data members to the Student class, modify existing methods to account for these new data members, and add new methods to use these data members.
Load your previous Person.java file or the Person class from your previous Lab7.java. If you do not have the Person class or did not complete Lab 7, you can download the Lab 7 solution from the Blackboard and use the Person class provided therein as your starting point. If you decide to work from your previous Lab 7 file, be sure to rename both the main class and the
In this lab, you will be adding two new data members to the Students class. In the previous lab, we used the Student ID, Major, Grade Points, and Credits members. In this lab, we will add the First Name and Last Name members to the class. Both of these new members will be of the String type.
private String studentID;
private String studentMajor;
private int studentPoints;
private int studentCredits;
// NEW
private String studentFirstName;
private String studentLastName;
Two additional data members in the class indicate that we must modify the constructor method of the Student class in order to accommodate these new members. In order to do this, add two additional arguments to the standard constructors input arguments, one for the first name and one for the last name, as below.
Student(< old arguments >,String fname, String lname)
These getter and setter methods are similar to the ones we created for Lab 7. Refer to the Lab 7 handout for the creation and function of getter and setter methods.
In addition to the getter and setter methods, we will create two new methods, one to get the students full name, and one to change the students major. The logic for the getFullName method is given below:
public String getFullName() {
// In this function, concatenate the student’s
// firstname and lastname together with a space
// between and return the result.
}
The method used to change the students major will be an overloaded method. That is, the changeMajor functionality is actually two methods, both with the same name that accept a different number of input arguments. The first option will take only a string and will change the Student objects major variable to the new input.
public void ChangeMajor(String input) {
// Change the value of the Student object’s major
// variable to the new input’s value.
}
The second option, however, will be slightly more complex. In the second version of the changeMajor function, we will accept a new major string, as before, and two integers, a new value for the students number of grade points and credits. As changing your major is likely to reduce the number of credits and grade points that qualify for your degree, we want to be able to change all three at the same time. However, since changing your major will only reduce the values of these variables, we need to check the inputs to be sure they are less than or equal to the current values before we assign the new values to their appropriate data members.
public void ChangeMajor(String newMajor, int newPoints, int newCredits) {
// If newPoints and newCredits are less than or equal
// to their respective data member variable, update
// the student object’s major variable, the student
// object’s credits variable, and the student object’s
// grade points variable to their new values.
// Otherwise, print an error message.
}
The final task of this lab is to update the Lab 8 classs main function to use the new members weve added to the Student class.
public class Lab8 {
public static void main(String[] args) {
// Create a new Scanner object to take in input from
// the user.
// ->
// Prompt the user for and input their first name.
// ->
// Prompt the user for and input their last name.
// ->
// Prompt the user for and input their student id.
// ->
// Prompt the user for and input their major.
// ->
// Create a new Student object using the first name,
// last name, student id, and major given by the
// student. Since we did not prompt for credits and
// points, use 50 and 175, respectively.
// ->
// Create a new DecimalFormat object to print out the
// GPA in the traditional, two-decimal place format.
// ->
// Print out the student’s information in the
// following format:
// Student Information:
// ->
//is majoring in
// ->
// The student’s ID number is: < Student ID>
// ->
// Their GPA is < Student’s GPA, formatted as above>
// ->
// Attempt to change the major to
// “Underwater Basket-Weaving” with 300 points and 75
// credits. This should not succeed.
// Change just the student’s major to
// “Underwater Basket-Weaving”
// Print out the following:
// < Student full name> has changed majors to < Student
// Major>
// Their GPA is now: < GPA, formatted as above>
}
}
Sample Output
Below is an example of what your output should roughly look like when this lab is completed. Text in bold represents user input.
Please enter your first name:
Isaac
Please enter your last name:
Jones
Please enter your student id:
123456789
Please enter your major:
Computer Science
Student Information:
Isaac Jones is majoring in Computer Science
The student’s ID number is 123456789
Their GPA is: 3.5
Invalid attempt to change major.
Isaac Jones has changed majors to Underwater Basket Weaving
Their GPA is now 3.5
Please enter your first name:
Albert
Please enter your last name:
Einstein
Please enter your student id:
987654321
Please enter your major:
Mathematics
Student Information: Albert Einstein is majoring in Mathematics
The student’s ID number is 123456789
Their GPA is: 3.5
Invalid attempt to change major.
Albert Einstein has changed majors to Underwater Basket Weaving
Their GPA is now 3.5