Answer to Question #201654 in HTML/JavaScript Web Application for Nidhi

Question #201654

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 Also add Unit Test case – both positive and negative. You can do in choice of your language (Any high level language like – Java, Python, PHP, C#, JavaScript) 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 } } ] 


1
Expert's answer
2021-06-02T02:58:32-0400

I chose Java:

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();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS