Problem Statement
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 If possible add Unit Test case – both positive and negative.
You can do in choice of your language (Any high level language like – PHP, JavaScript, Angular).
For example JSON given below of 3 students for your reference.
{
"students":[
{
"roll_no":101,
"details":{
"name":"ajay",
"marks":42,
"age":20
}
},
{
"roll_no":102,
"details":{
"name":"amit",
"marks":45,
"age":21
}
},
{
"roll_no":111,
"details":{
"name":"ramesn",
"marks":31,
"age":21
} } ] }
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