Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have two arrays with one containing UC students and the other containing SMC

ID: 3541713 • Letter: Y

Question

You have two arrays with one containing UC students and the other containing SMC alumni, you plan to process the arrays and find the students who belong to both arrays. Your data structures are these two arrays. Your algorithm will be: for each student in the UC students array you try to find it in the SMC students array by comparing it for equality (based on ID) with every student in the SMC students array. If you do find it, that UC student is then stored in an array containing students who are SMC alumni attending UC schools. Once you have the array of the common students, you sort it by ID and print it to a file. We will use the Java library sort for this. From the incomplete code below please write the code to complete the program. It includes

readStudentsFromFile

writeStudentsToFile   

findCommonStudents1

findCommonStudents2

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;



public class SMCAlumniFinder {

      /**
       * Read in a file containing student records and create an array of Student
       * objects
       *
       * @param file The filename
       * @param num The number of students in the file
       * @return An array of Student objects
       */
      static Student[] readStudentsFromFile(String filename, int num) {
            try (Scanner in = new Scanner(new File(filename))) {
                  Student[] students = new Student[num];
                  
                  // YOUR CODE HERE
                  // Hints:
                  // To read a line from the file you can call
                  //          String line = in.nextLine();
                  // The split method of the String class might come in handy to get the fields
                  
                  return students;
            } catch (FileNotFoundException e) {
                  e.printStackTrace();
                  return null;
            }
      }

      /**
       * Write an array of Student objects to a file
       *
       * @param students The array of Student objects to write out
       * @param filename The output filename
       */
      static void writeStudentsToFile(Student[] students, String filename) {
            try (FileWriter out = new FileWriter(filename)) {
                  
                  // YOUR CODE HERE
                  // Hints:
                  // To write a line to the file you can call
                  //             out.write("Hello World!" + " ");
                  
            } catch (IOException e) {
                  e.printStackTrace();
            }
      }

      /**
       * Find students belonging to both groups.
       *
       * This function checks each student in group1 for membership in group2 by
       * comparing it with every student in group2.
       *
       * @param group1 A group of Students
       * @param group2 A group of Students
       * @param numCommon number of students belonging to both groups
       * @return An array of Students which belong to both group1 and group2
       */
      static Student[] findCommonStudents1(Student[] group1, Student[] group2,
                  int numCommon) {
            Student[] common = new Student[numCommon];

            // YOUR CODE HERE

            return common;
      }

      /**
       * Find students belonging to both groups.
       *
       * This function checks each student in group1 for membership in group2 by
       * doing a binary search on group2.
       *
       * @param group1 A group of Students
       * @param group2 A group of Students
       * @param numCommon number of students belonging to both groups
       * @return An array of Students which belong to both group1 and group2
       */
      static Student[] findCommonStudents2(Student[] group1, Student[] group2,
                  int numCommon) {
            Student[] common = new Student[numCommon];
            
            // YOUR CODE HERE
            // Hints:
            // To sort an array of Comparables you can call    
            //          Arrays.sort(arr);
            // To search for an element in a sorted array you can call
            //          Arrays.binarySearch(arr, element)
            // if the returned index is >= 0, the element was found at that index

            return common;
      }

      /**
       * @param args
       */
      public static void main(String[] args) {
            
            /***** These files provided to help you with initial testing *****/
            Student[] uc = readStudentsFromFile("sample_uc_students.txt", 10);
            Student[] smc = readStudentsFromFile("sample_smc_grads.txt", 5);
            final int SMC_UC_GradsNumber = 2;
            
            /***** Use these files for the output you submit *****/
//          Student[] uc = readStudentsFromFile("uc_students.txt", 350000);
//          Student[] smc = readStudentsFromFile("smc_grads.txt", 75000);
//          final int SMC_UC_GradsNumber = 25000;

            long start, end;

            start = System.currentTimeMillis();
            Student[] common1 = findCommonStudents1(uc, smc, SMC_UC_GradsNumber);
            end = System.currentTimeMillis();
            System.out.println("Cross checking took " + (end - start) / 1000.0
                        + " seconds.");
            Arrays.sort(common1);
            writeStudentsToFile(common1, "smc_grads_at_uc_1.txt");

            start = System.currentTimeMillis();
            Student[] common2 = findCommonStudents2(uc, smc, SMC_UC_GradsNumber);
            end = System.currentTimeMillis();
            System.out.println("Using binary search it took " + (end - start)
                        / 1000.0 + " seconds.");
            Arrays.sort(common2);
            writeStudentsToFile(common2, "smc_grads_at_uc_2.txt");
            
      }

}

   

You have two arrays with one containing UC students and the other containing SMC alumni, you plan to process the arrays and find the students who belong to both arrays. Your data structures are these two arrays. Your algorithm will be: for each student in the UC students array you try to find it in the SMC students array by comparing it for equality (based on ID) with every student in the SMC students array. If you do find it, that UC student is then stored in an array containing students who are SMC alumni attending UC schools. Once you have the array of the common students, you sort it by ID and print it to a file. We will use the Java library sort for this. From the incomplete code below please write the code to complete the program. It includes

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Arrays;

