I have this lab I\'ve been trying to work on but I can\'t figure out how to comp
ID: 3830345 • Letter: I
Question
I have this lab I've been trying to work on but I can't figure out how to complete it. Here are all the resources provided by the instructor.
LAB08, Array of Student Class objects – Selection Sort Points: 25
OBJECTIVE
To demonstrate the concept of sorting and index to an array
To demonstrate the Selection sort
To demonstrate sorting objects
REQUIRED
A report of your work, in a flat pocket folder, in the following order:
1. Grade form Lab08GradeForm.doc
2. This program description
3. The source listing of the Student class Student.java
4. The source listing of the StudentSort class StudentSort.java
5. Problem Analysis with Input, Output, Pseudocode (word processed)
6. The listing of the source program Lab08.java
Data dictionary (included as comments)
7. Listing of the input file Lab08StudentFile.txt
8. Listing of the student name file Lab08Names.txt
9. Listing of the expected results (already created) Lab08ExpectedResults.xlsx
10. Listing of the output file Lab08Report.txt
SPECIFICATIONS
Start with Lab06 or Lab07 and rename it as Lab08
There were two parts to Lab06:
Part1 – create an array of objects and then place values into the objects in the array
Part2 – retrieve the objects from the array and print a report
Lab08 will insert a step in between –- Sorting the array of objects
Using the Selection sort, sort the array in ascending order, by student name
Since the name is not in the Student objects, we will create a separate array (index) to sort the name. This array is an array of objects of the StudentSort class. I have created the StudentSort class (StudentSort.java). It consists of the subscript for the student in the Student array and that student’s name.
Create the StudentSort array at the same time you are creating the array of Students.
Use the student id to search for the student name. Create a StudentSort object with the subscript from the Student array and the student’s name. Insert it into the StudentSort array. You need a separate subscript for the StudentSort array.
Sort the StudentSort array in name order using the selection sort.
Then go through the StudentSort array in sequence, using the subscript from the StudentSort object to retrieve the Student object from the Student array.
Refer to Java11SelectionSort and Java12MoreStrings
INPUT
File: Student file Lab08StudentFile.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 Lab08Names.txt
Record: Student name record
Field Data Type
Student id# 4 numbers (ex. 1234)
Student name String
OUTPUT
File: Grade Report file Lab08Report.txt
Record:
Student Grade Report, sorted by Name
ID# Name /---------------------TEST Scores----------------------/ Total Adj Total Avg
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx
Total students = xx
Here's the link to the completed lab07 which it builds upon.--> https://pastebin.com/M8NmNYry
Here's the link to the completed Student Class file.--> https://pastebin.com/DvZvKq2M
Here's the link to the completed StudentSort Class file.--> https://pastebin.com/7emKcUL6
Explanation / Answer
Hi, please find my implementation,
//Lab08
//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 Lab08
{//start of class
public static void main(String[] args) throws FileNotFoundException
{//start of main method
//declare a variable of type Scanner
Scanner infileName = new Scanner(new FileReader("Lab08StudentFile.txt"));
PrintWriter outfileName = new PrintWriter ("Lab08Report.txt");
//variables and defined constants
String studentName; //need studentName for searchName variable
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[] arrayStudentSort = new StudentSort[16];
//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;
studentName = searchName(stdId); //assigning searchName to studentName
StudentSort stdObject = new StudentSort(x, studentName); //created StudentSort array, used x (stdId) to search for name
arrayStudentSort[x] = stdObject; //arrayStudentSort inserts stdObject into the array
x++;
student++;
}//end while
//calls selectionSort method
selectionSort(arrayStudentSort);
for (int j = 0; j < arrayStudent.length; j++)
{//start for loop
StudentSort outObject = arrayStudentSort[j];
int k = outObject.getStudentSub();
Student std = arrayStudent[k];
outfileName.printf("%-5d", std.getStudentId());
scores = std.getStudentGradeArray();
outfileName.printf("%-20s", outObject.getStudentName());
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
private static void bubbleSort(Student[] arrayStudent)
{//begin method
for (int i = 0; i < arrayStudent.length; i++)
{
for (int j = 0; j < arrayStudent.length-1-i; j++)
{
if (arrayStudent[j].getAdjustedAverage() >arrayStudent[j+1].getAdjustedAverage())
{
Student temp = new Student();
//set arrayStudent at index=j to temp object
temp.setStudentId(arrayStudent[j].getStudentId());
temp.setStudentGradeArray(arrayStudent[j].getStudentGradeArray());
//set arrayStudent at index=j+1 to arrayStudent object
arrayStudent[j].setStudentId(arrayStudent[j+1].getStudentId());
arrayStudent[j].setStudentGradeArray(arrayStudent[j+1].getStudentGradeArray());
//set arrayStudent at index=j+1 to temp object
arrayStudent[j+1].setStudentId(temp.getStudentId());
arrayStudent[j+1].setStudentGradeArray(temp.getStudentGradeArray());
}
}
}
}//end method
public static String searchName(int searchNumber) throws FileNotFoundException
{//begin method
Scanner infileName = new Scanner (new FileReader("Lab08Names.txt"));
boolean found = false;
String returnName = "name not found";
while (infileName.hasNext() && found == false)
{//start while loop
int stdId = infileName.nextInt();
String searchName = infileName.nextLine().trim();
{
if(searchNumber == stdId)
{
returnName = searchName;
found = true;
}
}
}//end while
infileName.close();
return returnName;
}//end of method
//function to sort StudentSort array
public static void selectionSort(StudentSort[] sortStudent)
{//begin method
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;
}
}//end method
}//end of class
/*
Sample run:
Student Grade Report
ID# Name /-------------------Test Scores----------------/ Total AdjTotal Avg
1225 Caruso, Anthony 70 70 70 70 70 70 70 70 70 70 700 770 77
1555 Collins, Richard 70 70 70 60 65 90 85 80 80 80 750 840 84
1991 Graham, Gary 100 90 75 85 90 88 79 88 91 90 876 976 98
1989 Kelley, Nico 59 59 59 59 59 59 59 59 59 75 606 681 68
1221 Korb, Barbara 100 100 100 100 100 100 100 100 100 100 1000 1100 110
1223 Kramer, Susan 80 80 80 80 80 80 80 80 80 80 800 880 88
1987 Lydon, Arthur 68 68 68 68 68 68 68 68 68 100 712 812 81
1227 Nickerson, Philip 60 60 60 60 60 60 60 60 60 60 600 660 66
1993 Parsons, Michael 91 90 75 85 90 88 79 88 91 90 867 958 96
1222 Schafer, Anne 87 87 87 87 87 87 87 87 87 87 870 957 96
1224 Smith, Lindy 77 77 77 77 77 77 77 77 77 77 770 847 85
1226 Sokalski, Mark 67 67 67 67 67 67 67 67 67 67 670 737 74
1856 Stanson, Luke 60 0 45 68 89 67 60 50 75 80 594 683 68
1343 Vickers, Jerry 90 85 80 65 75 95 85 75 80 94 824 919 92
1228 Wydner, Peter 57 57 57 57 57 57 57 57 57 57 570 627 63
1967 name not found 90 85 87 88 70 90 92 87 88 67 844 936 94
Total Students = 16
*/