Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedimage text in transcribed
Assumptions Your program will process one file for each run. Each file contains only one course information. Rank the students' result within a course. The length of student id is fixed at 8 characters long. 1 2 3 4. There is no comma in between surname and first name. 5 6 The length of student name is fixed at 20 characters long. Based on your assignment 1, you are asked to rewrite the Java program with Object-oriented programming. The program should be consisted of 3 classes (Student, Course and a driver class that contains the main() method). The program should also implement and use the compareTol) method for interface class Comparable. Use a class diagram to show the relationship of your classes. Input File The input le format will be the same as the one in assignment 1. A general format of input text le depicts below: Student to, Student name, marks for assignment 1, percentage of assignment 1, marks for assignment 2, percentage of assignment 2 Example, [Student ID], [Student Name}, Asg l, 15, Asg 2, 20, Quiz, 35, Project, 30 01234567, Samuel Lai, 99.5, 97, l00.0, 99.0 02345678, Tommy Ng, 89.5, 88.5, 99.0, 100 The rst row denotes the names of all assessments of one course. Then, each student record presents in a line. All elds are separated by commas. Output Screen Here is a sample output from the input file shown above, similar format as in the assignment 1. The Rank column is the ranking of each student according to the Overall score. The output should be sorted by the rank in ascending order. ID Name Asg l Asg 2 Quiz Project Overall Rank 01234567 Samuel Lai 99.50 97.00 100.00 99.00 99.03 1 02345678 Tommy Ng 89.50 88.50 99.00 100.00 95.78 2 Average: 94.50 92.75 99.50 99.50 97.40 Your program should be able to process as many as possible students' data, which are all contained in one input le as shown in the example. In addition to output student records, your program should calculate the averages for each individual assessment. Please be reminded that all numbers except the rank are displayed with 2 decimal places. \f

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions