Java Topics Java I/O, while loops, if-else-if, methods, summing/averaging data.
ID: 3592081 • Letter: J
Question
Java Topics Java I/O, while loops, if-else-if, methods, summing/averaging data.
The programming standards and guidelines as discussed in class.
Use of leftPad and padString in the Toolkit class.
References Textbook – use the index for relevant topics.
Your code from Assignment 4 for I/O
Toolkit.java
Specification
Write a Java program that reads from one file and writes to a second file. The files are named according to the naming convention we have been following using your name, section number and assignment number. The program processes input lines until the end of file. Read and process the input file in one pass through of the file, as should be done in all programs unless specified otherwise. All output should be to one file. Each line in the input file contains an integer followed by a name as shown below. Use a String to store the name. Do NOT use an array. Do NOT use global (non-local) variables except for the Toolkit. The main method should contain all the variables. Do NOT use a separate class except for the Toolkit.
Create a printed report that consists of a table with appropriate headings. The columns of the report contain the name, value, and then a message in that order. The message is “OUTSTANDING” if the value is 90 or more; “Satisfactory” if the value is between 70 and 89 inclusive; “FAILING” otherwise. Use "if/else/if" or a table-driven approach. Use methods to line up the columns of the report. Use the padString method to output the name (a string) to a specified width, and a leftPad method for the value. Use Toolkit for the leftPad and padString methods.
After all the data lines have been processed, print with messages the number of data lines processed, the number and real average of the values between 70 and 89 inclusive and the average of all grades. The average is formatted to one decimal place.
Follow the documentation guidelines. Methods must be used at least for the heading of the table and for the summary of the table.
Data to use in this order:
------------------------------------
70 Light Karen L
99 Fagan Bert Todd
60 Antrim Forrest N
73 Camden Warren
80 Mulicka Al B
99 Lee Phoebe
75 Bright Harry
92 Garris Ted
83 Benson Martyne
71 Lloyd Jeanine D
43 Leslie Bennie A
40 Brandt Leslie
100 Schulman David
51 Worthington Dan
60 Hall Gus W
60 Prigeon Dale R
96 Fitzgibbons Rusty
Instead of typing in the above data lines, you can copy them from this assignment file to a text file using Notepad (PC) or textEdit (Mac). Do not put the table heading in the input file. Using the variable “inputFile” below to represent the input file, you can input each pair of data values as follows:
while (inputFile.hasNext()) {
grade = inputFile.nextInt(); // On the first row, reads 70 into variable ‘grade’
name = inputFile.nextLine(); // Reads “ Light Karen L ” into variable ‘name’
name = name.trim(); // Trims leading and trailing spaces, leaving
// “Light Karen L” in the variable ‘name’
// Process the information
}
____________________________________________________________________
Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class Grades_3_08 {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
// Define file names
final String INPUT_FILE = "Grades_3_08_Input.txt";
final String OUTPUT_FILE = "Grades_3_08_Output.txt";
int grade;
String name = " ";
double value = 0;
String msg = " ";
String line = " ";
// Access the input/output files
File inputDataFile = new File(INPUT_FILE);
Scanner inputFile = new Scanner(inputDataFile);
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
PrintWriter outputFile = new PrintWriter(outputDataFile);
System.out.println("Reading file " + INPUT_FILE + " " +
"Creating file " + OUTPUT_FILE);
// Read all of the values from the file
while (inputFile.hasNext() ) {
String lines=inputFile.nextLine();
String[] words=lines.split(" ");
grade=Integer.parseInt(words[0]);
name=words[1];
name = name.trim();
} // End while
if(value >= 90)
{
msg = "OUTSTANDING";
}
else if (value >= 70)
{
msg = "Satisfactory";
}
if(value >= 90){
msg = "OUTSTANDING";
}else{
if(value >= 70){
msg = "Satisfactory";
}else
msg = "FAILING";
}
outputFile.println(value + " " + name + " " + msg);
outputFile.println("processed names");
outputFile.println("between 70 and 89 inclusive");
outputFile.close();
} // End outputResults
} // End class
This is the input file:
Data to use in this order:
------------------------------------
70 Light Karen L
99 Fagan Bert Todd
60 Antrim Forrest N
73 Camden Warren
80 Mulicka Al B
99 Lee Phoebe
75 Bright Harry
92 Garris Ted
83 Benson Martyne
71 Lloyd Jeanine D
43 Leslie Bennie A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.