// ExamScores1B.java
// Given an input text file ExamScores.txt
// containing exam scores, generates text file
// Curve.txt containing curved scores.
//
// Observe that this version simply assumes
// that the array is big enough and does not
// test this.
//
// Follows "Write everything once!" rule
// (only one readLine statement), using
// break statement.
public class ExamScores1B {
public static void main(String[] args)
{
final int MAX_NUMBER_OF_SCORES = 40;
final int columnWidth = 5;
final String inputFileName = "ExamScores.txt";
final String outputFileName = "Curve.txt";
TextFileInput in = new TextFileInput(inputFileName);
TextFileOutput out = new TextFileOutput(outputFileName);
// Read scores and determine maximum:
int[] scores = new int[MAX_NUMBER_OF_SCORES];
int maximumScore = 0;
int lengthFilled = 0;
while ( true )
{
String line = in.readLine();
if ( line == null )
break;
scores[lengthFilled] = Integer.parseInt(line);
maximumScore = Math.max(maximumScore, scores[lengthFilled]);
lengthFilled++;
} // while
// Determine how many points to curve up all scores:
int curveUp = 100 - maximumScore;
// Print original scores and curved scores
// to output text file:
for ( int i = 0; i < lengthFilled; i++ )
{
// Print original score in a right-justified column:
String scoreText = Integer.toString(scores[i]);
for ( int j = scoreText.length(); j < columnWidth; j++ )
out.print(" ");
out.print(scoreText);
// Calculate curved score;
int curvedScore = scores[i] + curveUp;
// Print curved score in a right-justified column;
String curvedScoreText = Integer.toString(curvedScore);
for ( int k = curvedScoreText.length(); k < columnWidth; k++ )
out.print(" ");
out.println(curvedScoreText);
} // for i
// Make sure the output finds its way to the file:
out.flush();
// Release files for re-use:
out.close();
in.close();
// Print message indicating successful completion:
System.out.println("File " + outputFileName
+ " has been created.");
} // method main(String[])
} // class ExamScores1B