SpreadsheetWriter Let\'s write a program that will produce a file that can be op
ID: 1996177 • Letter: S
Question
SpreadsheetWriter Let's write a program that will produce a file that can be opened by Excel or LibreOffice Calc. Go to Getting Started and look up the grading weights used for this course. As an example, we'll use the following table, but you should use the values you find in Getting Started. Activity Weighting % Labs 20 Reading Quizzes 10 Group Work 10 Midterm 20 Project 15 Final 20 Instructor Discretion 5 Write a class SSRow. Objects of this class should keep track of String name, int score, int weight, String calc. SSRow also has a toString method that returns this data in order, separated by commas. name + "," + score + "," + weight + "," + calc Further design of the SSRow class is left to you. Add what you need, but don't get carried away. Write a class SSWriter that has a main method. For each of the grading categories (Lab, Reading Quizzes, Group Work, Midterm, Project, Final, Instructor Discretion), query the user for a score, and create the following SSRow objects: "Lab", , 20, "=B1*C1/100" "Reading Quizzes", , 10, "=B2*C2/100" "Group Work", , 10, "=B3*C3/100" "Midterm", , 20, "=B4*C4/100" "Project", , 15, "=B5*C5/100" "Final", , 20, "=B6*C6/100" "Instructor Discretion", , 5, "=B7*C7/100" Open a file for output with a file type of csv (say MyGrades.csv, for example). Write the toString value for each SSRow to the file. Finally, write a row "average",=(D1+D2+D3+D4+D5+D6+D7) Open MyGrades.csv with Excel or LibreOffice Calc. Hint: You can simplify writing this program greatly by using the tools we've covered.
Explanation / Answer
=====SSRow=====
public class SSRow {
private String name;
private int score;
private int weight;
private String calc;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getCalc() {
return calc;
}
public void setCalc(String calc) {
this.calc = calc;
}
@Override
public String toString() {
return name + "," + score + "," + weight + "," + calc ;
}
}
======SSWriter===========
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class SSwriter {
public static void main(String args[]) throws IOException {
if (args.length != 1) {
System.out.println("Must have exactly 1 command line parameter that is a file name.");
System.exit(4);
}
Map<String, Integer> map = new HashMap<String, Integer>();
List<SSRow> listOfObj = new ArrayList<SSRow>();
map.put("Lab", 20);
map.put("Reading Quizzes", 10);
map.put("Group Work", 10);
map.put("Midterm", 20);
map.put("Project", 15);
map.put("Final", 20);
map.put("Instructor Discretion", 5);
Scanner scanner = new Scanner(System.in);
Iterator entries = map.entrySet().iterator();
int score;
SSRow obj = null;
int i = 1;
/*
* // Writing file: FileWriter fileWriter = new FileWriter(args[0]); for
* (int j = 0; j < listOfObj.size(); j++) {
* fileWriter.append(listOfObj.get(j).toString() + " "); }
* fileWriter.append("Average" + "," + "=D1+D2+D3+D4+D5+D6+D7");
* fileWriter.close(); System.out.println("Written to "+args[0]);
*/
String filename = null;
if (args.length != 1) {
System.out.println("Must have exactly 1 command line parameter that is a file name");
System.exit(4);
}
Scanner scan = new Scanner(System.in);
filename = args[0];
File f = new File(filename);
PrintWriter pw = null;
try {
pw = new PrintWriter(f);
String in = scan.nextLine();
while (!"quit".equals(in) && entries.hasNext()) {
obj = new SSRow();
Entry thisEntry = (Entry) entries.next();
obj.setName(thisEntry.getKey().toString());
obj.setWeight(Integer.parseInt(thisEntry.getValue().toString()));
System.out.println("Enter Score for:" + thisEntry.getKey().toString());
score = scanner.nextInt();
obj.setScore(score);
obj.setCalc("=B" + i + "*C" + i + "/100");
i++;
pw.println(obj.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (pw != null){
pw.println("Average" + "," + "=D1+D2+D3+D4+D5+D6+D7");
pw.close();
}
scan.close();
}
}
}
====O/P=====
Midterm,78,20,=B1*C1/100
Project,79,15,=B2*C2/100
Reading Quizzes,79,10,=B3*C3/100
Instructor Discretion,78,5,=B4*C4/100
Lab,70,20,=B5*C5/100
Group Work,72,10,=B6*C6/100
Final,75,20,=B7*C7/100
Average,=D1+D2+D3+D4+D5+D6+D7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.