Skip to content
Snippets Groups Projects
Commit b2867537 authored by Ethan Robert's avatar Ethan Robert
Browse files

Added Student and UseStudent classes

parent 6691db5b
Branches
No related tags found
No related merge requests found
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package tp04;
import java.util.Arrays;
/**
*
* @author ethan
*/
public class Student {
private Person pers;
private double[] grades;
private int realGradeLength() {
int count = 0;
for (int i = 0; i < this.grades.length; i++) {
if (this.grades[i] != -1.0) {
count++;
}
}
return count;
}
public Student(Person pers, double[] grades) {
this.pers = pers;
this.grades = grades;
}
public Student(String forename, String name, double[] grades) {
this.pers = new Person(forename, name);
this.grades = grades;
}
public Student(String forename, String name, double grade) {
this.pers = new Person(forename, name);
this.grades = new double[]{grade};
}
public String getName() {
return this.pers.getName();
}
public String getForename() {
return this.pers.getForename();
}
public void setName(String newName) {
this.pers.setName(newName);
}
public void setForename(String newForename) {
this.pers.setForename(newForename);
}
public int getID() {
return this.pers.getID();
}
public String toString() {
String gradesString = "[";
for (int i = 0; i < this.grades.length; i++) {
gradesString += this.grades[i];
if (i < this.grades.length - 1) {
gradesString += ", ";
}
}
gradesString += "]]";
return "Student[" + this.pers + " = " + gradesString;
}
public double[] getGrades() {
return this.grades;
}
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (other.getClass() == this.getClass()) {
Student otherStudent = (Student) other;
boolean arrayEquals = true;
double[] otherStudentGrades = otherStudent.getGrades();
if (otherStudentGrades.length == this.grades.length) {
for (int i = 0; i < this.grades.length; i++) {
arrayEquals = this.grades[i] != otherStudentGrades[i];
}
}
return otherStudent.getID() == this.pers.getID() && arrayEquals;
}
return false;
}
public double getAverage() {
double sum = 0.0;
for (int i = 0; i < this.grades.length; i++) {
if (this.grades[i] != -1.0) {
sum += this.grades[i];
}
}
return sum / this.realGradeLength();
}
public void addGrade(double grade) {
if (this.grades.length != this.realGradeLength()) {
for (int i = 0; i < this.grades.length; i++) {
if (this.grades[i] == -1.0) {
this.grades[i] = grade;
}
}
} else {
this.grades = Arrays.copyOf(this.grades, this.grades.length + 1);
this.grades[this.grades.length - 1] = grade;
}
}
}
package tp04;
public class UseStudent {
public static void main (String[] args) {
Person person1 = new Person("John", "Doe");
Student student1 = new Student(person1, new double[]{12.5, 14.0});
Student student2 = new Student(person1, new double[]{12.5, 14.5});
Student student3 = new Student("Jane", "Smith", 14.0);
System.out.println("student1 == student2: " + student1.equals(student2));
System.out.println("student1 == student3: " + student1.equals(student3));
System.out.println("student2 == student3: " + student2.equals(student3));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment