I'm working on a project and have been racking my brain to meet this one requirement. I have two classes Roster and Student. Student contains all of the private variables and Student contains methods to access objects in the Student class.
Code for each below:
import java.util.ArrayList;
public class Roster {
Student studObj = new Student(students);
static String[] students = {"1,John,Smith,John1989@gmail.com,20,88,79,59","2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
"3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87","4,Erin,Black,Erin.black@comcast.net,22,91,98,82",
"5,Joshua,Selvidge,jselvidge@wgu.edu,26,79,88,91"};
static ArrayList<Student> studentlist = new ArrayList<Student>();
public static void main(String[] args){
for (int i = 0; i < students.length; i++){
String[] parts = students[i].split(",");
int age = Integer.parseInt(parts[4]);
int grade1 = Integer.parseInt(parts[5]);
int grade2 = Integer.parseInt(parts[6]);
int grade3 = Integer.parseInt(parts[7]);
String studentID = parts[0];
String firstname = parts[1];
String lastname = parts[2];
String email = parts[3];
add(studentID,firstname,lastname,email,age,grade1,grade2,grade3);
}
//Part C.1
print_all();
print_invalid_emails();
print_average_grade();
remove("3");
remove("3");
print_all();
}
//Part B.3.a
public static void add(String studentID, String firstname, String lastname, String email, int age, int grade1, int grade2, int grade3){
Student newStudent = new Student(studentID,firstname,lastname,email,age,grade1,grade2,grade3);
studentlist.add(newStudent);
}
//Part B.3.b
public static void remove(String studentID) {
int i = Integer.parseInt(studentID);
if(studentlist.get(i-=1).getStudentID().contains(studentID)){
studentlist.remove(i);
return;
}
else{
System.out.println("Student ID " + studentID + " not found!");
}
// }
}
//Part B.3.c
public static void print_all(){
System.out.print(Student.getStudentID());
System.out.println("Student ID\tRecent Grades\tName\t\tE-Mail\t\t\tAge");
for (Student w : studentlist) {
w.print();
}
}
//Part B.3.d
public static void print_average_grade(){
System.out.println("Average grades");
System.out.println("Student ID\tName\tAverage");
for (int i = 0; i < students.length; i++){
String[] parts = students[i].split(",");
String studentID = parts[0];
int grade1 = Integer.parseInt(parts[5]);
int grade2 = Integer.parseInt(parts[6]);
int grade3 = Integer.parseInt(parts[7]);
int average = (grade1+grade2+grade3)/3;
System.out.println(studentID + "\t" + parts[1] + " " + parts[2] + "\t" + average);
}
}
//Part B.3.e
public static void print_invalid_emails() {
for(String w : students){
if(!w.contains("@") || !w.contains(".")){
System.out.println("Email address error in record: Student ID " + w.charAt(0));
}
}
}
Student Class:
import java.util.Arrays;
public class Student {
//B1 - instance variables for each student info
private String studentID;
private int[] grades;
private String firstname;
private String lastname;
private String email;
private int age;
//B2c constructor using all of the input parameters
public Student(String studentID, String firstname, String lastname, String email, int age, int grade1, int grade2, int grade3){
super();
this.studentID = studentID;
int[] grades = {grade1,grade2,grade3};
this.grades = grades;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.age = age;
}
public String toString() {
return studentID + "\t\t" + Arrays.toString(grades) + "\t" + firstname + " " + lastname + "\t" + email + "\t" + age;
}
//B.2.a-b getters and setter methods for each instance variable in B1
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public int[] getGrades() {
return grades;
}
public void setGrades(int[] grades) {
this.grades = grades;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void print(){
System.out.println(toString());
}
}
}
So the part I'm stuck on is the print_all(); method in the Roster class:
public static void print_all(){
System.out.print(Student.getStudentID());
System.out.println("Student ID\tRecent Grades\tName\t\tE-Mail\t\t\tAge");
for (Student w : studentlist) {
w.print();
}
}
using the enhanced for loop works perfectly fine to output all of the student information in a nice tab separated format, but what I am being asked to do is to print all of the data using the print(); method and getters (accessors) from the Student class, which is why I have added the
System.out.print(Student.getStudentID());
Obviously as it is it does not work. The error I get is
Cannot make a static reference to the non-static method getStudentID(); from the type Student.
I've looked this error up and lots of the answers had to do with accessing objects or methods within the same class, but I have not had success finding out how to access the method from another class.
Effectively what I must do is use print(Student.getStudentID();Student.getAge();Student.getGrades();Student.getName())
in order to print everything out. However, using the get methods are only giving me errors. I tried to change the methods in Student class to static but then they were giving errors about returning and being void.
Pretty stuck here.
Do I need to change my add() method to use setters and then try to access them from the print_all() method? If so, how is the info stored for access by another method to print?
Here is the output as it should look, with the print(); method using accessors from Student{} class:
Student ID Recent Grades Name E-Mail Age
1 [88, 79, 59] John Smith John1989@gmail.com 20
2 [91, 72, 85] Suzan Erickson Erickson_1990@gmailcom 19
3 [85, 84, 87] Jack Napoli The_lawyer99yahoo.com 19
4 [91, 98, 82] Erin Black Erin.black@comcast.net 22
5 [79, 88, 91] Joshua Selvidge jselvidge@wgu.edu 26
Email address error in record: Student ID 2
Email address error in record: Student ID 3
Average grades
Student ID Name Average
1 John Smith 75
2 Suzan Erickson 82
3 Jack Napoli 85
4 Erin Black 90
5 Joshua Selvidge 86
Student ID 3 not found!
Student ID Recent Grades Name E-Mail Age
1 [88, 79, 59] John Smith John1989@gmail.com 20
2 [91, 72, 85] Suzan Erickson Erickson_1990@gmailcom 19
4 [91, 98, 82] Erin Black Erin.black@comcast.net 22
5 [79, 88, 91] Joshua Selvidge jselvidge@wgu.edu 26