2

I'm doing some past exam papers in preparation for a forthcoming exam, and have come across this question, and I'm not sure how to solve it, I've written a for-each loop with an if statement but I'm not sure what the header would be and what to return. Any help would be great. Thanks.

The question:

An ArrayList named classList is used to store Student objects.

Write code for a method that determines whether or not data for a student with given forename and surname is present in classList.

3
  • 1
    The question states what you need to provide as input and what the output would be. I don't see what your doubt is. Commented Jun 2, 2011 at 21:02
  • 2
    If you try showing the for-loop you've done so far, and what the Student class looks like, then you'll have more luck in getting an answer that helps you. Commented Jun 2, 2011 at 21:02
  • What do you mean by "header"? As for return type: what kind of answer is your function supposed to give to the question "is the student [first name] [last name] in the list?" -- "yes" or "no" right? booleans are great for that. Commented Jun 2, 2011 at 21:04

3 Answers 3

6

Something like:

boolean lala( Student given ) {
  for( Student s : classList ) {
    if( s.getForename().equals( given.getForename() ) &&
        s.getSurname().equals( given.getSurname() ) ) {
      return true;
    }
  }

  return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok I had something like that.. just wasn't sure on the header/return. Thanks very much Mike!
2
static boolean isPresentInList(List<Student> students, String forename, String surname)
{
     // loop thru students and check forename and surname and if there is a match, 
     // return true
     // outside of loop return false (no match).

     ...
}

Comments

1

Something like this?

private static ArrayList<Student> findStudents(ArrayList<Student> students, Student student) {
    ArrayList<Student> result = new ArrayList<Student>();
    for (Student obj: students) {
        if (obj.getFirstname().equals(student.getFirstname()) &&
            obj.getLastname().equals(student.getLastname())) {
            result.add(obj);
        }
    }
    return result;
}

class Student {
    private String mFirstname;
    private String mLastname;

    public Student() {

    }

    public Student(String fistName, String lastName) {
        mFirstname = fistName;
        mLastname = lastName;
    }

    public String getFirstname() {
        return mFirstname;
    }

    public void setFirstname(String mFirstname) {
        this.mFirstname = mFirstname;
    }

    public String getLastname() {
        return mLastname;
    }

    public void setLastname(String mLastName) {
        this.mLastname = mLastName;
    }
}

public class Main {
    public static void main(String[] args) {
        ArrayList<Student> studentList = new ArrayList<Student>();
        studentList.add(new Student("eder", "quiñones"));
        studentList.add(new Student("eder", "orteag"));
        studentList.add(new Student("lucia", "germes"));
        studentList.add(new Student("chespirito", "gomez"));

        System.out.println(findAllStudentsByName(studentList, "eder"));
        System.out.println(findAllStudentsByName(studentList, "lucia"));
        System.out.println(findAllStudentsByLastName(studentList, "germes"));
        System.out.println(findStudents(studentList, new Student("eder", "quiñones")));
    }

    private static ArrayList<Student> findAllStudentsByName(ArrayList<Student> students, String firstName) {
        ArrayList<Student> result = new ArrayList<Student>();
        for (Student obj: students) {
            if (obj.getFirstname().equals(firstName)) {
                result.add(obj);
            }
        }
        return result;
    }

    private static ArrayList<Student> findAllStudentsByLastName(ArrayList<Student> students, String lastName) {
        ArrayList<Student> result = new ArrayList<Student>();
        for (Student obj: students) {
            if (obj.getLastname().equals(lastName)) {
                result.add(obj);
            }
        }
        return result;        
    }
}

5 Comments

Returning an Array of objects just in case that we have more than one student with the same firstname or lastname.
Your findAllStudentsByLastName() is never called. Also you probably want to do it all in one function otherwise you have to go over the whole array twice.
@Mike you're right the function is never called!, but it works!
If it's never called, it shouldn't be written.
@dantuch, it's just an example. I just paste it, just in case that he wanted to use it or not, but it's 100% compilable and I believe that's what matters!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.