Arraylist pass/fail values problem/confused.
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:
Code :
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
Re: Arraylist pass/fail values problem/confused.
Ignore that, I solved it. I accidentally left some ;'s in :(.
Another problem tho, for some reason the method im using to get the name of the highest marking pupil isn't work and can't get my head around it as mathematically it should do the job.
Heres the code:
Code :
public String top(){
int highest = 0;
int k;
for( k = 0; k < people.size();k++){
int mark = people.get(k).getMark();
if ( mark > highest ){
highest = k;
}
}
return people.get(highest).getName();
}
heres the input data:
A N Other
51
B N Other
62
C N Other
70
D N Other
84
E N Other
75
F N Other
81
END
For some reason this outputs this:
#Enter student name: A N Other
#Enter mark : 51
#Enter student name: B N Other
#Enter mark : 62
#Enter student name: C N Other
#Enter mark : 70
#Enter student name: D N Other
#Enter mark : 84
#Enter student name: E N Other
#Enter mark : 75
#Enter student name: F N Other
#Enter mark : 81
#Enter student name: END
Students passing = 6
Students failing = 0
Top student = F N Other
Average mark = 70.50
When D N Other should be the highest scoring student.
Re: Arraylist pass/fail values problem/confused.
Mark is a score. Highest is an index. It doesn't make sense to compare them.
Re: Arraylist pass/fail values problem/confused.
Quote:
Originally Posted by
KevinWorkman
Mark is a score. Highest is an index. It doesn't make sense to compare them.
SOLVED, cheers for the help :)