Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this program that I\'ve been working on that I managed to get running wit

ID: 3833784 • Letter: I

Question

I have this program that I've been working on that I managed to get running with help, however it's not printing the letter grades of the students. I have the instructions for the lab below and links to the current version of the lab, names and student files.

LAB09, Array of objects – Binary Search, Sequential Search   

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 what I have so far for lab09.--> https://pastebin.com/qKqGP4dV

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

here's a link to the Lab09StudentFile.--> https://pastebin.com/pkaT9xpR

Explanation / Answer

Here is the completed files for the question. I have added a new method to Student class to get avergae alone and not adjusted average. The reason for this is that for student 1221 (Barbara), the adjusted average is being shown as 110 which does not make sense. So I have added an average method to student class which returns total / 10. The grades are calculated on this simple average.

Output file contents are attached. Please don't forget to rate the answer if it helped. Thank you very much

Student.java

//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 the average

   public double getAverage()

   {//begin method

   double total = 0;

   total = getTotal();

   return (total/inStudentGrades.length);

   }

   //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

Lab09.java


//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
{//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("Lab09StudentFile.txt"));
PrintWriter outfileName = new PrintWriter ("Lab09Report.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];

// arrays to store their ids and names   
int[] ids = new int[20];
String[] names = new String[20];


//parallel arrays for rangelow and letter grades
int lowrange[]={97, 93,90, 87, 83,80, 77, 73, 70, 67, 63, 60, 00};
String grades[] = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","D-","F"};

// loading name and id
int c = 0;
Scanner nameFileScanner = new Scanner (new FileReader("Lab09Names.txt"));
while (nameFileScanner.hasNext())
{//start while loop
int id = nameFileScanner.nextInt();
String name = nameFileScanner.nextLine().trim();
ids[c] = id;
names[c] = name;
c++;

}//end while
nameFileScanner.close();

  
//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
studentName = binarySearch(ids, names,std.getStudentId());
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);

//create an instance of printwriter object
outfileName.println("Student Grade Report");
outfileName.println();
outfileName.println("ID# Name /-------------------Test Scores----------------/ Total AdjTotal Avg Grade");
outfileName.println();



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 %-3s",
std.getTotal(),
std.getAdjustedtotal(),
std.getAverage(),
getGrade(std.getAverage(), lowrange, grades));

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





//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


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;
}

private static String getGrade(double avg, int lowrange[], String grades[])
{
   for(int i=0;i<lowrange.length; i ++)
   {
       if(avg >= lowrange[i])
           return grades[i];
   }
  
   return "no grade";
}

}//end of class

//Lab09

output

Student Grade Report

ID# Name /-------------------Test Scores----------------/ Total AdjTotal Avg Grade

1225 Caruso, Anthony 70 70 70 70 70 70 70 70 70 70 700 770 70       C-

1555 Collins, Richard 70 70 70 60 65 90 85 80 80 80 750 840 75       C

1991 Graham, Gary 100 90 75 85 90 88 79 88 91 90 876 976 88       B+

1989 Kelley, Nico 59 59 59 59 59 59 59 59 59 75 606 681 61       D-

1221 Korb, Barbara 100 100 100 100 100 100 100 100 100 100 1000 1100 100       A+

1223 Kramer, Susan 80 80 80 80 80 80 80 80 80 80 800 880 80       B-

1987 Lydon, Arthur 68 68 68 68 68 68 68 68 68 100 712 812 71       C-

1227 Nickerson, Philip 60 60 60 60 60 60 60 60 60 60 600 660 60       D-

1993 Parsons, Michael 91 90 75 85 90 88 79 88 91 90 867 958 87       B

1222 Schafer, Anne 87 87 87 87 87 87 87 87 87 87 870 957 87       B+

1224 Smith, Lindy 77 77 77 77 77 77 77 77 77 77 770 847 77       C+

1226 Sokalski, Mark 67 67 67 67 67 67 67 67 67 67 670 737 67       D+

1856 Stanson, Luke 60 0 45 68 89 67 60 50 75 80 594 683 59       F

1343 Vickers, Jerry 90 85 80 65 75 95 85 75 80 94 824 919 82       B-

1228 Wydner, Peter 57 57 57 57 57 57 57 57 57 57 570 627 57       F

1967 name not found 90 85 87 88 70 90 92 87 88 67 844 936 84       B

Total Students = 16