gcp Logo

Let's get started with a multiple classes application!

  1. Login to your Repl.It account.
  2. Click on: Languages
  3. In the Popular section choose: Java: A concurrent, class-based, statically typed object-oriented language.
  4. Click the pencil icon next to your repl name.
  5. In the Default name: input box type: GCP_Course
  6. Delete all the lines of code provided as an example.
  7. Copy the GCP_Course.java Template (Java) code (below)
  8. Paste that code into your new GCP_Course class in the editor
  9. Change the @author put-your-username-here
  10. Replace the blanks on these lines
    gcp.addStudent("________", "________", 3); // Populate gcp
    gcp.addStudent("________", "________", 3); // Populate gcp
    gcp.findStudent("________").addGrade(1, 100); // Search records and modify
    gcp.findStudent("________").addGrade(1, 60);

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

    with two (2) unique IDs and student names
  11. Click on the Add File (+) icon.
  12. Name this file: GCP_Student.java
  13. Delete all the lines of code provided as an example.
  14. Copy the GCP_Student.java Template (Java) code (below)
  15. Paste that code into your new GCP_Student class in the editor
  16. Change both occurrences of put-your-username-here
  17. Click on the Add File (+) icon.
  18. Name this file: Student.java
  19. Delete all the lines of code provided as an example.
  20. Copy the Student.java Template (Java) code (below)
  21. Paste that code into your new Student.java class in the editor
  22. Change both occurrences of put-your-username-here
  23. Click the GCP_Course tab
  24. Run icon

GCP_Course.java Template (Java)

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

GCP_Student.java Template (Java)

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

Student.java Template (Java)

/**
 * 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

GCP_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