I have this lab I\'ve been trying to work on but I can\'t figure out how to comp
ID: 3825775 • 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 lab06 which it builds upon.--> https://pastebin.com/4cxF0YGb
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 answer:
public class Student implements Comparable<Student>{
//Data members
private String firstName;
private String lastName;
int quizzes = 0;
int count = 0;
//Constructor Student
Student(String n){
n = n.trim();
int index = n.indexOf(' ');
if(index == -1){
firstName = n;
lastName = "";
}else{
firstName = n.substring(0, index);
lastName = n.substring(index+1).trim();
}
}
public Student(String f, String l) {
firstName = f;
lastName = l;
}
//methods
public String getName(){
return firstName+" "+lastName;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public void addQuiz(int score){
quizzes = quizzes + score;
count++;
}
public int getTotalScore(){
return quizzes;
}
public int getAverageScore(){
int avg = quizzes/count;
return avg;
}
public int getHighestScore(int[] q){
int[] qz = getScores(q);
return qz[qz.length-1];
}
public int getLowestScore(int[] q){
int[] qz = getScores(q);
return qz[0];
}
public int[] getScores(int[] q){
for(int i=0;i<q.length-1;i++){
for(int j=i+1;j<q.length; j++){
if(q[i]>q[j]){
int temp = q[i];
q[i] = q[j];
q[j] = temp;
}
}
}
return q;
}
public String toString(){
return " First Name : "+firstName+", Last Name: "+lastName+" , Number of quizzes : "+count;
}
@Override
public int compareTo(Student o) {
if( lastName.compareTo(o.lastName) == 0){
return firstName.compareTo(o.firstName);
}else{
return lastName.compareTo(o.lastName);
}
}
}
######################
import java.util.Arrays;
/**
* Sort an array of Student Objects. The sort requires that the Student
* class implement the Comparable interface.
*
*/
public class Lab8StudentSorter {
public static void main(String[] args) {
Student[] studentData = {
new Student("Bill", "Johnson"),
new Student("Tom", "Thompson"), new Student("Penny", "King"),
new Student("Bill", "Thomas"), new Student("Bob", "Johnson"),
new Student("Betty", "Johnson"), new Student("Bill", "Smith"),
new Student("Sam", "Appleton"), new Student("Sue", "Cook"),
};
System.out.println("Unsorted Student Data");
for(Student student : studentData) {
System.out.println(student);
}
// Sort the student data using the natural ordering
Arrays.sort(studentData);
System.out.println();
System.out.println();
System.out.println("Sorted Student Data");
for(Student student : studentData) {
System.out.println(student);
}
}
}
/*
Sample run:
Unsorted Student Data
First Name : Bill, Last Name: Johnson , Number of quizzes : 0
First Name : Tom, Last Name: Thompson , Number of quizzes : 0
First Name : Penny, Last Name: King , Number of quizzes : 0
First Name : Bill, Last Name: Thomas , Number of quizzes : 0
First Name : Bob, Last Name: Johnson , Number of quizzes : 0
First Name : Betty, Last Name: Johnson , Number of quizzes : 0
First Name : Bill, Last Name: Smith , Number of quizzes : 0
First Name : Sam, Last Name: Appleton , Number of quizzes : 0
First Name : Sue, Last Name: Cook , Number of quizzes : 0
Sorted Student Data
First Name : Sam, Last Name: Appleton , Number of quizzes : 0
First Name : Sue, Last Name: Cook , Number of quizzes : 0
First Name : Betty, Last Name: Johnson , Number of quizzes : 0
First Name : Bill, Last Name: Johnson , Number of quizzes : 0
First Name : Bob, Last Name: Johnson , Number of quizzes : 0
First Name : Penny, Last Name: King , Number of quizzes : 0
First Name : Bill, Last Name: Smith , Number of quizzes : 0
First Name : Bill, Last Name: Thomas , Number of quizzes : 0
First Name : Tom, Last Name: Thompson , Number of quizzes : 0
*/