Java program by just using import java.uitl.Scanner Write a program that impleme
ID: 3776054 • Letter: J
Question
Java program by just using import java.uitl.Scanner Write a program that implements parallel array. Your program name should be . The program should use two arrays stuName and stuGpa of type String and double respectively. In the main method, define the length or size of both arrays as SIZE_ARR of 30. Read the student name and gpa from the input file using sentinel loop. You need to create two methods 1. public static int seqSearch(int numRecords, String[] stuName, double[] stuGpa, String nameKey, double gpaKey) { // Write the sequential search that compares both name, and gpa. // Only when both are correct return that index otherwise return -1. // this index will be used to find out position of the key in the array in the main method. } 2. public static void printData(int numRecords, String[] stuName, double[] stuGpa) { // Write a loop that prints out all the record you read in. // Use the format shown in the sample output. } Here the first method performs the sequential search to find a record and the second record prints the result. You need to pass two arrays in both methods as arguments. In method 1, nameKey is the search key for the student and stuGpa is the search key for that student's GPA. If both the student name and gpa matches, then only it will return the index of array. If not, it will return -1. In method 2, you need to print the record as shown in sample output. Your input file should look like Tom 4.0 Bob 3.2 Jack 2.5 Jill 4.0 Carry 3.5 Kylie 3.2 EndData Jenny 2.4 Kylie 3.2 Jack 4.0 Bob 3.2 EndData Here, represents the sentinel value. The input values before first is student records and the input values after the first are search keys. For the sample input, your output should look like: The name of the student in location 0 is Tom and has a gpa of 4.0. The name of the student in location 1 is Bob and has a gpa of 3.2. The name of the student in location 2 is Jack and has a gpa of 2.5. The name of the student in location 3 is Jill and has a gpa of 4.0. The name of the student in location 4 is Carry and has a gpa of 3.5. The name of the student in location 5 is Kylie and has a gpa of 3.2. The student: Jenny with a gpa of 2.4 was not found in the array. The student: Kylie with a gpa of 3.2 was found in the array at position 5. The student: Jack with a gpa of 4.0 was not found in the array. The student: Bob with a gpa of 3.2 was found in the array at position 1.
Explanation / Answer
// program about maintaining student records and searching. beginner level
import java.util.Scanner;
import java.io.File;
public class StuRec{
static final int SIZE_ARR = 30; // static variable to be accessble to all static methods.
static String[] stuName = new String[SIZE_ARR];
static double[] stuGpa = new double[SIZE_ARR];
public static void main(String args[]) throws Exception{
Scanner sc = new Scanner(System.in);
// format is not specified by the questioner so enter filename including extension
System.out.println("Enter the file name with extention:-");
File file = new File(sc.next());
// linking scanner to file.
sc = new Scanner(file);
int numRecords = 0;
while(sc.hasNextLine()){ // reading file
String line = sc.nextLine();
String[] parts = line.split(" "); //split based on setinel value " ";
if(parts[0].equals("EndData")){ // if it is first EndData break the loop
break;
}
stuName[numRecords] = parts[0]; // storing data into arrays
stuGpa[numRecords] = Double.parseDouble(parts[1]);
numRecords++;
}
String nameKey;
double gpaKey;
int result;
printData(numRecords,stuName,stuGpa); // calling printData function
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] parts = line.split(" ");
if(parts[0].equals("EndData")){
break;
}
nameKey = parts[0]; // taking values to search
gpaKey = Double.parseDouble(parts[1]);
result = seqSearch(numRecords,stuName,stuGpa,nameKey,gpaKey); // calling seqSearch to get result
if(result == -1){ // printing output based on result
System.out.println("The student: "+parts[0]+" with a gpa of "+parts[1]+" was not found in the array.");
}
else{
System.out.println("The student: "+parts[0]+" with a gpa of "+parts[1]+" was found in the array at position "+result+".");
}
}
sc.close();
}
public static int seqSearch(int numRecords, String[] stuName, double[] stuGpa,String nameKey,double gpaKey){
for(int i=0;i<numRecords;i++){
if(stuName[i].equals(nameKey) && stuGpa[i]==gpaKey){ // both values matched with given values
return i; // return index
}
}
return -1; //not found
}
public static void printData(int numRecords,String[] stuName,double[] stuGpa){
for(int i=0;i<numRecords;i++){ // printing data one by one.
System.out.println("The name of the student in location "+i+" is "+stuName[i]+" and has a gpa of "+stuGpa[i]+".");
}
}
}
// end of program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.