Answer to Question #190695 in Java | JSP | JSF for Tanaya

Question #190695

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.(Any high level language like – Java,

Python, PHP, C#, JavaScript)


1
Expert's answer
2021-05-08T05:48:28-0400
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