Commit 601e4709 by Paktalin

created method getAverageGpaForPartTimeStudents + language tests

parent 58c4f184
......@@ -42,6 +42,19 @@ public abstract class Session implements Comparable<Session>{
student.addCredits(numberOfCredits);
}
double getAverageGpaForPartTimeStudents() {
double total = 0;
int counter = 0;
for (Student student : getAllStudents())
if (!student.isFullTime()) {
counter++;
total += student.getGpa();
}
return total / counter;
}
protected abstract int getSessionLength();
Date getEndDate() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(getStartDate());
......@@ -59,22 +72,23 @@ public abstract class Session implements Comparable<Session>{
return students;
}
protected abstract int getSessionLength();
Date getStartDate() {
return startDate;
}
int getNumberOfStudents() {
return students.size();
}
String getDepartment() {
return department;
}
String getNumber() {
return number;
}
Student get(int index) {
return students.get(index);
}
}
......@@ -65,4 +65,30 @@ abstract public class SessionTest extends TestCase {
Session session = createSession("ENGL", "200", startDate);
assertEquals(getExpectedEndDate(), session.getEndDate());
}
public void testAverageGpaForPartTimeStudents() {
session.enroll(createFullTimeStudent());
assertEquals(1, session.getNumberOfStudents());
createPartTimer("1", Student.Grade.A);
assertEquals(2, session.getNumberOfStudents());
createPartTimer("2", Student.Grade.D);
assertEquals(3, session.getNumberOfStudents());
assertEquals(2.5, session.getAverageGpaForPartTimeStudents(), 0.05);
}
private void createPartTimer(String name, Student.Grade grade) {
Student partTimer = new Student(name);
partTimer.addGrade(grade);
session.enroll(partTimer);
assertFalse(partTimer.isFullTime());
assertEquals(grade.getPoints(), partTimer.getGpa(), 0.05);
}
private Student createFullTimeStudent() {
Student student = new Student("a");
student.addCredits(Student.CREDITS_REQUIRED_FOR_FULL_TIME);
return student;
}
}
......@@ -31,13 +31,50 @@ public class LoopTest extends TestCase {
assertFalse(isPalindrome("abbaa"));
}
public void testFibonacci() {
assertEquals(0, fib(0));
assertEquals(1, fib(1));
assertEquals(1, fib(2));
assertEquals(2, fib(3));
assertEquals(3, fib(4));
assertEquals(5, fib(5));
}
private boolean isPalindrome(String string){
int limit = string.length() / 2;
for
(int forward = 0, backward = string.length() - 1;
forward < string.length();
forward < limit;
forward++, backward--)
if (string.charAt(forward) != string.charAt(backward))
return false;
return true;
}
private int fib(int x) {
if (x == 0)
return 0;
if (x == 1)
return 1;
return fib(x - 1) + fib(x - 2);
}
public void testEndTrim() {
assertEquals("", endTrim(""));
assertEquals(" x", endTrim(" x "));
assertEquals("y", endTrim("y"));
assertEquals("xaxa", endTrim("xaxa"));
assertEquals("", endTrim(" "));
assertEquals("xxx", endTrim("xxx "));
}
private String endTrim(String source) {
int i = source.length() - 1;
while (i >= 0) {
if (source.charAt(i) != ' ')
break;
i--;
}
return source.substring(0, i + 1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment