Let's get started with a multiple classes application!
Open Eclipse
Right-Click on your edu.buffalostate.cis425.fa10.exercises..your-username
Choose: File -> New -> class
In the Name:
textbox type: BSC_Course
Click: Finish
Copy the BSC_Course.java
Template (Java) code (below)
Paste that code into your new BSC_Course class in the editor
Change both occurrences of put-your-username-here
Replace the blanks on these lines
bsc.addStudent("________" , "________" , 3); // Populate bsc
bsc.addStudent("________" , "________" , 3); // Populate bsc
bsc.findStudent("________" ).addGrade(1, 100); // Search records and modify
bsc.findStudent("________" ).addGrade(1, 60);
and theses lines:
System.out .println(bsc.findStudent("________" ).getName() + " (" + bsc.findStudent("________" ).getID()
+ ") has a grade of: " + bsc.findStudent("________" ).getGrade(1) + " on exam 1" );
System.out .println(bsc.findStudent("________" ).getName() + " (" + bsc.findStudent("________" ).getID()
+ ") has a grade of: " + bsc.findStudent("________" ).getGrade(1) + " on exam 1" );
System.out .println("Class average for exam 1: " + bsc.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: BSC_Student
Click: Finish
Copy the BSC_Student.java
Template (Java) code (below)
Paste that code into your new BSC_Student class in the editor
Change both occurrences of put-your-username-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-username-here
Click the BSC_Course tab
Run icon
BSC_Course.java
Template (Java)
package edu.buffalostate.cis425.fa10.exercises..put-your-username-here ;
/**
* File: BSC_Course.java
*
* Description: This class creates a bsc_Course
* object which contains student records which can be
* modified
*
* Fill in the blanks to make the program work
*
*/
/**
*
* @author put-your-name-here
*
*/
public class BSC_Course {
private BSC_Student roster [];
private int student_num , counter = 0;
/**
* BSC_Course() constructor creates a Course object which
* is comprised of an array of BSC_Student objects
* @param capacity int -- the given class size
*/
public BSC_Course( int capacity ) {
roster = new BSC_Student[capacity ];
student_num = capacity ;
} // end BSC_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 ) {
BSC_Student newStudent = new BSC_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 BSC_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[] ) {
BSC_Course bsc = new BSC_Course (30); // Declares a new bsc_Course object
bsc.addStudent("________" , "________" , 3); // Populate bsc
bsc.addStudent("________" , "________" , 3); // Populate bsc
bsc.findStudent("________" ).addGrade(1, 100); // Search records and modify
bsc.findStudent("________" ).addGrade(1, 60);
// modifications made here which print each student and their grade for exam 1
System.out .println(bsc.findStudent("________" ).getName()
+ " (" + bsc.findStudent("________" ).getID() +
") has a grade of: "
+ bsc.findStudent("________" ).getGrade(1) + " on exam 1" );
System.out .println(bsc.findStudent("________" ).getName()
+ " (" + bsc.findStudent("________" ).getID() +
") has a grade of: "
+ bsc.findStudent("________" ).getGrade(1) + " on exam 1" );
System.out .println("Class average for exam 1: "
+ bsc.computeAverage(1)); // calculate and print output to terminal
System.exit(0);
} // end main()
} // end bsc_Course class
BSC_Student.java
Template (Java)
package edu.buffalostate.cis425.fa10.exercises..put-your-username-here ;
/**
* File: BSC_Student.java
*
* Description: Defines the BSC_Student class which
* inherits from Student
*
* Fill in the blanks to make the program work
*
*/
/**
*
* @author put-your-name-here
*
*/
public class BSC_Student extends Student {
private int exam_array [];
private int exam_count ;
/**
* BSC_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 BSC_Student( String id , String name , int num_exams ) {
super (id , name );
exam_array = new int [num_exams ];
exam_count = num_exams ;
} // end BSC_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 bsc_Student class
Student.java
Template (Java)
package edu.buffalostate.cis425.fa10.exercises..put-your-username-here ;
/**
* File: Student.java
*
* Description: This class defines the student class
*
*/
/**
*
* @author put-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
BSC_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