Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
  
Lab 8: Extending the Student Class 
 
Introduction: 
 
In object oriented programming, the goal is to combine data and operations that belong together 
to create "objects" that can be used to store and manipulate data in application programs. In this 
lab we will be working with the "Student" class. As you will see, this class is used to store and 
manipulate typical pieces of information associated with a student. We will be extending this 
class by adding a copy constructor and several new methods. In the next lab, we will be using 
this class to store and process data for several students. 
 
Instructions: 
 
Consider the following Java code. 
 
public class Student 
{ 
    private int ID; 
    private double GPA; 
    private String Name; 
    private String Address; 
     
    public Student() 
    { 
        setID(0); 
        setGPA(0); 
        Name = "none"; 
        Address = "none"; 
    } 
     
    public Student(int id, double gpa, String name, String address) 
    { 
        setID(id); 
        setGPA(gpa); 
        Name = name; 
        Address = address; 
    } 
     
    public void setID(int id) 
    { 
        ID = id; 
        if (ID < 0) 
            ID = 0; 
        if (ID > 99999) 
            ID = 99999; 
    } 
     
    public void setGPA(double gpa) 
    { 
        GPA = gpa; 
        if (GPA < 0) 
            GPA = 0; 
        if (GPA > 4) 
            GPA = 4; 
    } 
     
    public void setName(String name) 
    { 
        Name = name; 
    } 
     
    public void setAddress(String address) 
    { 
        Address = address; 
    } 
     
    public int getID() 
    { 
        return ID; 
    } 
     
    public double getGPA() 
    { 
        return GPA; 
    } 
     
    public String getName() 
    { 
        return Name; 
    } 
     
    public String getAddress() 
    { 
        return Address; 
    } 
     
    public void print() 
    { 
        System.out.println("ID: " + ID); 
        System.out.printf("GPA: %4.2f\n", GPA); 
        System.out.println("Name: '" + Name + "'"); 
        System.out.println("Address: '" + Address + "'"); 
    } 
} 
 
  
public class Main 
{ 
    // Testing the Student class 
    public static void main(String[] args) 
    { 
        System.out.println("\nTesting the Student class"); 
         
        Student student1 = new Student(123, 3.14,  
"John Smith", "101 Main Street"); 
        System.out.println("\nStudent information: "); 
        student1.print(); 
         
        Student student2 = new Student(-123, 5.14,  
"Tom Jones", "42 State Street"); 
        System.out.println("\nStudent information: "); 
        student2.print(); 
    } 
} 
 
Step 1: Copy the code above into Student.java and Main.java in your Java IDE and compile it. 
When you run the program you should see several testing messages that demonstrate how the 
constructor and print messages work. Notice that the incorrect ID and GPA values have been 
automatically corrected. 
 
Step 2: Your first task is to add a copy constructor to the Student class. This method only has 
one parameter “Student param” which is used to initialize all four private variables. There are 
two ways you can access the variables in the parameter object. You can call the getter methods 
using “param.getID()” or you can access the fields directly using “param.ID” because we are 
inside the Student class. 
 
Step 3: To test the copy constructor, edit the main program to create student3 that is a copy of 
student1. Now change the value of one of the private variables in student1, and print both objects 
out. You should see that the private variable you changed was only changed in one of the 
objects. 
 
Step 4: Your next task is to add a read method to the Student class. You should prompt the user 
to enter each of the private variables, and use the Scanner methods nextInt, nextFloat and 
nextLine to read the input from the user. As you know, the nextInt and nextFloat methods will 
skip over spaces and newline characters waiting for the user to enter an integer or float value. 
The nextLine method will read all characters up to the next newline and store them in a String. 
This will let the user enter a name or address like “John Smith” or “101 Main Street”.   
 
Step 5: To test the read method, edit the main program to call the read method to update the 
information in student3. Call the print method to display this data. You may notice that your 
name and address values are incorrect. If you call nextLine directly after nextInt or nextFloat, 
you will whatever the user types after the integer or float value before the newline. This will 
typically give you an empty string. You will need to call nextLine a second time to get the name 
or address entered by the user.  
 
Step 6: You will notice that the setName and setAddress methods above do not have any error 
checking. There is very little we could do to verify that a name or address is correct because 
there are literally billions of valid values. There is one small thing we should do, and that is to 
remove any spaces the user enters at the start or the end of the string. Fortunately, there is a 
method in the String class that will do this for us. Edit the String class and add “Name = 
name.trim() and “Address = address.trim()” to the setName and setAddress methods. 
 
Step 7: Finally, edit the Student class and edit all methods that store Name or Address values 
and add calls the setName and setAddress methods to ensure we are always trimming the strings. 
When you run the program again, you should see that any spaces you type at the start or end of 
the name or address will be removed when you print the student3 object. 
 
Step 8: When your program is working correctly, upload your final program and a copy of your 
program output into Blackboard for grading.