IN JAVA LANGUGE: A spreadsheet file can be stored as a comma separated file. For
ID: 3770941 • Letter: I
Question
IN JAVA LANGUGE:
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
The required solution is as givem below,
A student class being created to support.
////Student.java
package simulation.csvReader;
import java.util.List;
public class Student{
private String name;
private double quiz1;
private double quiz2;
private double quiz3;
private double hw1;
private double hw2;
private double hw3;
private double midterm;
private double finalAmnt;
public Student(String name, double quiz1, double quiz2,
double quiz3,
double hw1, double hw2, double hw3,
double midterm, double finalAmnt){
this.setName(name);
this.quiz1 = quiz1;
this.quiz2 = quiz2;
this.quiz3 = quiz3;
this.hw1 = hw1;
this.hw2 = hw2;
this.hw3 = hw3;
this.setMidterm(midterm);
this.setFinalAmnt(finalAmnt);
}
public double getFinalPercentage(double total){
double per = ((this.quiz1+this.quiz2+this.quiz3+this.hw1+this.hw2+this.hw3)/total)/100;
return per;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setFinalAmnt(double finalAmnt) {
this.finalAmnt = finalAmnt;
}
public double getFinalAmnt() {
return finalAmnt;
}
public void setMidterm(double midterm) {
this.midterm = midterm;
}
public double getMidterm() {
return midterm;
}
public double getQuiz1() {
return this.quiz1;
}
public double getQuiz2() {
return this.quiz1;
}
public double getQuiz3() {
return this.quiz1;
}
public double getHw1() {
return this.hw1;
}
public double getHw2() {
return this.hw2;
}
public double getHw3() {
return this.hw3;
}
}
////Reader.java
package simulation.csvReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class Reader {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String splitBy = ",";
try {
BufferedReader br = new BufferedReader(new FileReader("data.csv"));
String line = null;
List<Student> studentList = new ArrayList<Student>();
List<Double> totalNums = new ArrayList<Double>();
while((line = br.readLine()) != null){
String[] studentDetails = line.split(splitBy);
if(studentDetails[0] != "name"){
if(studentDetails[0] == "totals"){
totalNums.add(Double.parseDouble(studentDetails[1]));
totalNums.add(Double.parseDouble(studentDetails[2]));
totalNums.add(Double.parseDouble(studentDetails[3]));
totalNums.add(Double.parseDouble(studentDetails[4]));
totalNums.add(Double.parseDouble(studentDetails[5]));
totalNums.add(Double.parseDouble(studentDetails[6]));
totalNums.add(Double.parseDouble(studentDetails[7]));
totalNums.add(Double.parseDouble(studentDetails[8]));
}
else{
Student stu = new Student(
studentDetails[0],
Double.parseDouble(studentDetails[1]),
Double.parseDouble(studentDetails[2]),
Double.parseDouble(studentDetails[3]),
Double.parseDouble(studentDetails[4]),
Double.parseDouble(studentDetails[5]),
Double.parseDouble(studentDetails[6]),
Double.parseDouble(studentDetails[7]),
Double.parseDouble(studentDetails[8])
);
studentList.add(stu);
}
}
}
double total = 0;
for(double num : this.totalNumbers){
total += num;
}
PrintWriter writer = new PrintWriter(new File("out.csv"));
StringBuilder builder = new StringBuilder();
builder.append("name");
builder.append(",");
builder.append("quiz1");
builder.append(",");
builder.append("quiz2");
builder.append(",");
builder.append("quiz3");
builder.append(",");
builder.append("hw1");
builder.append(",");
builder.append("hw2");
builder.append(",");
builder.append("hw3");
builder.append("midterm");
builder.append(",");
builder.append("final");
builder.append(",");
builder.append("final Percentage ");
for(Student student : studentList){
double per = student.getFinalPercentage(total);
builder.append(student.getName());
builder.append(",");
builder.append(student.getQuiz1());
builder.append(",");
builder.append(student.getQuiz2());
builder.append(",");
builder.append(student.getQuiz3());
builder.append(",");
builder.append(student.getHw1());
builder.append(",");
builder.append(student.getHw2());
builder.append(",");
builder.append(student.getHw3());
builder.append(student.getMidterm());
builder.append(",");
builder.append(student.getFinalAmnt());
builder.append(",");
builder.append(per);
builder.append(" ");
}
builder.append("totals");
builder.append(",");
builder.append("10");
builder.append(",");
builder.append("10");
builder.append(",");
builder.append("10");
builder.append(",");
builder.append("50");
builder.append(",");
builder.append("50");
builder.append(",");
builder.append("50");
builder.append("100");
builder.append(",");
builder.append("100");
builder.append(",");
builder.append("100 ");
writer.print(builder.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}