LAB09, Array of objects – Binary Search, Sequential Search Points: 25 OBJECTIVE
ID: 3832388 • Letter: L
Question
LAB09, Array of objects – Binary Search, Sequential Search Points: 25
OBJECTIVE
To demonstrate using parallel arrays
To demonstrate using a binary search through arrays
To demonstrate using a sequential search through arrays
REQUIRED
A report of your work, in a flat pocket folder, in the following order:
1. Grade Form
2. This program description
3. The source listing of the Student class Student.java
4. Problem Analysis with Input, Output, Pseudocode (word processed)
5. The listing of the source program Lab09.java
Data dictionary (included as comments)
6. Listing of the input file Lab09StudentFile.txt
7. Listing of the student name file Lab09Names.txt
8. Listing of the expected results (already created) Lab09ExpectedResults.xlsx
9. Listing of the output file Lab09Report.txt
SPECIFICATIONS
Start with Lab08
1. Instead of doing a sequential search through the names file, load the file into two parallel arrays. Parallel arrays are arrays that are linked positionally. For this application, create an array for the student number and an array for the student name. This should be done in main.
2. Do a binary search to locate the student name. Use a method for the binary search. See Java13BinarySearch and Java13BinarySearchTrace.
3. Set up the letter grade and low range in parallel arrays. Create an array for the low end of the range and an array for the letter grade. This should be done in main
4. Convert the average to a letter grade.
Use a method and a sequential search to select the letter grade.
Instead of doing an “==” comparison, use a “>=” comparison on the low end of the grade range.
FOR EXAMPLE:
First time through the search loop, if average is >= low (97), then grade is an ‘A+’
If not an ’A+’, then average MUST be 96 or less, so if average >= low (93), then grade is an ‘A’
If not an ’A’, then average MUST be 92 or less, so if average >= low (90), then grade is an ‘A-’
Etc.
Exit the search when the correct grade is found or when the search is over
Grade low - high
A+ 97%-100%
A 93%-96%
A- 90%-92%
B+ 87%- 89%
B 83%- 86%
B- 80%- 82%
C+ 77%- 79%
C 73%- 76%
C- 70%- 72%
D+ 67%- 69%
D 63%- 66%
D- 60% - 62%
F 00%- 59%
INPUT
File: Student file Lab09StudentFile.txt
Record: Student record
Field Data Type
Student id# 4 numbers (ex. 1234)
Ten test scores integers (valid numbers are 0 -100)
INPUT
File: Student name file Lab09Names.txt
Record: Student name record
Field Data Type
Student id# 4 numbers (ex. 1234)
Student name String
OUTPUT
File: Grade Report file LAB09Report.txt
Record:
Student Grade Report, sorted by Name
ID# Name /---------------------TEST Scores----------------------/ Total Adj Total Avg Grade
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx xx
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx xx
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx xx
Total students = xx
Here's a link to the completed Lab08 which this builds upon.--> https://pastebin.com/UXT6XHTw
Here's the link to the completed Student Class file.--> https://pastebin.com/DvZvKq2M
Here's a link to the Lab09Names file.-->https://pastebin.com/hVsq0Pwk
Explanation / Answer
Hi, I have implemented the required changes.
Please let me know in case of any issue.
//Lab09
//this makes available all extra utilities from Java library including scanner
import java.util.*;
//this makes available all extras from Java library needed for files
import java.io.*; //needed for files
public class Lab09
{
// FUNCTION TO SORT StudentSort array
public static void selectionSort(StudentSort[] sortStudent)
{
int n = sortStudent.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (sortStudent[j].getStudentName().compareTo(sortStudent[min_idx].getStudentName()) < 0)
min_idx = j;
// Swap the found minimum element with the first
// element
StudentSort temp = sortStudent[min_idx];
sortStudent[min_idx] = sortStudent[i];
sortStudent[i] = temp;
}
}
public static void main(String[] args) throws FileNotFoundException
{//start of main method
//declare a variable of type Scanner
Scanner infileName = new Scanner(new FileReader("Lab06StudentFile.txt"));
PrintWriter outfileName = new PrintWriter ("Lab06Report.txt");
//variables and defined constants
int stdId; //student id number
int[] scores = new int[10]; //test scores
int x = 0; //variable for a single instance of the student class
int student = 0; //student variable initialized to zero
Student[] arrayStudent = new Student[16]; //array of student class objects
StudentSort[] sortStudent = new StudentSort[16];
// arrays to store their ids and names
int[] ids = new int[20];
String[] names = new String[20];
//create an instance of printwriter object
outfileName.println("Student Grade Report");
outfileName.println();
outfileName.println("ID# Name /-------------------Test Scores----------------/ Total AdjTotal Avg");
outfileName.println();
//read score until end of file
while(infileName.hasNext())
{//start while loop
stdId = infileName.nextInt();
for (int i = 0; i < scores.length; i++)
{
scores[i] = infileName.nextInt();
}
Student std = new Student(stdId, scores);
arrayStudent[x] = std;
x++;
student++;
}//end while
// loading name and id
int c = 0;
Scanner nameFileScanner = new Scanner (new FileReader("Lab06Names.txt"));
while (nameFileScanner.hasNext())
{//start while loop
int id = infileName.nextInt();
String name = infileName.nextLine().trim();
ids[c] = id;
names[c] = name;
c++;
}//end while
nameFileScanner.close();
//Callig sort method
selectionSort(sortStudent);
for (int j = 0; j < arrayStudent.length; j++)
{//start for loop
Student std = arrayStudent[j];
outfileName.printf("%-5d", std.getStudentId());
scores = std.getStudentGradeArray();
outfileName.printf("%-20s", binarySearch(ids, names,std.getStudentId()));
for (int i = 0; i < scores.length; i++)
{
outfileName.printf("%-5d", scores[i]);
}
//write total, ajdusted total and adjusted average
outfileName.printf("%8.0f%8.0f%8.0f",
std.getTotal(),
std.getAdjustedtotal(),
std.getAdjustedAverage());
outfileName.println();
}//end for loop
outfileName.println();
outfileName.printf ("Total "
+ "Students = %2d", student);
infileName.close();
outfileName.close();
}//end of main
public static String binarySearch(int[] ids, String[] names, int id) {
int start = 0;
int end = ids.length - 1;
String returnName = "name not found";
while (start <= end) {
int mid = (start + end) / 2;
if (id == ids[mid]) {
return names[mid];
}
if (id < ids[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return returnName;
}
}//end of class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.