UQ Students should read the Disclaimer & Warning
Note: This page dates from 2005, and is kept for historical purposes.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>COMP2500 - Assignment One</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
.wrong {
background: #FF9999;
}
body {
background: url(_img/DSC04989.jpg) fixed center;
font-family: "Arial Unicode MS", Arial, Helvetica, sans-serif;
}
th, td, textarea {
border: 1px solid #000000;
padding: 0 1ex;
background: transparent;
overflow: hidden;
}
table {
border: none;
}
-->
</style>
</head>
<body>
<h1>COMP2500 – Programming in the Large – Assignment One</h1>
<table border="1" summary="Criteria and results achieved for COMP2500 assignment one">
<thead>
<tr>
<th colspan="3">Criteria and Results</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2">Average class result</td>
<td>10</td>
</tr>
</tfoot>
<tbody>
<tr>
<th colspan="3" scope="col">Adherence to code format rules (0-3)</th>
</tr>
<tr>
<td>no violation of code format rules</td>
<td>3</td>
<td>3 ✔</td>
</tr>
<tr>
<td>small number of minor violations</td>
<td>2</td>
<td> </td>
</tr>
<tr>
<td>significant number of violations</td>
<td>1</td>
<td> </td>
</tr>
<tr>
<td>work with little or no academic merit</td>
<td>0</td>
<td> </td>
</tr>
<tr>
<th colspan="3" scope="col">Quality of code (0-6)</th>
</tr>
<tr>
<td>code that is correct, clear and succint </td>
<td>6</td>
<td>6 ✔</td>
</tr>
<tr>
<td>code with small number of minor problems</td>
<td>4</td>
<td> </td>
</tr>
<tr>
<td>code that is clearly incorrect, too complex or hard to understand</td>
<td>2</td>
<td> </td>
</tr>
<tr>
<td>work with little or no academic merit</td>
<td>0</td>
<td> </td>
</tr>
<tr>
<th colspan="3" scope="col">Our testing of AssessmentOptions.java
(0-6)</th>
</tr>
<tr>
<td>no errors detected</td>
<td>6</td>
<td>6 ✔</td>
</tr>
<tr>
<td>one or two minor problems detected</td>
<td>4</td>
<td> </td>
</tr>
<tr>
<td>substantial number of problems detected</td>
<td>2</td>
<td> </td>
</tr>
<tr>
<td>work with little or no academic merit</td>
<td>0</td>
<td> </td>
</tr>
<tr>
<th scope="row">Total Possible Marks</th>
<td>15</td>
<td>15/15</td>
</tr>
</tbody>
</table>
<h2>Submitted Code</h2>
<p>View the <a href="COMP2500-assignment-1.javadoc/" title="JavaDoc documentation for this code">JavaDoc</a></p>
<p>
<textarea cols="80" rows="312" readonly="readonly" title="Java Code - Copyright 2003 Ned Martin">/*
* Author: Ned Martin #40529927
*
* Creation Date: 21-AUG-2003
*
* Copyright: Ned Martin
*
* Description: Assignment 1 for COMP2500
*
* Tab Stops: Eight spaces
*/
import java.io.*; // Provides standard IO
import java.util.*; // Provides StringTokenizer
import java.text.*; // Provides DecimalFormat
/**
* Accepts a student name and three results and formats and prints them.
*
* Input is assumed to be valid. Invalid input is not handled. Maximum of 50
* student records, minimum of 2 student records allowed
*
* @author Ned Martin
* @version Final 21-AUG-2003
*/
public class AssessmentOptions
{
/**
* Prints information about student results.
*
* Accepts input, formats appropriately and prints output
*
* @return None
* @param -i String input file
* @param -o String output file
*/
public static void main
(
String[] args
)
{
// initialising variables
String line, // holds input lines
inputFile = "", outputFile = "default.out";
boolean input = false, output = false;
// calculation variables
double sumA = 0.0, sumB = 0.0,
stdDevA = 0.0, stdDevB = 0.0,
meanA = 0.0, meanB = 0.0;
int numStudents = 0;
// holds individual student results
double[][] marks = new double [50][2];
// holds running totals, avoids useage of public vars
double[] totals = new double [3];
// holds student names
String[] students = new String [50];
// parse args if args exist
if (args.length >= 2)
{
// if input arg set input file
if (args[0].equals("-i"))
{
inputFile = args[1];
input = true;
}
// if output arg set output file
else if (args[0].equals("-o"))
{
outputFile = args[1];
output = true;
}
}
if (args.length == 4)
{
// if input arg set input file
if (args[2].equals("-i"))
{
inputFile = args[3];
input = true;
}
// if output arg set output file
else if (args[2].equals("-o"))
{
outputFile = args[3];
output = true;
}
}
try
{
// read input from file
if (input)
{
BufferedReader in = new BufferedReader(new
FileReader(inputFile));
// capture line
while ((line = in.readLine()) != null)
{
// send line to parser
parse(line, totals, marks, students);
}
in.close();
}
// read from standard in
else
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
// capture line
while ((line = in.readLine()) != null)
{
// send line to parser
parse(line, totals, marks, students);
}
}
numStudents = (int) totals[0];
// print student results array
for (int i = 0; i < numStudents ; i++ )
{
print(students[i]+" "
+format(marks[i][0])+" "
+format(marks[i][1]), outputFile,
output, i);
}
// calculate means
meanA = totals[1]/totals[0];
meanB = totals[2]/totals[0];
// calculate standard deviations
stdDevA = stdDev(marks, meanA, numStudents, 0);
stdDevB = stdDev(marks, meanB, numStudents, 1);
// print means
print("mean "+format(meanA)+" "+format(meanB),
outputFile, output, 1);
// print standard deviations
print("std dev "+format(stdDevA)+" "
+format(stdDevB), outputFile, output, 1);
}
// catch exceptions
catch (IOException e)
{
System.err.println("IO Exception: Check input\n"+e);
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.err.println("Array out of bounds exception:"
+" Check input\n"+e);
System.exit(1);
}
catch (NoSuchElementException e)
{
System.err.println("No such element exception:"
+" Check input\n"+e);
System.exit(1);
}
catch (NumberFormatException e)
{
System.err.println("Number format exception:"
+" Check input\n"+e);
System.exit(1);
}
}
/**
* Calculates standard deviation
*
* Accepts numerical input and calculates standard deviation
*
* @return Double representing standard deviation of input values
*/
private static double stdDev
(
double[][] marks, // array of student's results
double mean, // average of results
int numStudents, // number of students
int x // mean type selector
)
{
double stdDev = 0.0;
// calculate standard deviation
for (int i = 0; i < numStudents ; i++ )
{
stdDev = stdDev + ( (marks[i][x] - mean) *
(marks[i][x] - mean));
}
// return standard deviation
return Math.sqrt(stdDev / (double) (numStudents - 1) );
}
/**
* Format a number.
*
* Accepts a double and returns a formatted string
*
* @return String representing formatted double
*/
private static String format
(
double number // number to format
)
{
// format number
DecimalFormat df = new DecimalFormat("0.00");
// return number
return df.format(number);
}
/**
* Parse input.
*
* Accepts input and parses input into various arrays
*
* @return None, however various arrays are altered by this
* function
*/
private static void parse
(
String line, // input line to parse
double[] totals, // increment running totals
double[][] marks, // store student results
String[] students // store student names
)
{
double numStudents = totals[0] + 1;
StringTokenizer st = new StringTokenizer(line);
// assume exact string format "name int int int"
students[(int) numStudents-1] = st.nextToken();
// parse input line and calulate values
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
double d = a+b+c- Math.min(Math.min(a,b),Math.min(b,c));
double e = (double) ((a+b+c)/3.0)*2;
totals[0] = numStudents;
totals[1] = totals[1] + d; // setting sumA
totals[2] = totals[2] + e; // setting sumB
marks[(int) numStudents-1][0] = d; // method 1
marks[(int) numStudents-1][1] = e; // method 2
}
/**
* Prints data.
*
* Accepts input from main function and prints it accordingly
*
* @return None
*/
private static void print
(
String line, // line to print
String outputFile, // file to print to
boolean output, // if true, prints to file
int overwrite // specifies whether to overwrite
)
{
boolean append = true;
try
{
// print to file
if (output)
{
// overwrite only on first print
if (overwrite == 0)
{
append = false;
}
BufferedWriter out = new BufferedWriter(new
FileWriter(outputFile, append));
out.write(line+"\n");
out.close();
}
// print to standard out
else
{
System.out.println(line);
}
}
// catch exceptions
catch (IOException e)
{
System.err.println("IO Exception:"
+" Check output file\n"+e);
System.exit(1);
}
}
}
</textarea>
<br />
Code © Copyright 2003 Ned Martin</p>
<p>09-Sep-2003</p>
</body>
</html>