Question

Write a class that reads data from 'Grades.txt' calculates average grade and output the data t 'Grades_average.txt'in JAVA

public class CH10 Grader_main { CH10 Grade public static void main(String [ ] args) { String fileName = StudentGrades.txt;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE

import java.io.IOException;
import java.io.BufferedReader; // FILE READING
import java.io.FileReader; // FILE READING
import java.io.BufferedWriter; // FILE WRITER
import java.io.FileWriter; // FILE WRITER
/*READING STUDENT DATA FROM A FILE THEN CALCULATE AVERAGE THEN STORE RESULT WITH STUDENT DATA TO ANOTHER FILE*/
class Grades
{
   public static void main(String a[])
   {
       try /// IOEXCEPTION FOR FILE READING AND WRITING
       {
           double Totsub1=0; // TO STORE TOTAL SUBJECT 1 FO ALL STIDENTS
           double Totsub2=0; // TO STORE TOTAL SUBJECT 2 FO ALL STIDENTS
           double Totsub3=0;
           double Totsub4=0;
           double TotAvg=0;
           int sub1,sub2,sub3,sub4; // STORE INDIVIDUAL SUBJECTS FOR A STUDENT , TOTAL 4 SUBJECTS

           //READING FROM FILE
           FileReader fr = new FileReader("Grades.txt"); // FILE CREATED Grades.txt
           BufferedReader ReadFileBuffer = new BufferedReader(fr); //FILE OPEN USING BufferedReader

           FileWriter fw = new FileWriter("Grades_Average.txt"); // file CREATED FOR WRIITING Grades_Average.txt
           BufferedWriter WriteFileBuffer = new BufferedWriter(fw);

           String data,Header;

           Header=ReadFileBuffer.readLine(); // READS FIRST HEADER ROW LINE FROM FILE

           WriteFileBuffer.write(Header+",Average"); // ADD A NEW COULUMN TO THE HEADER
           WriteFileBuffer.newLine(); // WRITE THE HEADER TO THE WRITE FILE

           int totStud=0; // USER FOR AVERAGE COUNTING

           while ((data=ReadFileBuffer.readLine()) !=null) //READING FILE LINE BY LINE
           {
               System.out.println(data); // PRINT THE MESSAGE
               String[] StudentData = data.split(","); /// SPLITT ALL DATAS OF A STUDENT

               // SUBJECT 1 IS AT POSTION INDEX OF 2 IN THE STRING ARRAY, SO NEED CASTING FROM STRING TO INT
               sub1=Integer.parseInt( StudentData[2]);
               sub2=Integer.parseInt( StudentData[3]);
               sub3=Integer.parseInt( StudentData[4]);
               sub4=Integer.parseInt( StudentData[5]);

               // SUBJECTWISE SUMMATION
               Totsub1=Totsub1+sub1;
               Totsub2=Totsub2+sub2;
               Totsub3=Totsub3+sub3;
               Totsub4=Totsub4+sub4;

               Double avg=(sub1 + sub2+ sub3+sub4) /4.0; // FIND AVERAGE FOR 4 SUBJECTS

               TotAvg=TotAvg+avg;

               WriteFileBuffer.write(data+','+avg); // WRITE STUDENT DATA WITH SUBEJCT AVERAGE TO WRITE FILE
               WriteFileBuffer.newLine(); // NEW LINE AFTER EACH INTEGER VALUE

               totStud++;
           }

           // WRITE FOOTER DATA, ALL AVERAGE IN SUBJECTS AND TOTAL AVERAGE TO WRITE FILE
           String footer="Average,00000,"+Totsub1/totStud+","+Totsub2/totStud+","+Totsub3/totStud+","+Totsub4/totStud+","+TotAvg/totStud;
           WriteFileBuffer.write(footer);


           ReadFileBuffer.close(); // CLOSE ReadFileBuffer
           WriteFileBuffer.close(); // CLOSE BufferedWriter


           System.out.println(" Average Grade Generated ");
       }
       catch (IOException e)
       {
           System.out.println(e); // IF ANY IO ERROR
       }
   } // MAIN METHOD END

} // END OF CLASS

SCREENSHOT CODE

import java.io.IOException; import java.io. BufferedReader; // FILE READING import java.io.FileReader; // FILE READING importwhile ((data=ReadFileBuffer.readLine()) !=null) //READING FILE LINE BY LINE System.out.println (data); // PRINT THE MESSAGE SSystem.out.println( Average Grade Generated ); catch (IOException e) System.out.println(e); // IF ANY IO ERROR } // MAIN ME

SCREEN SHOT OUTPUT

cs. C:\Windows\system32\cmd.exe aman, 201601,70,30,10,20 bman, 201602,30,30,20,20 cman, 201603,30,70,30,40 dman, 201604,10,70

Add a comment
Know the answer?
Add Answer to:
Write a class that reads data from 'Grades.txt' calculates average grade and output the data t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • java question: Write a program that reads in data from a text file named in.txt. Compute...

    java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • This is a java assignment Please help me Write the body of the fileAverage() method. Have...

    This is a java assignment Please help me Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place). For the testing system to work, don't change the class name nor the method name. Additionally, the file will have different contents during testing. public class Main { public double fileAverage( String filename ){    }...

  • Java. (20 pts)          Write a program that reads all the numbers from the file mynums.dat...

    Java. (20 pts)          Write a program that reads all the numbers from the file mynums.dat and prints out the sum of the positive values from the file. Assume that the file contains only numeric values. You must output an error if the file can't be opened. You must write the complete program and the output when the program runs successfully must conform exactly to the sample output. Bonus ( 5 pts). Output an error if the file contains non-numeric...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • java create java class "nameperson" the example of output would be this: //if the user inputs...

    java create java class "nameperson" the example of output would be this: //if the user inputs sophia 206999109 2 //then the output shows this My name is None and my studentid is 0 My name is sophia and studentid is 0 My name is sophia and studentid is 206999109 My grade is 2 so i have to create two classes   class nameperson{ } class studentid extends person{ } class grade extends studentid{ } my main method is this class Main...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • Predict the output of following Java program class T { int t= 20; T() { t...

    Predict the output of following Java program class T { int t= 20; T() { t = 40 } } class Main { public static void main(String args[]) {    T t1 = new T();    System.out.println(t1.t); } } a.20 b.Garbage value c.40 d.Compiler Error

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT