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

I was sick the day my professor talked about files, and now this is due. I had a

ID: 3859310 • Letter: I

Question

I was sick the day my professor talked about files, and now this is due. I had a rough idea on how to get numbers from a file but wanted to post it here just in case. I am going to try it on my own but wanted to post this here just in case I can't figure it out. Any help would be much appreciated.

"Write a program that reads student scores from a file. You do not know how many students in the class (therefore in the file) The program finds the average for the class. Then it assigns the letter grade as follows: average + 10% and above is "A" average and above "B" is (less than the A grade above) average - 10% and above is a "C" (less than B above) less than average -10% is "F".

Sorry if its worded weird this is how he posted it on the blackboard website.

Explanation / Answer

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.IntStream;

public class GradeBook {
private List<double[]> Studs;
private static final DecimalFormat FMT = new DecimalFormat("#.#");

public GradeBook(Scanner input) {
this.Studs = new ArrayList<double[]>();
while (input.hasNextLine()) {
double[] Stu = Arrays.stream(input.nextLine().trim().split("\s+"))
.mapToDouble(Double::parseDouble)
.toArray();
Studs.add(Stu);
}
}

public double getScore(int Stu, int assing) {
return this.Studs.get(Stu)[assing];
}

public double averageForStu(int Stu) {
return Arrays.stream(this.Studs.get(Stu))
.average()
.getAsDouble();
}

public double averageForassing(int assing) {
return this.Studs.stream()
.mapToDouble((assignments) -> assignments[assing])
.average()
.getAsDouble();
}

public double average() {
return IntStream.range(0, this.Studs.size())
.mapToDouble((s) -> this.averageForStu(s))
.average()
.getAsDouble();
}

public String toString() {
StringBuilder out = new StringBuilder();
int numAssignments = this.Studs.stream()
.mapToInt((assignments) -> assignments.length)
.max()
.getAsInt();


out.append(" assing #: ");
for (int a = 0; a < numAssignments; a++) {
out.append(a + 1).append(' ');
}
out.append("Avg ");


for (int s = 0; s < this.Studs.size(); s++) {
out.append("Stu #").append(s + 1).append(": ");
for (int a = 0; a < numAssignments; a++) {
out.append(FMT.format(this.getScore(s, a))).append(' ');
}
out.append(FMT.format(this.averageForStu(s))).append(' ');
}

  
out.append("Average ");
for (int a = 0; a < numAssignments; a++) {
out.append(FMT.format(this.averageForassing(a))).append(' ');
}
out.append(' ');

return out.toString();
}

public static void main(String[] args) throws IOException {
try (Scanner input = args.length > 0 ? new Scanner(new File(args[0])) :
new Scanner(System.in)) {
GradeBook book = new GradeBook(input);
System.out.println(book);
System.out.printf("Overall Average: %s ",
FMT.format(book.average()));
}
}
}

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