Another day another assignment. For this assignment I need to read in values into an arraylist and then output things such as
Students Passed: value here
Students Failed: value here
Top Student: string here
Average: value here.
So far i've gotten Top Student and the average working but the Students passed/failed aren't working and I can't figure it out. I've tested certain things like extracting the marks from the arraylist seperately so I can check if my IF statement is using the right conditions which it is. Anyway here it is:
import java.util.ArrayList; class Course { private ArrayList<Student> people = new ArrayList<Student>(); //Add a students mark public void add( Student s ){ people.add( s ); } //Return the number of students who passed (mark>= 40) public int pass(){ int size = people.size(); int pass = 0; for ( int i = 0; i < size; i++ ) { int test = people.get(i).getMark(); if( test >= 40 ); { pass++; } } return pass; } //Return the number of students who failed (Mark < 40 ) public int fail(){ int size = people.size(); int fail = 0; for ( int i = 0; i < size; i++ ) { int test = people.get(i).getMark(); if( test < 40); { fail++; } } return fail; } //Return the name of the student with the top mark public String top(){ int highest = 0; int k; for( k = 0; k < people.size();k++){ if (people.get(k).getMark() > highest ){ highest = k; } } return people.get(highest).getName(); } //Return the average mark public double average(){ double sum = 0.0; double size = people.size(); for ( int i = 0; i<people.size(); i++){ sum = sum + people.get(i).getMark(); } double total = sum / size; return total; } }
The problem is that the students that are passing/failing always seem to be the number of students that I have entered instead of the students that achieved a mark within the pass/fail criteria.
Heres what I get on output:
#Enter student name: Einstein
#Enter mark : 15
#Enter student name: Karl Pilkington
#Enter mark : 99
#Enter student name: END
Students passing = 2 //WHY?
Students failing = 2 //WHY?
Top student = Karl Pilkington
Average mark = 57.00