cs4hs Logo

Let's get started with a multiple classes application!

  1. Open Eclipse
  2. Right-Click on your cs4hs.sp18..your-username
  3. Choose: File -> New -> class
  4. In the Name: textbox type: CS4HS_Course
  5. Click: Finish
  6. Copy the CS4HS_Course.java Template (Java) code (below)
  7. Paste that code into your new CS4HS_Course class in the editor
  8. Change both occurrences of put-your-username-here
  9. Replace the blanks on these lines
    cs4hs.addStudent("________", "________", 3); // Populate cs4hs
    cs4hs.addStudent("________", "________", 3); // Populate cs4hs
    cs4hs.findStudent("________").addGrade(1, 100); // Search records and modify
    cs4hs.findStudent("________").addGrade(1, 60);

    and theses lines:
    System.out.println(cs4hs.findStudent("________").getName() + " (" + cs4hs.findStudent("________").getID()
    + ") has a grade of: " + cs4hs.findStudent("________").getGrade(1) + " on exam 1");
    System.out.println(cs4hs.findStudent("________").getName() + " (" + cs4hs.findStudent("________").getID()
    + ") has a grade of: " + cs4hs.findStudent("________").getGrade(1) + " on exam 1");
    System.out.println("Class average for exam 1: " + cs4hs.computeAverage(1)); // calculate and print output to terminal

    with two (2) unique IDs and student names
  10. Choose: File -> New -> class
  11. In the Name: textbox type: CS4HS_Student
  12. Click: Finish
  13. Copy the CS4HS_Student.java Template (Java) code (below)
  14. Paste that code into your new CS4HS_Student class in the editor
  15. Change both occurrences of put-your-username-here
  16. Choose: File -> New -> class
  17. In the Name: textbox type: Student
  18. Click: Finish
  19. Copy the Student.java Template (Java) code (below)
  20. Paste that code into your new Student class in the editor
  21. Change both occurrences of put-your-username-here
  22. Click the CS4HS_Course tab
  23. Run icon

CS4HS_Course.java Template (Java)

package cs4hs.sp18..put-your-username-here;

/**
 * File: CS4HS_Course.java
 *
 * Description: This class creates a cs4hs_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 CS4HS_Course {
  
  private CS4HS_Student roster[];
  private int student_num, counter = 0;
  
  /**
   * CS4HS_Course() constructor creates a Course object which
   * is comprised of an array of CS4HS_Student objects
   * @param capacity int -- the given class size
   */
  public CS4HS_Course( int capacity ) {
    roster = new CS4HS_Student[capacity];
    student_num = capacity;
  } // end CS4HS_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) {
      CS4HS_Student newStudent = new CS4HS_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 CS4HS_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[] ) {   
    CS4HS_Course cs4hs = new CS4HS_Course (30); // Declares a new cs4hs_Course object
    cs4hs.addStudent("________", "________", 3); // Populate cs4hs
    cs4hs.addStudent("________", "________", 3); // Populate cs4hs
    cs4hs.findStudent("________").addGrade(1, 100); // Search records and modify
    cs4hs.findStudent("________").addGrade(1, 60);
    // modifications made here which print each student and their grade for exam 1
    System.out.println(cs4hs.findStudent("________").getName() 
    + " (" + cs4hs.findStudent("________").getID() + 
    ") has a grade of: "
      + cs4hs.findStudent("________").getGrade(1) + " on exam 1");
    System.out.println(cs4hs.findStudent("________").getName() 
    + " (" + cs4hs.findStudent("________").getID() + 
    ") has a grade of: "
      + cs4hs.findStudent("________").getGrade(1) + " on exam 1");
    System.out.println("Class average for exam 1: " 
    + cs4hs.computeAverage(1)); // calculate and print output to terminal
    System.exit(0);
  } // end main()
} // end cs4hs_Course class

CS4HS_Student.java Template (Java)

package cs4hs.sp18..put-your-username-here;

/**
 * File: CS4HS_Student.java
 *
 * Description: Defines the CS4HS_Student class which 
 * inherits from Student
 *
 * Fill in the blanks to make the program work 
 *
 */
/**
 *
 * @author put-your-name-here 
 *
 */
 
public class CS4HS_Student extends Student {
  
  private int exam_array[];
  private int exam_count;
  
  /**
   * CS4HS_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 CS4HS_Student( String id, String name, int num_exams) {
    super(id, name);
    exam_array = new int [num_exams];
    exam_count = num_exams;
  } // end CS4HS_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 cs4hs_Student class

Student.java Template (Java)

package cs4hs.sp18..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

CS4HS_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