modify below program so that the names of the input file and the output file wil
ID: 3633626 • Letter: M
Question
modify below program so that the names of the input file and the output file will be supplied by the command line instruction to run the program.the program to be modified:
import java.io.*;
import java.util.*;
class Student {
private String name;
private double[] grades;
private double average;
/* Student constructor. Initializes instance variables. */
public Student(String aName, double[] theGrades) {
name = aName;
grades = theGrades;
double sum = 0;
for (int i = 0; i < grades.length; i++)
sum = sum + grades[i];
average = sum / grades.length;
}
/* returns the Student average */
public double getAv() {
return average;
}
/* prints the Student name and average to the given stream */
public void print(PrintStream out) {
out.printf("%20s%10.2f", name, average);
}
}
class aClass {
private int noStudents; // number of students in class
private double total; // the sum of all Students averages
private double average; // the class average
private Student[] classList; // an array of the Students in the class
/*
* constructor of aClass. Initializes instance variables to 0. creates an
* array of 10 Students
*/
public aClass() {
noStudents = 0;
total = 0;
average = 0;
classList = new Student[10];
}
/*
* adds a new Student to the array of Students, updating the number of
* Students, the class total and the class average
*/
public void addStudent(String name, double[] grades) {
Student aStudent = new Student(name, grades);
classList[noStudents] = aStudent;
total = total + aStudent.getAv();
noStudents++;
average = total / noStudents;
}
// alternatively
public void addStudent(Student aStudent) {
classList[noStudents] = aStudent;
total = total + aStudent.getAv();
noStudents++;
average = total / noStudents;
}
/* returns the class average */
public double getClassAverage() {
return average;
}
/* returns the Student with the highest average */
public Student maxStudent() {
double max = classList[0].getAv();
int loc = 0;
for (int i = 1; i < noStudents; i++) {
double av = classList[i].getAv();
if (av > max) {
max = av;
loc = i;
}
}
return classList[loc];
}
/* prints the class information to the given stream*/
public void print(PrintStream out) {
out.println("the class average is: " + average);
for (int i = 0; i < noStudents; i++) {
out.println();
classList[i].print(out); // calling the Student method print()
if (classList[i].getAv() == average)
out.print("---> equal to average");
else if (classList[i].getAv() < average)
out.print("---> below average");
else
out.print("---> above average");
// print the indicator to the best student
if (classList[i] == maxStudent())
out.print("<--- Highest");
}
out.println();
}
}
public class Marks {
public static void main(String[] args) throws Exception {
aClass csc111 = new aClass();
// opens input file
Scanner inFile = new Scanner(new File("input.txt"));
//creates output file
PrintStream outFile = new PrintStream(new File("output.txt"));
//reads first line of the file as the number of students
int noOfStudents = inFile.nextInt();
inFile.nextLine();
while (noOfStudents > 0) {
//gets first line as student name
String name = inFile.nextLine();
// gets second line grades
String mark = inFile.nextLine();
// splits grades based on spaces
String marks[] = mark.split(" ");
//converts grades from string to double and
//placed in double array
double grades[] = new double[marks.length];
for (int i = 0; i < grades.length; i++)
grades[i] = Double.parseDouble(marks[i]);
//creates student object
Student aStudent = new Student(name, grades);
//adds student to list
csc111.addStudent(aStudent);
noOfStudents--;
}
// prints output to console
csc111.print(System.out);
// prints output to output.txt file
csc111.print(outFile);
//closes both input and output files
inFile.close();
outFile.close();
} // main
}
next step
Explanation / Answer
public static void main(String[] args) throws Exception {
aClass csc111 = new aClass();
// opens input file
Scanner inFile = new Scanner(new File(args[0]));
//creates output file
PrintStream outFile = new PrintStream(new File(args[1]));
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.