Commit 601e4709 by Paktalin

created method getAverageGpaForPartTimeStudents + language tests

parent 58c4f184
...@@ -42,6 +42,19 @@ public abstract class Session implements Comparable<Session>{ ...@@ -42,6 +42,19 @@ public abstract class Session implements Comparable<Session>{
student.addCredits(numberOfCredits); 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() { Date getEndDate() {
GregorianCalendar calendar = new GregorianCalendar(); GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(getStartDate()); calendar.setTime(getStartDate());
...@@ -59,22 +72,23 @@ public abstract class Session implements Comparable<Session>{ ...@@ -59,22 +72,23 @@ public abstract class Session implements Comparable<Session>{
return students; return students;
} }
protected abstract int getSessionLength();
Date getStartDate() { Date getStartDate() {
return startDate; return startDate;
} }
int getNumberOfStudents() { int getNumberOfStudents() {
return students.size(); return students.size();
} }
String getDepartment() { String getDepartment() {
return department; return department;
} }
String getNumber() { String getNumber() {
return number; return number;
} }
Student get(int index) { Student get(int index) {
return students.get(index); return students.get(index);
} }
} }
...@@ -65,4 +65,30 @@ abstract public class SessionTest extends TestCase { ...@@ -65,4 +65,30 @@ abstract public class SessionTest extends TestCase {
Session session = createSession("ENGL", "200", startDate); Session session = createSession("ENGL", "200", startDate);
assertEquals(getExpectedEndDate(), session.getEndDate()); 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 { ...@@ -31,13 +31,50 @@ public class LoopTest extends TestCase {
assertFalse(isPalindrome("abbaa")); 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){ private boolean isPalindrome(String string){
int limit = string.length() / 2;
for for
(int forward = 0, backward = string.length() - 1; (int forward = 0, backward = string.length() - 1;
forward < string.length(); forward < limit;
forward++, backward--) forward++, backward--)
if (string.charAt(forward) != string.charAt(backward)) if (string.charAt(forward) != string.charAt(backward))
return false; return false;
return true; 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