In java only please A spreadsheet file can be stored as a comma separated file.
ID: 3572037 • Letter: I
Question
In java only please
A spreadsheet file can be stored as a comma separated file. For instance if a file called data.csv had the following in it: name, quiz1, quiz2, quiz3, hw1, hw2, hw3, midterm, final bill,8,9,10,50,45,43,88,93 jill,9,9,10,50,48,40,90,91 gill,6,5,7,38,22,40,70,73 will,8,9,9,40,45,43,83,94 totals,10,10,10,50,50,50,100,100 It would display in a program like Microsoft excel appropriately. a) create a file called data.csv with this data on your hard drive. b) read the file into a program calculating each students final percentage. c) have the program write a new file with an extra column containing the final percentage for each student, a column heading for the first row, and the last row can end with 100 for its percentage
Explanation / Answer
import java.util.*;
import java.io.*;
public class SpreadSheet
{
public static void main(String s[]) throws Exception
{
// to read file
Scanner sc = new Scanner(new File("results.txt"));
// to write to file
BufferedWriter fw = new BufferedWriter(new FileWriter("spreadsheet.txt", false));
String name, r;
float q1,q2,q3,h1,h2,h3,mt,f,p;
int q1m=10, q2m=10,q3m=10, h1m=50,h2m=50,h3m=50,mtm=100,fm=100;
String words[];
fw.write(sc.nextLine());
while(sc.hasNext())
{
// read each line
r=sc.next();
// break line into words array
words = r.split(",");
//if
name=words[0];
q1=Float.parseFloat(words[1]);
q2=Float.parseFloat(words[2]);
q3=Float.parseFloat(words[3]);
h1=Float.parseFloat(words[4]);
h2=Float.parseFloat(words[5]);
h3=Float.parseFloat(words[6]);
mt=Float.parseFloat(words[7]);
f=Float.parseFloat(words[8]);
p=((q1*100/q1m)+(q2*100/q2m)+(q3*100/q3m)+(h1*100/h1m)+(h2m*100/h2m)+(h3*100/h3m)+(mt*100/mtm)+(f*100/fm))/8;
// write word to file
fw.write(" "+name+","+q1+","+q2+","+q3+","+h1+","+h2+","+h3+","+mt+","+f+","+p);
System.out.print(" "+name+","+q1+","+q2+","+q3+","+h1+","+h2+","+h3+","+mt+","+f+","+p);
//fw.newLine();
System.out.println(" ");
}
fw.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.