In: Computer Science
Create a class called Student which stores
• the name of the student
• the grade of the student
• Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into the grade field.
• Calculate the average of all students’ grades.
• Open the output file, for writing, and print all the students’ names on one line and the average on the next line.
• Average should only have 1 digit after the decimal
• “Average” should be left justified in a field of 15 characters. The average # should be right justified in a field of 10 spaces
Sample input:
Minnie Mouse 98.7
Bob Builder 65.8
Mickey Mouse 95.1
Popeye SailorMan 78.6
Output:
Minnie Mouse, Bob Builder, Mickey Mouse, Popeye SailorMan
Average: 84.5
How can I fix my code to get the same output
public class Student {
public static void main(String[] args)throws IOException{
Scanner k=new Scanner(System.in);
String name;
float grade;
float Average=(float) 0.0;
float sum=(float) 0.0;
System.out.println("Enter input file name:");
String input=k.nextLine();
System.out.println("Enter output file name:");
String output=k.nextLine();
PrintWriter pw = new PrintWriter("students.txt");
pw.print("Minnie Mouse 98.7\nBob Builder 65.8\nMickey Mouse
95.1\nPopeye SailorMan 78.6\n");
pw.close();
PrintWriter pw2 = new PrintWriter("students2.txt");
pw2.print("Donald Duck 77.77\nTweety Bird 55.555\nCharlie Brown
95.231\n");
pw2.close();
/* End of creating the input file */
while(k.hasNext())
{
name=k.next();
grade=k.nextFloat();
Average+=grade;
sum++;
System.out.printf("name, ", name);
}
grade=Average/sum;
System.out.printf("\n%-15s%10.1f","Average", grade);
/* test output file */
Scanner testOutput = new Scanner(new File("average.txt"));
while(testOutput.hasNext())
System.out.println(testOutput.nextLine());
/* end of test output file */
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thank
Note: The average will be 84.6 not 84.5. Because the actual average is 84.55, formatting to 1 digit after decimal point will automatically round it to 84.6
//Student.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Student {
// attributes
private String name;
private double grade;
// constructor taking name and grade
public Student(String name, double grade) {
this.name = name;
this.grade = grade;
}
// getters for name and grade
public String getName() {
return name;
}
public double getGrade() {
return grade;
}
// helper method to create input files dynamically, if you want
private static void createInputFiles() throws FileNotFoundException {
PrintWriter pw = new PrintWriter("students.txt");
pw.print("Minnie Mouse 98.7\nBob Builder 65.8\nMickey Mouse 95.1\nPopeye SailorMan 78.6\n");
pw.close();
PrintWriter pw2 = new PrintWriter("students2.txt");
pw2.print("Donald Duck 77.77\nTweety Bird 55.555\nCharlie Brown 95.231\n");
pw2.close();
}
// main method
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String name;
double grade, sum, avg;
// uncomment below statement if you want to create input files
// (students.txt, students2.txt) dynamically.
// createInputFiles();
// asking, reading input & output file names
System.out.println("Enter input file name:");
String inFile = sc.nextLine();
System.out.println("Enter output file name:");
String outFile = sc.nextLine();
// creating a list of Student objects
ArrayList<Student> students = new ArrayList<Student>();
// reinitializing scanner to read from file
sc = new Scanner(new File(inFile));
// looping through file
while (sc.hasNext()) {
// reading next two strings to create single name
name = sc.next() + " " + sc.next();
// reading next number to store grade
grade = sc.nextDouble();
// creating a Student object with this name and grade, adding to
// list
students.add(new Student(name, grade));
}
// closing file
sc.close();
sum = 0;
// opening output file
PrintWriter writer = new PrintWriter(new File(outFile));
// looping through the list
for (int i = 0; i < students.size(); i++) {
// adding grade to sum
sum += students.get(i).getGrade();
// printing student name to output file followed by ', ' if
// necessary
writer.print(students.get(i).getName());
if (i != students.size() - 1) {
writer.print(", ");
}
}
// finding average
avg = sum / students.size();
// displaying average with proper formatting. (label is formatted with
// 15 character wide field left justified, average formatted to 1 digit
// after decimal, in a field of length 10, right justified)
writer.printf("\n%-15s%10.1f\n", "Average:", avg);
// closing file, saving changes
writer.close();
}
}
/*OUTPUT*/
Enter input file name:
students.txt
Enter output file name:
output.txt
/*output.txt*/
Minnie Mouse, Bob Builder, Mickey Mouse, Popeye SailorMan
Average: 84.6