Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Arraylist pass/fail values problem/confused.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Arraylist pass/fail values problem/confused.

    Mark is a score. Highest is an index. It doesn't make sense to compare them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist pass/fail values problem/confused.

    Quote Originally Posted by KevinWorkman View Post
    Mark is a score. Highest is an index. It doesn't make sense to compare them.
    SOLVED, cheers for the help

Similar Threads

  1. confused on what this problem is asking for (bitwise operators)
    By mmholdford in forum Java Theory & Questions
    Replies: 3
    Last Post: February 9th, 2012, 06:52 AM
  2. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM
  3. Replies: 3
    Last Post: May 21st, 2011, 08:47 AM
  4. HELP! Cant pass my input values to the paint component
    By mindmaster in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 22nd, 2011, 04:39 PM
  5. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 08:57 PM