Let's get started with a multiple classes application!
Open Eclipse
Right-Click on your buffalo.edu.fa20. package
Choose: File -> New -> class
In the Name: textbox type: UB_Course
Click: Finish
Copy the UB_Course.java Template (Java) code (below)
Paste that code into your new UB_Course class in the editor
Change both occurrences of put-your-lastname-here
Replace the blanks on these lines ub.addStudent("________", "________", 3); // Populate ub
ub.addStudent("________", "________", 3); // Populate ub
ub.findStudent("________").addGrade(1, 100); // Search records and modify
ub.findStudent("________").addGrade(1, 60);
and theses lines: System.out.println(ub.findStudent("________").getName() + " (" + ub.findStudent("________").getID()
+ ") has a grade of: " + ub.findStudent("________").getGrade(1) + " on exam 1");
System.out.println(ub.findStudent("________").getName() + " (" + ub.findStudent("________").getID()
+ ") has a grade of: " + ub.findStudent("________").getGrade(1) + " on exam 1");
System.out.println("Class average for exam 1: " + ub.computeAverage(1)); // calculate and print output to terminal
with two (2) unique IDs and student names
Choose: File -> New -> class
In the Name: textbox type: UB_Student
Click: Finish
Copy the UB_Student.java Template (Java) code (below)
Paste that code into your new UB_Student class in the editor
Change both occurrences of put-your-lastname-here
Choose: File -> New -> class
In the Name: textbox type: Student
Click: Finish
Copy the Student.java Template (Java) code (below)
Paste that code into your new Student class in the editor
Change both occurrences of put-your-lastname-here
Click the UB_Course tab
Run icon
UB_Course.java Template (Java)
package buffalo.edu.fa20.put-your-lastname-here;
/**
* File: UB_Course.java
*
* Description: This class creates a ub_Course
* object which contains student records which can be
* modified
*
* Fill in the blanks to make the program work
*
*/
/**
*
* @authorput-your-name-here
*
*/public class UB_Course {
private UB_Student roster[];
private intstudent_num, counter = 0;
/**
* UB_Course() constructor creates a Course object which
* is comprised of an array of UB_Student objects
* @param capacity int -- the given class size
*/public UB_Course( int capacity ) {
roster = new UB_Student[capacity];
student_num = capacity;
} // end UB_Course()/**
* addStudent() adds a student record to the roster array
* @param name string -- student name
* @param num_exams Int -- number of exams
* @return boolean true/false
*/public boolean addStudent( String id, String name, int num_exams ) {
if (counter != student_num) {
UB_Student newStudent = new UB_Student(id, name, num_exams);
roster[counter] = newStudent;
counter++;
return true;
} else {
return false;
}
} // end addStudent()/**
* findStudent() searches the roster array for
* a student record
* @param id string -- student's id
* @return object -- student
*/
public UB_Student findStudent( String id ) {
for (int i = 0; i < student_num; i++) {
if (roster[i].getID() == id) {
return roster[i];
}
}
return null;
} // end findStudent()/**
* computeAverage()
* @param exam int -- exam number
* @return double -- average for exam
*/
public double computeAverage( int exam ) {
double temp_grades = 0;
for (int i = 0; i < counter; i++) {
temp_grades += roster[i].getGrade(exam);
}
return temp_grades / counter;
} // end computeAverage()/**
* main()
* @param args string array of command line arguments
*/public static void main( String args[] ) {
UB_Course ub = new UB_Course (30); // Declares a new ub_Course object
ub.addStudent("________", "________", 3); // Populate ub
ub.addStudent("________", "________", 3); // Populate ub
ub.findStudent("________").addGrade(1, 100); // Search records and modify
ub.findStudent("________").addGrade(1, 60);
// modifications made here which print each student and their grade for exam 1
System.out.println(ub.findStudent("________").getName()
+ " (" + ub.findStudent("________").getID() +
") has a grade of: "
+ ub.findStudent("________").getGrade(1) + " on exam 1");
System.out.println(ub.findStudent("________").getName()
+ " (" + ub.findStudent("________").getID() +
") has a grade of: "
+ ub.findStudent("________").getGrade(1) + " on exam 1");
System.out.println("Class average for exam 1: "
+ ub.computeAverage(1)); // calculate and print output to terminal
System.exit(0);
} // end main()
} // end ub_Course class
UB_Student.java Template (Java)
package buffalo.edu.fa20.put-your-lastname-here;
/**
* File: UB_Student.java
*
* Description: Defines the UB_Student class which
* inherits from Student
*
* Fill in the blanks to make the program work
*
*/
/**
*
* @authorput-your-name-here
*
*/public class UB_Student extends Student {
private intexam_array[];
private intexam_count;
/**
* UB_Student() constructor creates a student object
* @param id string -- the student's id
* @param name string -- the student's name
* @param num_exams int -- the number of exams
*/public UB_Student( String id, String name, intnum_exams) {
super(id, name);
exam_array = new int [num_exams];
exam_count = num_exams;
} // end UB_Student()
/**
* addGrade() adds grades to a given student.
* @param exam int -- the exam number
* @param grade int -- the grade of the exam
*/
public boolean addGrade( int exam, int grade ) {
if ((exam_array[exam] >= 0) && (exam_array[exam] <= exam_count)) {
exam_array[exam] = grade;
return true;
} else {
return false;
}
} // end addGrade()/**
* getGrade() retrieves the grade at a specific location
* in the exam_array
* @param exam int -- the exam to be checked
*/
public int getGrade( int exam ) {
if ((exam_array[exam] >= 0) || (exam_array[exam] <= exam_count)) {
return exam_array[exam];
} else {
return -1;
}
} // end getGrade()
public String getName() {
return super.getName();
} // end getName()
public String getID() {
return super.getID();
} // end getID()
public int getCount(){
return exam_count;
} // end getCount()
} // end ub_Student class
Student.java Template (Java)
package buffalo.edu.fa20.put-your-lastname-here;
/**
* File: Student.java
*
* Description: This class defines the student class
*
*/
/**
*
* @authorput-your-name-here
*
*/public class Student {
private String id, name;
/**
* Constructor, set name of student
* @param name string -- student name
*/public Student( String id, String name ) {
this.setName(name);
this.setID(id);
} // end Student()/**
* return name of student
* @return name string -- student name
*/public String getName() {
return this.name;
} // end getName()/**
* set name of student
* @param name string -- student name
*/public void setName(String name){
this.name = name;
} // end setName()/**
* return id of student
* @return id string -- student id
*/public String getID() {
return this.id;
} // end getID()/**
* set id of student
* @param id string -- student id
*/public void setID(String id){
this.id = id;
}
} // end Student class
UB_Course Class Output
Sally Smarty (B0000001) has a grade of: 100 on exam 1
Phil Phailure (B0000002) has a grade of: 60 on exam 1
Class average for exam 1: 80.0