Write a class that reads data from 'Grades.txt' calculates average grade and output the data t 'Grades_average.txt'in JAVA
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
SCREEN SHOT OUTPUT
Write a class that reads data from 'Grades.txt' calculates average grade and output the data t...
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...
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 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 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 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...
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...
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
Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya import java.util.Scanner; public class SpaceReplace { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); String firstName; String lastName; //answer goes here// } }
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...