Java SPECIFICATIONS Start with Lab05 and rename it as Lab06 Create an array of S
ID: 3825936 • Letter: J
Question
Java
SPECIFICATIONS
Start with Lab05 and rename it as Lab06
Create an array of Student class objects
1. Instead of creating one instance of the Student class, create an array of Student class objects. Create the array as you would for any data type, but use Student objects, 16 elements
EXAMPLE: Student [ ] arrayStudent = new Student [16];
2. Within the loop, input data from the data file into variables
3. Create a single instance of the Student class, using the constructor
4. Place the single instance of the Student class into the array of Student objects
This completes loading the array with Student objects
Retrieve data from the array of objects (this is to prepare for Labs 07 and 08)
5. Use a for/loop to go through the array of objects and print the report
Within the loop, create a single instance of the Student class and move the object out of the array into the single instance
6. Use the class method to calculate the total, adjusted total and average
7. Do the following in the application program:
For each student, look up their name from the student name file, using the student id.
Do a sequential search from the file (not an array). Use a value returning method.
The method will open and close the file for each student look-up.
The method header needs throws FileNotFoundException
Declare the student name file in the method
Read the student id and student name, then compare student id to the search id
Once the name is found (or “name not found”), close the file and return the name
8. Print the report (almost exactly as in Lab05 except add the student name)
PROGRAMMING STYLE
I find it clearer to use single variables instead of subscripted variables, whenever possible.
When reading the input student file, first place data into a variable
Then create a single object.
Then place the object into the array of objects.
When retrieving data from the array of objects, return each object into a single object
Methods: should have ONE exit point.
For a value-returning method, this means one return statement
INPUT
File: Student file Lab06StudentFile.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 Lab06Names.txt
Record: Student name record
Field Data Type
Student id# 4 numbers (ex. 1234)
Student name String
OUTPUT
File: Grade Report file Lab06Report.txt
Record:
Student Grade Report
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
Lab 5
public class StudentLab5
{//begin class
private int studentID;
private int[]studentGrades = new int[10];
//class constructor
public StudentLab5(int inStudentID, int[]inStudentGrades)
{
setStudentID(inStudentID);
setStudentGradesArray(inStudentGrades);
}
//class default constructor
public StudentLab5()
{
int tempArray[] = {0,0,0,0,0,0,0,0,0,0};
setStudentID(0);
setStudentGradesArray(tempArray);
}
//set methods
public void setStudentID(int inStudentID)
{
studentID = inStudentID;
}
public void setStudentGradesArray (int[]inStudentGrades)
{
for (int setSub = 0; setSub < 10; setSub++)
{
studentGrades[setSub] = inStudentGrades[setSub];
}
}//end for loop
//get method
public int getStudentID()
{
return studentID;
}
public int[]getStudentGradesArray()
{
return studentGrades;
}
//calculate total
public int calculatedTotal()
{int total = 0;
for (int totalSub = 0; totalSub < 10; totalSub++)
{
total += studentGrades[totalSub];
}
return total;
}//
public int calculateAdjustedTotal()//
{
int lastSub = 0;
int high = 0;
int low = 100;
while (lastSub < 10)//begin while
{
if (studentGrades[lastSub] > high)
{
high = studentGrades[lastSub];
}
if (studentGrades[lastSub] < low)
{
low = studentGrades[lastSub];
}
lastSub++;
}//end while
int adjustedTotal = calculatedTotal() + high - low;
return adjustedTotal;
}
public int calculatedAverage()//round grade
{
double average = Math.round(calculateAdjustedTotal()/10.0);
int roundedAverage = (int)average;
return roundedAverage;
}
}//end class
lab 5 other part
import java.util.*;
import java.io.*;
public class Lab05
{
//start class
public static void main(String [] args) throws FileNotFoundException
{
//start of main method
Scanner fileStudentInput = new Scanner(new FileReader("Lab05StudentFile.txt"));
//input file
PrintWriter reportFile = new PrintWriter("Lab05Report.txt");
//output file
int studentID;
int studentGrades[] = new int[10];
reportFile.println("Student Test Grades");
reportFile.println();
reportFile.printf("%-11s%5s%7s%7s%7s%7s", "StudentID", "Test1", "Test2",
"Test3", "Test4", "Test5");
reportFile.printf("%7s%7s%7s%7s%8s", "Test6", "Test7", "Test8",
"Test9", "Test10");
reportFile.printf("%7s%10s%9s%n", "Total", "AdjTotal", "Average");
reportFile.println();
while(fileStudentInput.hasNext())//begin while for input
{
studentID = fileStudentInput.nextInt();
for(int m = 0; m < studentGrades.length; m++)
{
studentGrades[m] = fileStudentInput.nextInt();
}
StudentLab5 okay = new StudentLab5(studentID, studentGrades);
studentID = okay.getStudentID();
studentGrades = okay.getStudentGradesArray();
int total = okay.calculatedTotal();
int adjustedTotal = okay.calculateAdjustedTotal();
int roundedAverage = okay.calculatedAverage();
reportFile.printf("%9d%7d", studentID, studentGrades[0]);
for(int n = 1; n < (studentGrades.length - 1); n++)
{
reportFile.printf("%7d", studentGrades[n]);
}
reportFile.printf("%8d%7d%10d%9d%n", studentGrades[9], total, adjustedTotal, roundedAverage);
}//end while
reportFile.println();
reportFile.print("Total Students: 20");
fileStudentInput.close();
reportFile.close();
}//end main method
}//end class
Lab 6 stuff
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
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
Explanation / Answer
import java.util.*;
import java.io.*;
public class Lab06
{
//start class
public static void main(String [] args) throws FileNotFoundException
{
//start of main method
Scanner fileStudentInput = new Scanner(new FileReader("Lab06StudentFile.txt"));
//input file
PrintWriter reportFile = new PrintWriter("Lab06Report.txt");
//output file
int studentID;
int studentGrades[] = new int[10];
StudentLab06 arrayStudent[] = new StudentLab06[16];
reportFile.println("Student Test Grades");
reportFile.println();
reportFile.printf("%-11s%5s%7s%7s%7s%7s", "StudentID", "Test1", "Test2", "Test3", "Test4", "Test5");
reportFile.printf("%7s%7s%7s%7s%8s", "Test6", "Test7", "Test8", "Test9", "Test10");
reportFile.printf("%7s%10s%9s%n", "Total", "AdjTotal", "Average");
reportFile.println();
int count = 0;
while(fileStudentInput.hasNext())//begin while for input
{
studentID = fileStudentInput.nextInt();
for(int m = 0; m < studentGrades.length; m++){
studentGrades[m] = fileStudentInput.nextInt();
}
StudentLab06 okay = new StudentLab06(studentID, studentGrades);
arrayStudent[count] = okay;
count++;
if(count>=16)
break;
}
for(int i=0; i<16; i++){
StudentLab06 okay = arrayStudent[i];
studentID = okay.getStudentID();
studentGrades = okay.getStudentGradesArray();
int total = okay.calculatedTotal();
int adjustedTotal = okay.calculateAdjustedTotal();
int roundedAverage = okay.calculatedAverage();
reportFile.printf("%9d%7d", studentID, studentGrades[0]);
for(int n = 1; n < (studentGrades.length - 1); n++){
reportFile.printf("%7d", studentGrades[n]);
}
reportFile.printf("%8d%7d%10d%9d%n", studentGrades[9], total, adjustedTotal, roundedAverage);
}//end for
reportFile.println();
reportFile.print("Total Students: "+count);
fileStudentInput.close();
reportFile.close();
}//end main method
}//end class
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class StudentLab06
{//begin class
private int studentID;
private int[]studentGrades = new int[10];
//class constructor
public StudentLab06(int inStudentID, int[]inStudentGrades)
{
setStudentID(inStudentID);
setStudentGradesArray(inStudentGrades);
}
//class default constructor
public StudentLab06()
{
int tempArray[] = {0,0,0,0,0,0,0,0,0,0};
setStudentID(0);
setStudentGradesArray(tempArray);
}
//set methods
public void setStudentID(int inStudentID)
{
studentID = inStudentID;
}
public void setStudentGradesArray (int[]inStudentGrades)
{
for (int setSub = 0; setSub < 10; setSub++)
{
studentGrades[setSub] = inStudentGrades[setSub];
}
}//end for loop
//get method
public int getStudentID()
{
return studentID;
}
public int[]getStudentGradesArray()
{
return studentGrades;
}
//calculate total
public int calculatedTotal()
{int total = 0;
for (int totalSub = 0; totalSub < 10; totalSub++)
{
total += studentGrades[totalSub];
}
return total;
}//
public int calculateAdjustedTotal()//
{
int lastSub = 0;
int high = 0;
int low = 100;
while (lastSub < 10)//begin while
{
if (studentGrades[lastSub] > high)
{
high = studentGrades[lastSub];
}
if (studentGrades[lastSub] < low)
{
low = studentGrades[lastSub];
}
lastSub++;
}//end while
int adjustedTotal = calculatedTotal() + high - low;
return adjustedTotal;
}
public int calculatedAverage()//round grade
{
double average = Math.round(calculateAdjustedTotal()/10.0);
int roundedAverage = (int)average;
return roundedAverage;
}
}//end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.