Question
A USEFUL JAVA PROGRAMMING TO HELP(SIMILAR TO THIS ASSIGNMENT) import java.text.*; import java.util.*; import java.io.*; public class Assignment1 { public static void main(String[] args) throws
A USEFUL JAVA PROGRAMMING TO HELP(SIMILAR TO THIS ASSIGNMENT)
import java.text.*;
import java.util.*;
import java.io.*;
public class Assignment1 {
public static void main(String[] args) throws Exception {
// ****************************************************************************************************
// * Step 1: Scan the file, capture the data into arrays, find the number of students and assessments *
// ****************************************************************************************************
String hdrStr, hdrData[], stuStr[], stuData[][], assessments[];
double weightings[], overall[], average[], overallAvg;
String separatorSymbol = "###;###";
String separatorOut= " ";
intnumOfStudents = 0, numOfAssessments = 0;
String line = "", tmpStr = "";
hdrData = new String[1];// This statement is dummy, hdrData is to be replaced
// Get the file and open by Scanner
File file = new File("asg1_sample_input.txt");
Scanner input = new Scanner(file);
// Read the file
// for row 0, store in hdrStr, then split and store in hdrData[]
// for the rest of file, append and store in tmpStr with suffix "###;###"
for (int i=0; input.hasNextLine(); i++) {
line = input.nextLine();
if (i == 0) {// header row
hdrStr = line;
hdrData = hdrStr.split(",");
numOfAssessments = (hdrData.length - 2) / 2;// -X-ID-X-, -X-Name-X-, Assessment1, weighting1, Assessment2, weighting2, ...
}
else {// student data block
tmpStr = tmpStr + line + separatorSymbol;
}
}
// Split tmpStr by symbol "###;###", store in stuStr[]
stuStr = tmpStr.split(separatorSymbol);
numOfStudents = stuStr.length;
// Split each row of student data, store in stuData[][]
stuData = new String[numOfStudents][numOfAssessments+2];
for (int i=0; i stuData[i] = stuStr[i].split(","); } assessments = new String[numOfAssessments]; weightings= new double[numOfAssessments]; overall= new double[numOfStudents]; average= new double[numOfAssessments]; overallAvg= 0; for (int i=2, j=0; i assessments[j] = hdrData[i].trim(); weightings[j]= Double.parseDouble(hdrData[i+1]); System.out.println("!!! " + weightings[j]);// debug message!!! j++; } for (int i=0; i overall[i] = 0; for (int j=0; j overall[i] += Double.parseDouble(stuData[i][j+2]) * weightings[j]; System.out.println(stuData[i][j+2] + " * " + weightings[j]);// debug message!!! } overall[i] /= 100; overallAvg += overall[i]; } overallAvg /= numOfStudents; for (int j=0; j average[j] = 0; for (int i=0; i average[j] += Double.parseDouble(stuData[i][j+2]); } average[j] /= numOfStudents; } // **************************************************************************************************** // * Step 3: Display data in table format* // **************************************************************************************************** int reqColumnWidth[] = new int[numOfAssessments+3]; String outStr[]= new String [numOfStudents+2]; DecimalFormat df = new DecimalFormat("#0.00"); reqColumnWidth[0] = 8;// ID reqColumnWidth[1] = 20;// Name for (int j=0; j reqColumnWidth[j+2] = (assessments[j].length()>6)?assessments[j].length():6; } reqColumnWidth[numOfAssessments+2] = 7;// Overall // Header row outStr[0] = pad("ID", "L", reqColumnWidth[0]) + separatorOut + pad("Name", "L", reqColumnWidth[1]) + separatorOut; for (int j=0; j outStr[0] += pad(assessments[j], "L", reqColumnWidth[j+2])+ separatorOut; } outStr[0] += "Overall"; // Student data for (int i=0; i outStr[i+1] = ""; for (int j=0; j if (j>1) {// marks outStr[i+1] += pad(df.format(Double.parseDouble(stuData[i][j])), "L", reqColumnWidth[j]) + separatorOut; } else { outStr[i+1] += pad(stuData[i][j].trim(), "L", reqColumnWidth[j]) + separatorOut; } } outStr[i+1] += pad(df.format(overall[i]), "L", reqColumnWidth[numOfAssessments+2]); } // Average row outStr[outStr.length - 1] = pad("Average:", "R", reqColumnWidth[0] + separatorOut.length() + reqColumnWidth[1]) + separatorOut; for (int j=0; j outStr[outStr.length - 1] += pad(df.format(average[j]), "L", reqColumnWidth[j+2]) + separatorOut; } outStr[outStr.length - 1] += pad(df.format(overallAvg), "L", reqColumnWidth[numOfAssessments+2]); // Print data for (int i=0; i System.out.println(outStr[i]); } } // method name : pad() // return type : String // input parameters: //1. text: String- the original text string //2. alignment: String- l(eft), r(ight), or c(enter/entre) //3. length: int- number of output characters public static String pad(String text, String alignment, int length) { String lpad, rpad; lpad = rpad = ""; switch (alignment.toLowerCase()) { case "r": case "right": for (int i=0; i lpad += " "; } break; case "c": case "center": case "centre": for (int i=0; i lpad += " "; rpad += " "; } break; case "L": case "left": default: for (int i=0; i rpad += " "; } break; } return (lpad + text + rpad + " ").substring(0, length); } } INPUT TEXT TO BE PUT IN: [Student ID], [Student Name], Asg 1, 10, Asg 2, 10, Quiz 1, 15, Quiz 2, 15, Project, 35, Lab, 15 20000001, Matthew, 93.33, 100.00, 100.00, 100.00, 100.00, 95.00 20000002, Sing, 86.67, 100.00, 100.00, 100.00, 100.00, 95.00 20000003, Kenneth, 78.67, 100.00, 100.00, 100.00, 100.00, 100.00 20000004, Weber, 69.33, 100.00, 100.00, 100.00, 100.00, 100.00 20000005, Charles, 64.00, 100.00, 100.00, 100.00, 100.00, 100.00 20000006, Fun, 85.33, 100.00, 95.00, 100.00, 100.00, 100.00 20000007, Tany, 78.67, 100.00, 95.00, 100.00, 100.00, 100.00 20000008, Aaron, 76.00, 100.00, 0.00, 100.00, 100.00, 100.00 20000009, Ellen, 69.33, 100.00, 100.00, 100.00, 100.00, 100.00 20000010, Bennalt, 62.67, 100.00, 95.00, 100.00, 100.00, 100.00 20000011, Andy, 62.67, 100.00, 100.00, 100.00, 0.00, 100.00 20000012, Samuel, 64.00, 100.00, 60.00, 60.00, 100.00, 100.00 20000013, Tammy, 0.00, 100.00, 95.00, 100.00, 100.00, 100.00
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started