I have this lab I\'ve been trying to work on but I can\'t figure out how to comp
ID: 3824859 • 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.
LAB07, Array of Student Class objects – Bubble Sort Points: 25
OBJECTIVE
• To demonstrate the concept of sorting
• To demonstrate the Bubble sort
• To demonstrate sorting objects
REQUIRED
A report of your work, in a flat pocket folder, in the following order:
1. Grade form Lab07GradeForm.doc
2. This program description
3. The source listing of the Student class Student.java
4. Problem Analysis with Input, Output, Pseudocode (word processed)
5. Data dictionary (included as comments in Java program
6. The listing of the source program Lab07.java
7. Listing of the input file Lab07StudentFile.txt
8. Listing of the student name file Lab07Names.txt
8. Listing of the expected results (already created) Lab07ExpectedResults.xlsx
9. Listing of the output file Lab07Report.txt
SPECIFICATIONS
Start with Lab06 and rename it as Lab07
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
Lab07 will insert a step in between –- Sorting the array of objects
Create a Bubble Sort method and pass the array to it. Using the Bubble sort, sort the array in descending order, by average Since arrays are referenced when passed to a method, there is no need to return the array.
In order for this to work properly: Part1 must create the complete array and nothing else Then the array is sorted Part2 retrieves objects from the array and creates the report, with the student name. Therefore, the name search must be after the sort.
The report should look the same as Lab06 except it will be in descending order by average.
INPUT
File: Student file Lab07StudentFile.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 Lab07Names.txt
Record: Student name record
Field Data Type
Student id# 4 numbers (ex. 1234)
Student name String
OUTPUT
File: Grade Report file Lab07Report.txt
Record:
Student Grade Report, sorted by Avg
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
Explanation / Answer
/***
* The java program is modified to sort the objects of the Student
* class and write descending sorted Student objects to console.
* */
//Lab06.java
import java.util.*;
import java.io.*;
public class Lab06
{//start of class
public static void main(String[] args) throws FileNotFoundException
{//start of main method
Scanner filescanner = new Scanner(System.in);
//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
//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
//calling bubbleSort method with arrayStudent array object
bubbleSort(arrayStudent);
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", searchName(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
/*
* Bubble sort method that takes arrray of type Student and
* sorts the array of objects in the descending order .
* */
private static void bubbleSort(Student[] arrayStudent)
{
for (int i = 0; i < arrayStudent.length; i++)
{
for (int j = 0; j < arrayStudent.length-1-i; j++)
{
//descending order
if(arrayStudent[j].getAdjustedAverage()
>arrayStudent[j+1].getAdjustedAverage())
{
//create a temp object of Student class
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());
}
}
}
}
public static String searchName(int searchNumber) throws FileNotFoundException
{//begin method
Scanner infileName = new Scanner (new FileReader("Lab06Names.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
}//end of class
-------------------------------------------------------------------------------------------------------------------
//Student.java
public class Student
{
private int inStudentId;
private int [] inStudentGrades = new int[10];
//class constructor
public Student(int inStudentId, int[] inStudentGrades)
{//begin method
setStudentId (inStudentId);
setStudentGradeArray(inStudentGrades);
}//end method
//class default constructor
public Student()
{//begin method
int tempArray [ ] = {0,0,0,0,0,0,0,0,0,0};
setStudentId (0);
setStudentGradeArray(tempArray);
}//end method
public void setStudentGradeArray (int[] inStudentGrades)
{//begin method
for (int c = 0; c<inStudentGrades.length; c++)
this.inStudentGrades[c] = inStudentGrades[c];
}//end method
public void setStudentId(int inStudentId)
{//begin method
this.inStudentId = inStudentId;
}//end method
public int[] getStudentGradeArray()
{//begin method
return inStudentGrades;
}//end method
public int getStudentId()
{//begin method
return inStudentId;
}//end method
//Returns total
public double getTotal()
{//begin method
double total=0;
for (int i = 0; i < inStudentGrades.length; i++)
{
total+=inStudentGrades[i];
}
return total;
}//end method
//Returns adjusted total
public double getAdjustedtotal()
{//begin method
int lowestScore = getLowestScore();
int highestScore = getHighestScore();
double total = getTotal();
total = total+highestScore;
return total;
}//end method
//Returns the adjusted average
public double getAdjustedAverage()
{//begin method
double total = 0;
total = getAdjustedtotal();
return (total/inStudentGrades.length);
}
//end method
//Returns highest score
public int getHighestScore()
{//begin method
int highestScore = inStudentGrades[0];
for (int i = 1; i < inStudentGrades.length; i++)
{
if(inStudentGrades[i]>highestScore)
highestScore = inStudentGrades[i];
}
return highestScore;
}//end method
//Returns lowest score
public int getLowestScore()
{//begin method
int lowestScore = inStudentGrades[0];
for (int i = 1; i < inStudentGrades.length; i++)
{
if(inStudentGrades[i]<lowestScore)
lowestScore = inStudentGrades[i];
}
return lowestScore;
}//end method
}//end of Student Class
-------------------------------------------------------------------------------------------------------------------
Input files
Lab06Names.java
1217 Carson, Joseph
1221 Korb, Barbara
1222 Schafer, Anne
1223 Kramer, Susan
1224 Smith, Lindy
1225 Caruso, Anthony
1226 Sokalski, Mark
1227 Nickerson, Philip
1228 Wydner, Peter
1256 Anderson, Beth
1277 Meyers, Henry
1343 Vickers, Jerry
1555 Collins, Richard
1602 Zimmer, Nancy
1856 Stanson, Luke
1976 Richman, Julie
1987 Lydon, Arthur
1989 Kelley, Nico
1991 Graham, Gary
1993 Parsons, Michael
Lab06StudentFile.java
1221 100 100 100 100 100 100 100 100 100 100
1222 87 87 87 87 87 87 87 87 87 87
1223 80 80 80 80 80 80 80 80 80 80
1224 77 77 77 77 77 77 77 77 77 77
1225 70 70 70 70 70 70 70 70 70 70
1226 67 67 67 67 67 67 67 67 67 67
1227 60 60 60 60 60 60 60 60 60 60
1228 57 57 57 57 57 57 57 57 57 57
1343 90 85 80 65 75 95 85 75 80 94
1555 70 70 70 60 65 90 85 80 80 80
1856 60 0 45 68 89 67 60 50 75 80
1967 90 85 87 88 70 90 92 87 88 67
1987 68 68 68 68 68 68 68 68 68 100
1989 59 59 59 59 59 59 59 59 59 75
1991 100 90 75 85 90 88 79 88 91 90
1993 91 90 75 85 90 88 79 88 91 90
-------------------------------------------------------------------------------------------------------------------
Output file :
Lab06Report.java
Student Grade Report
ID# Name /-------------------Test Scores----------------/ Total AdjTotal Avg
1228 Wydner, Peter 57 57 57 57 57 57 57 57 57 57 570 627 63
1227 Nickerson, Philip 60 60 60 60 60 60 60 60 60 60 600 660 66
1989 Kelley, Nico 59 59 59 59 59 59 59 59 59 75 606 681 68
1856 Stanson, Luke 60 0 45 68 89 67 60 50 75 80 594 683 68
1226 Sokalski, Mark 67 67 67 67 67 67 67 67 67 67 670 737 74
1225 Caruso, Anthony 70 70 70 70 70 70 70 70 70 70 700 770 77
1987 Lydon, Arthur 68 68 68 68 68 68 68 68 68 100 712 812 81
1555 Collins, Richard 70 70 70 60 65 90 85 80 80 80 750 840 84
1224 Smith, Lindy 77 77 77 77 77 77 77 77 77 77 770 847 85
1223 Kramer, Susan 80 80 80 80 80 80 80 80 80 80 800 880 88
1343 Vickers, Jerry 90 85 80 65 75 95 85 75 80 94 824 919 92
1967 name not found 90 85 87 88 70 90 92 87 88 67 844 936 94
1222 Schafer, Anne 87 87 87 87 87 87 87 87 87 87 870 957 96
1993 Parsons, Michael 91 90 75 85 90 88 79 88 91 90 867 958 96
1991 Graham, Gary 100 90 75 85 90 88 79 88 91 90 876 976 98
1221 Korb, Barbara 100 100 100 100 100 100 100 100 100 100 1000 1100 110
Total Students = 16