Question

In: Computer Science

/*    * Returns a new array of students containing only those students from the given...

/*
   * Returns a new array of students containing only those students from the given roster that belong
   * to the given academic program
   * The resulting array must not contain any null entries
   * Returns empty array (length == 0) if no students belong to the program
   */
   public static UStudent[] filterByProgram(String program, UStudent[] roster) {
       //YOUR CODEf
       int counter=0;
       for(int i=0;i<roster.length;i++) {
           if(roster[i].getAcademicProgram().equals(program)) {
               counter++;
               UStudent[] Studentbelongprogram = new UStudent[counter];
               Studentbelongprogram[i]=roster[i];
               roster=Studentbelongprogram;
           }else {
               return new UStudent[0];
           }


       }
       return roster;  
   }

Solutions

Expert Solution



Use the following code snippet to achieve the goal:

I have used Java Stream API to achieve this. Filtered out any null values and next filter will shortlist the matching elements.


public static UStudent[] filterByProgram(String program, UStudent[] roster) {
  

    UStudent[] result = Arrays.stream(roster)

        .filter(x -> x!=null)

        .filter(x -> x.getAcademicProgram().equals(program))

        .collect(Collectors.toList()).stream().toArray(UStudent[]::new);

return result;  

   }

==============================================================================================

Do not forget to add imports

==========================================================================================

import java.util.*;

import java.util.stream.Collectors;

=========================================================================================


Related Solutions

Java Write a method that removes duplicates from an array of strings and returns a new...
Java Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the function parameters (complete the area shown in ___________. ___________maxFun(____________________________) { }
Write a boolean function that is given a binary tree and returns true if and only...
Write a boolean function that is given a binary tree and returns true if and only if the tree has an odd number of nodes. An empty tree is considered to have an even number of nodes. Notes: The function should have just one argument, a pointer to the root. No global variables may be used. No additional functions may be defined. You may not count the number of nodes.
(c++)You will be given a data file containing data for 10 students. The format is as...
(c++)You will be given a data file containing data for 10 students. The format is as follows - grades are double precision numbers: Line 1: Header Information Line 2: Student Full name Line 3: Student ID Line 4: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Line 5: Student Full name Line 6: Student ID Line 7: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Line 8: Student Full name Line 9: Student ID Line 10: testgrade_1 testgrade_2 testgrade_3 testgrade_4 testgrade_5 Etc. Read the data into...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm? Java Language....
Suppose you are given an n-element array containing distinct integers that are listed in increasing order....
Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm?
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int i = 0;...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list. #include #include #include using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() { Student stuArr[NUM]; ifstream myfile; myfile.open("Test.txt"); for(int i = 0; i < NUM; i++)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT