Let's get started with a multiple classes application!
Login to your Repl.It account.
Click on: Languages
In the Popular
section choose: Java: A concurrent, class-based, statically typed object-oriented language.
Click the pencil
icon next to your repl name.
In the Default name:
input box type: GCP_Course
Delete all the lines of code provided as an example.
Copy the GCP_Course.java
Template (Java) code (below)
Paste that code into your new GCP_Course class in the editor
Change the @author put-your-username-here
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
Click on the Add File (+)
icon.
Name this file: GCP_Student.java
Delete all the lines of code provided as an example.
Copy the GCP_Student.java
Template (Java) code (below)
Paste that code into your new GCP_Student class in the editor
Change both occurrences of put-your-username-here
Click on the Add File (+)
icon.
Name this file: Student.java
Delete all the lines of code provided as an example.
Copy the Student.java
Template (Java) code (below)
Paste that code into your new Student.java
class in the editor
Change both occurrences of put-your-username-here
Click the GCP_Course tab
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