Create a class called Student which stores • the name of the student • the grade of...

80.2K

Verified Solution

Question

Programming

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 theinput file and the name of the output file. Main should open theinput file for reading . It should read in the first and last nameof each student into the Student’s name field. It should read thegrade 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 15characters. The average # should be right justified in a field of10 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, PopeyeSailorMan

Average:       84.5

How can I fix my code to get the sameoutput

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 Mouse95.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 Brown95.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 */
}
}
  

Answer & Explanation Solved by verified expert
4.1 Ratings (673 Votes)
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 Ill help you fix whatever issues Thank Note The average will be 846 not 845 Because the actual average is 8455 formatting to 1 digit after decimal point will automatically round it to 846 Studentjava import javaioFile import javaioFileNotFoundException import javaioIOException import    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students