Below are the 4 methods that I got confused, that 4 methods are in the Teacher class. I have a Student class and Teacher class. and in the Teacher class, declared is the ArrayList<Student> students
as instance variable.
How to explain the Student that I saw in the methods given below and it is also used as a parameter. I am very confused with the Student searchStudent
(in the method) and the Student student
(inside the argument). Is that for ArrayList
only? How to understand that concept where one class will search another class using the class name?
public Student searchStudent(Student student)
{
//confuses me
Student found = null;
if (this.students.contains(student))
{
int index = this.students.indexOf(student);
if (index != -1)
{
found = this.students.get(index);
}
}
return found;
}
public Student searchStudent(int id)
{
//confuses me
Student beingSearched = new Student();
beingSearched.setStudentId(id);
return this.searchStudent(beingSearched);
}
public boolean addStudent(Student student)
{
//confuses me
boolean added = false;
if (this.searchStudent(student) == null)
{
this.students.add(student);
added = true;
}
return added;
}
public boolean addStudent(int id, String name, double grade)
{
//this is fine as i know boolen and int, String and double//
Student student = new Student(id, name, grade);
return this.addStudent(student);
}
I advice you to go through this link about Defining Methods.
public Student searchStudent(Student student)
It's a public
method that returns an object of type Student
, it accepts an object of type Student
as well. It needs to accept the student
parameter because it search for it. You'll use this method when you want to search whether some student
exists in your records (in the student
ArrayList).
public Student searchStudent(int id)
The same, but the parameter it accepts is an int
. Here you'll search for the student
not by the object itself, but by the id of the student
.
public boolean addStudent(Student student)
It's a method that adds a student
(which is of type Student
) to students
ArrayList.
Tip: Run your code in Debug mode and follow each method you don't understand, you'll be amazed of how much this will help you to better understand the flow of the program.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments