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

JAVA Write a program that allows the user to enter and then search a list of stu

ID: 3731225 • Letter: J

Question

JAVA

Write a program that allows the user to enter and then search a list of student names and grades. Your program will execute in two phases. In the first phase, the user will enter student name and grade pairs. In the second phase, the user will search this data for matching parts of names, or matching grades. In the first phase, your program will prompt the user for the number of name and grade pairs. It will then call a method named readData(), to read the names and grades, and store these values in parallel arrays. The first array will be an array of Strings, and the second will be an array of char. Here is the signature of the method: public static void readData (String [] names, // student names char [] grades, // student grades int nStudents, // number of students Scanner in) // input

In the second phase, the user will enter a series of queries. There are two types of queries: • a name search indicated by the String “name” followed by another String representing part of a name, and • a grade search, indicated by the String “grade” followed by another String representing the grade. For each query, your program should print out all information for any student that matches that particular query. Both the name search and the grade search must be done in methods. The name search should be done in a method called executeNameSearch() with this signature: public static void executeNameSearch (String [] names, // student names char [] grades, // student grades String target) // the string // to search for The String method indexOf()will be useful when you write this method. It has the signature int indexOf (String s). It returns the position of the substring s in the String invoking the method, or -1 if s is not a substring of the String. The grade search should also be done in a method with a signature similar to executeNameSearch().

Explanation / Answer

Answer:

StudentGrades.java:

import java.util.Scanner;

public class StudentGrades {

// Variable declaration

public static int nStudents;

public static String[] studentNames;

public static char[] studentGrades;

public static String nameSearch;

public static String gradeSearch;

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("Please enter numer of students : ");

Scanner sc = new Scanner(System.in);

// read input for number of students

nStudents = sc.nextInt();

// Assigning the size of the arrays

studentNames = new String[nStudents];

studentGrades = new char[nStudents];

//invoking readData() to get the input from user for Student details

readData(studentNames, studentGrades, nStudents);

System.out.println(" Please enter the Student name to search : ");

// user input to search for the string in students Names

nameSearch = sc.next();

// Student name search function

executeNameSearch(studentNames, studentGrades, nameSearch);

System.out.println(" Please enter the Grade to search : ");

// user input to search for the string in students Grades

gradeSearch = sc.next();

//Student Grade search function

executeGradeSearch(studentNames, studentGrades, gradeSearch);

}

// read students details from user

public static void readData(String[] names, char[] grades,int nStudents){

for(int i = 0; i<nStudents; i++){

Scanner sc = new Scanner(System.in);

System.out.println("Please Enter the "+(i+1)+" Student Name :");

names[i] = sc.nextLine();

System.out.println("Please Enter the Grade : ");

grades[i] = sc.next().charAt(0);

}

}

// Display search results for student name search

public static void executeNameSearch (String [] names, char [] grades, String target){

System.out.println("The Search Results for : "+target);

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

if(names[i].indexOf(target) != -1){

System.out.println("Student Name : "+names[i]);

System.out.println("Grade : "+grades[i]);

}

}

}

//Display search results for student grade search

public static void executeGradeSearch (String [] names, char [] grades, String target){

System.out.println("The Search Results for : "+target.charAt(0));

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

if(grades[i] == target.charAt(0)){

System.out.println("Student Name : "+names[i]);

System.out.println("Grade : "+grades[i]);

}

}

}

}

Output:

Please enter numer of students :
5
Please Enter the 1 Student Name :
John will
Please Enter the Grade :
A
Please Enter the 2 Student Name :
John Carter
Please Enter the Grade :
B
Please Enter the 3 Student Name :
Will Smith
Please Enter the Grade :
A
Please Enter the 4 Student Name :
Shareef John
Please Enter the Grade :
C
Please Enter the 5 Student Name :
Racheal cart
Please Enter the Grade :
A

Please enter the Student name to search :
Jo
The Search Results for : Jo
Student Name : John will
Grade : A
Student Name : John Carter
Grade : B
Student Name : Shareef John
Grade : C

Please enter the Grade to search :
A
The Search Results for : A
Student Name : John will
Grade : A
Student Name : Will Smith
Grade : A
Student Name : Racheal cart
Grade : A