import java.util.Scanner;





class Student implements Comparable<Student>

{

public int id;

public String name;

public String school;

public Student()

{

id=0;

name=null;

school=null;

}

public int getid()

{

return this.id;

}

public String getname()

{

return this.name;

}

public String getschool()

{

return this.school;

}

public void setid(int id)

{

this.id=id;

}

public void setname(String name)

{

this.name=name;

}

public void setschool(String s)

{

this.school=s;

}


@Override

public int compareTo(Student arg0)

{

if((this==null)||(arg0==null))

{

return -1;

}

else if(this.id!=arg0.id)

{

return -1;

}

// TODO Auto-generated method stub

return 0;

}

}


class Mukesh

{


static Student[] sortArray(Student s[])

{

for(int i=0;i<s.length;i++)

{

for(int j=0;j<s.length;j++)

{

if((s[i]==null)||(s[j]==null))

{

break;

}

else

{

if(s[i].id<s[j].id)

{

Student temp=new Student();

temp=s[i];

s[i]=s[j];

s[j]=temp;

}

}

}

}

return s;

}

static Student[] readStudentsFromFile(String filename, int num)

{

try (Scanner in = new Scanner(new File(filename)))

{

Student[] students = new Student[num];

int i=0;


String[] arr=new String[3];

while(in.hasNext())

{

if(i>num-1)

{

break;

}

String line=in.nextLine();

arr=line.split(" ",3);

students[i]=new Student();

students[i].id=Integer.parseInt(arr[0]);

students[i].name=arr[1];

students[i].school=arr[2];

i++;

}

in.close();

return students;

}

catch (FileNotFoundException e)

{

e.printStackTrace();

return null;

}

}


/**

* Write an array of Student objects to a file

*

* @param students The array of Student objects to write out

* @param filename The output filename

*/

static void writeStudentsToFile(Student[] students, String filename)

{

try (FileWriter out = new FileWriter(filename))

{

for(int i=0;i<students.length;i++)

{

if(students[i]==null)

{

break;

}

out.write(students[i].id+" "+students[i].name+" "+students[i].school+" ");

}

out.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}


static Student[] findCommonStudents1(Student[] group1, Student[] group2,int numCommon)

{

Student[] common = new Student[numCommon];

int k=0;

for(int i=0;i<group1.length;i++)

{

for(int j=0;j<group2.length;j++)

{

if(group1[i].id==group2[j].id)

{

common[k]=new Student();

common[k]=group1[i];

k++;

break;

}

if(k>=numCommon-1)

{

break;

}

}

if(k>=numCommon-1)

{

break;

}

}

return common;

}


/**

* Find students belonging to both groups.

*

* This function checks each student in group1 for membership in group2 by

* doing a binary search on group2.

*

* @param group1 A group of Students

* @param group2 A group of Students

* @param numCommon number of students belonging to both groups

* @return An array of Students which belong to both group1 and group2

*/

static Student[] findCommonStudents2(Student[] group1, Student[] group2,int numCommon)

{

Student[] common = new Student[numCommon];

sortArray(group1);

int k=0;

int n=0;

for(int i=0;i<group1.length;i++)

{

if(group1[n]==null)

{

break;

}

n++;

}

Student[] grp=new Student[n];

for(int i=0;i<n;i++)

{

grp[i]=group1[i];

}

for(int i=0;i<group2.length;i++)

{

int check= Arrays.binarySearch(grp, group2[i]);

if(check >=0)

{

common[k]=group2[i];

k++;

if(k>numCommon-1)

{

break;

}

}

}

return common;

}


/**

* @param args

*/

public static void main(String[] args)

{

/***** These files provided to help you with initial testing *****/

Student[] uc = readStudentsFromFile("sample_uc_students.txt", 10);

Student[] smc = readStudentsFromFile("sample_smc_grads.txt", 5);

final int SMC_UC_GradsNumber = 2;

/***** Use these files for the output you submit *****/

// Student[] uc = readStudentsFromFile("uc_students.txt", 350000);

// Student[] smc = readStudentsFromFile("smc_grads.txt", 75000);

// final int SMC_UC_GradsNumber = 25000;

long start, end;

start = System.currentTimeMillis();

Student[] common1 = findCommonStudents1(uc, smc, SMC_UC_GradsNumber);

end = System.currentTimeMillis();

System.out.println("Cross checking took " + (end - start) / 1000.0

+ " seconds.");

sortArray(common1);

writeStudentsToFile(common1, "smc_grads_at_uc_1.txt");

start = System.currentTimeMillis();

Student[] common2 = findCommonStudents2(uc, smc, SMC_UC_GradsNumber);

end = System.currentTimeMillis();

System.out.println("Using binary search it took " + (end - start)

/ 1000.0 + " seconds.");

sortArray(common2);

writeStudentsToFile(common2, "smc_grads_at_uc_2.txt");

}


}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote