Subject
public class Subject
{
private String name;
private int [] gradeArr;
private int numOfGrades;
public
Subject(String name) {
this.name = name;
gradeArr = new int [30];
numOfGrades = 0;
}
public String
getName() {
return name;
}
public void
setName(String name) {
this.name = name;
}
public boolean
addGrade(int grade) {
if (numOfGrades == gradeArr.length)
return false;
if (grade <
0 || grade >
100)
return false;
gradeArr[numOfGrades] = grade;
numOfGrades++;
return true;
}
}
Studetn
public class Student {
private String firstName;
private String lastName;
private Subject [] subArr;
private int numOfSubjects;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
numOfSubjects = 0;
subArr = new Subject[30];
}
public boolean addSubject(String sub) {
if (numOfSubjects == subArr.length)
return false;
if (checkSubjectName(sub)) {
return false;
}
subArr[numOfSubjects] = new Subject(sub);
numOfSubjects++;
return true;
}
private boolean checkSubjectName(String sub) {
for (int i=0;i<numOfSubjects;i++) {
if (subArr[i].getName().equals(sub)) {
return true;
}
}
return false;
}
public boolean addGrade(String subName,int grade) {
int idx =
findSubjectIndex(subName);
if (idx == -1)
return false;
return (subArr[idx].addGrade(grade));
}
private int findSubjectIndex(String subName) {
for (int i=0;i<numOfSubjects;i++) {
if (subArr[i].getName().equals(subName)) {
return
i;
}
}
return -1;
}
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;
}
}