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

Task The task is to read a file, store the data into objects, and process the ob

ID: 3882916 • Letter: T

Question

Task The task is to read a file, store the data into objects, and process the objects. The file is formatted as pairs of lines: the first line of each pair is the name of a student, and the second line is a list of grades. This data will be stored in a Grades object. The Grades class will have several methods for processing the data. For example, if this is the contents of the file being processed: Alice 87 99 96 99 86 96 77 95 70 88 Bob 73 78 76 80 99 96 73 96 76 78 78 92 93 75 93 Camila 99 94 85 99 99 93 81 95 76 80 77 79 98 72 98 97 9.2 Diego 76 97 72 92 86 86 89 85 81 87 76 80 89 then the following should be printed: Alice [87, 99, 96, 99, 86, 96, 77, 95, 70, 88] Alice Name Length: 10 Average: 89.30 Median: 91.5 Maximum: 99 Mininum: 70 Bob [73, 78, 76, 80, 99, 96, 73, 96, 76, 78, 78, 92, 93, 75, 93] Bob Name Length: 15 Average: 83.73 Median: 78.0 Maximum: 99 Mininum: 73 Camila [99, 94, 85, 99, 99, 93, 81, 95, 76, 80, 77, 79, 98, 72, 98, 97, 92] Camila Name: Length: 17 Average: 89.06 Median: 93.0 Maximum: 99 Mininum: 72 Diego [76, 97, 72, 92, 86, 86, 89, 85, 81, 87, 76, 80, 89] Diego Name Length: 13 Average: 84.31 Median: 86.0 Maximum: 97 Mininum: 72

Explanation / Answer

_______________

data.txt

Alice
87 99 96 99 86 96 77 95 70 88
Bob
73 78 76 80 99 96 73 96 76 78 78 92 93 75 93
Camila
99 94 85 99 99 93 81 95 76 80 77 79 98 72 98 97 92
Diego
76 97 72 92 86 86 89 85 81 87 76 80 89

_______________

Grades.java

import java.util.ArrayList;

public class Grades {
//Declaring instance variables
private String name;
private ArrayList < Integer > grades;


//Parameterized constructor
public Grades(String name, ArrayList < Integer > grades) {
super();
this.name = name;
this.grades = grades;
}


//getters and setters
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

//This method returns the size of an array List
public int length() {
return grades.size();
}
//This method calculates the average of a Student grades
public double average() {
double tot = 0;
for (int i = 0; i < grades.size(); i++) {
tot += grades.get(i);
}
return tot / grades.size();
}

//This method calculates the maximum grade of a Student
public int maximum() {
int max = grades.get(0);
for (int i = 0; i < grades.size(); i++) {
if (max < grades.get(i)) {
max = grades.get(i);
}
}
return max;
}

//This method calculates the minimum grade of a Student
public int minimum() {
int min = grades.get(0);
for (int i = 0; i < grades.size(); i++) {
if (min > grades.get(i)) {
min = grades.get(i);
}
}
return min;
}

//This method calculates the median of Student grades
public double median() {
//This Logic will Sort the Array of elements in Ascending order
int temp;
double median;
int middle;
for (int i = 0; i < grades.size(); i++) {
for (int j = i + 1; j < grades.size(); j++) {
if (grades.get(i) > grades.get(j)) {
temp = grades.get(i);
grades.set(i, grades.get(i));
grades.set(j, temp);
}
}
}
int count = grades.size();
if (count % 2 == 0) {
middle = count / 2;
} else {
middle = (count + 1) / 2;
}

median = (double)(grades.get(middle - 1) + grades.get(middle)) / 2.0;
return median;

}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return name + " " + grades;
}

public static void testGrades(Grades grades) {

System.out.println(grades.toString());
System.out.printf(" Name: %s ", grades.getName());
System.out.printf(" Length: %d ", grades.length());
System.out.printf(" Average: %.2f ", grades.average());
System.out.printf(" Median: %.2f ", grades.median());
System.out.printf(" Maximum: %d ", grades.maximum());
System.out.printf(" Minimum: %d ", grades.minimum());


}


}

_________________

Test.java

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {

public static void main(String[] args) {

//Creating an ArrayList which Stores grades of a student
ArrayList < Integer > grades = null;

//Creating an ArrayList which Stores Grades class objects
ArrayList < Grades > gr = new ArrayList < Grades > ();
Grades g;
Scanner in = null;

//Opening the data.txt file
try { in = new Scanner(new File("data.txt"));


} catch (Exception e) {
System.err.println("failed to ope data.txt ");
System.exit(1);
}

//Reading the data from the txt file
while ( in .hasNext()) {
grades = new ArrayList < Integer > ();
String name = in .next();
while ( in .hasNextInt()) {
int grade = in .nextInt();
grades.add(new Integer(grade));
}
g = new Grades(name, grades);
gr.add(g);
grades = null;
}


for (int i = 0; i < gr.size(); i++) {
Grades.testGrades(gr.get(i));
}

}

}

______________________

Output:

Alice [87, 99, 96, 99, 86, 96, 77, 95, 70, 88]

Name: Alice

Length: 10

Average: 89.30

Median: 99.00

Maximum: 99

Minimum: 87

Bob [73, 78, 76, 80, 99, 96, 73, 96, 76, 78, 78, 92, 93, 75, 93]

Name: Bob

Length: 15

Average: 83.73

Median: 99.00

Maximum: 99

Minimum: 73

Camila [99, 94, 85, 99, 99, 93, 81, 95, 76, 80, 77, 79, 98, 72, 98, 97, 92]

Name: Camila

Length: 17

Average: 89.06

Median: 99.00

Maximum: 99

Minimum: 99

Diego [76, 97, 72, 92, 86, 86, 89, 85, 81, 87, 76, 80, 89]

Name: Diego

Length: 13

Average: 84.31

Median: 97.00

Maximum: 97

Minimum: 76

______________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote