given list of students records.Write a program with signature as below:
Method Name: getStudentsWithMarksmoreThan()
Input : Marks - integer
output: List of students having marks more than input.
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.assertTrue;
public class Main {
public static class Student
{
private String name;
private double mark;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
}
@Override
public String toString() {
return name + ", " + mark;
}
}
public static ArrayList<Student> getStudentsWithMarksMoreThan(ArrayList<Student> students, double mark)
{
ArrayList<Student> filteredStudents = new ArrayList<>();
for(Student student: students)
{
if(student.getMark()>mark)
filteredStudents.add(student);
}
return filteredStudents;
}
@Test
public static void positiveTestCase()
{
ArrayList<Student> students = new ArrayList<>();
for(int i = 0; i < 10; i++)
{
Student student = new Student();
student.setName("Student "+ (i + 1));
student.setMark(10*(i+1));
students.add(student);
}
ArrayList<Student> filteredStudents = getStudentsWithMarksMoreThan(students, 10);
assertTrue(filteredStudents.size() == 9);
System.out.println("Test case is positive");
}
@Test
public static void negativeTestCase()
{
ArrayList<Student> students = new ArrayList<>();
for(int i = 0; i < 10; i++)
{
Student student = new Student();
student.setName("Student "+ (i + 1));
student.setMark(10*(i+1));
students.add(student);
}
ArrayList<Student> filteredStudents = getStudentsWithMarksMoreThan(students, 50);
assertTrue(filteredStudents.size() == 4);
}
public static void main(String[] args)
{
positiveTestCase();
negativeTestCase();
}
}
Comments
Leave a comment