Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
  
Lab 9: Creating a Course Class 
 
Introduction: 
 
As we have seen, it is possible to define Java classes where the private variables are objects from 
one or more other classes. It is also possible to declare arrays of objects as private variables. In 
this lab we will be storing information about multiple students in a “Course” class. To do this, 
we will create private variable in the “Course” class that is an array of “Student” objects. We will 
then implement several methods in the “Course” class to store, manipulate, and print information 
about the Students in this course. 
 
 
 
Instructions: 
 
Consider the following Java code. The “Student” class is the from the previous lab. The 
“Course” class is a “skeleton” that includes two private variables and a collection of empty 
method definitions. The “Main” class contains code to test the first two classes. 
 
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 Course 
{ 
    private Student[] student; 
    private int count; 
     
    public Course(){ } 
    public Course(Course param){ } 
    public void store(Student param){ } 
    public void print(){ } 
    public Student topStudent(){ } 
} 
 
public class Main 
{ 
   public static void main(String[] args) 
    { 
        System.out.println("\nTesting the Student class"); 
        Student student1 = new Student(321, 3.21, "Clark Kent",  
"101 Metro Road"); 
        Student student2 = new Student(345, 3.45, "Mary Richards",  
"42 State Street"); 
        Student student3 = new Student(246, 2.46, "Charlie Brown",  
"50 Peanut Lane"); 
        student1.print(); 
        student2.print(); 
        student3.print(); 
 
        System.out.println("\nTesting the Course class"); 
        Course course = new Course(); 
        course.store(student1); 
        course.store(student2); 
        course.store(student3); 
        course.print(); 
 
        System.out.println("\nCopy of course"); 
        Course copy = new Course(course); 
        copy.print(); 
        System.out.println("\nTop Student:"); 
        Student top = copy.topStudent(); 
        top.print(); 
        top.setGPA(4.0); 
        copy.print();  
   } 
} 
 
Step 1: Copy the code above into Student.java, Course.java and Main.java in your Java IDE. 
When you compile it you should see several error messages caused by the incomplete methods in 
the “topStudent” method in the “Course” class. To make these error messages go away, you must 
add one line of code to the method to “return new Student();”.  
 
Step 2: Your first task is to add code in the “Course” constructor method to initialize the two 
private variables. You should set count to zero, and create an array of 100 Student objects. When 
you compile and run your code now, it should print the “Student” test messages but the “Course” 
messages should still be empty. 
 
Step 3: Next, you should implement the “store” method. The idea is to make a copy of the 
“Student” parameter and store it in the next available location in the array, and add one to the 
count variable. You should compile and run again to make sure you have no syntax errors. 
 
Step 4: Now that you have “Students” in the array, you can implement the “print” method. Here 
you will need to loop from location 0 to count-1 and call the “Student” print method. Remember 
the notation for calling a method in this way is “student[i].print()”. Now when you compile and 
run your program you should see several student records being printed. 
 
Step 5: Your next task is to implement the copy constructor for the “Course” class. Here you 
need to create a new array of 100 Student objects, and then loop over the “Student” array in the 
parameter “Course” to copy them into your new array of “Students”. Once this is done, your 
testing program should show the “copy” object matches the original “course” object.  
 
Step 6: Finally, edit the “Course” class to implement the “topStudent” method. Here you will 
need to loop over all of the “Students” in the array and compare their GPA values to locate the 
array location “top” containing the student with the highest GPA value. This can be done with 
the following: “if (student[index].getGPA() > student[top].getGPA()) top = index;”. Once you 
know the correct value of “top” you can return “student[top]”. 
 
Step 7: When your program is working correctly, upload your final program and a copy of your 
program output into Blackboard for grading.