In: Computer Science
Description
This assignment is a modification of the last assignment. In this assignment, you will input from a file called in.txt and output to a file called out.txt. Otherwise, the assignment will behave the same way as the last assignment.
For doing this assignment, the statement doing input and output will be changed. Otherwise, the code will mostly remain unchanged.
Creating Text IO Files
In Eclipse, using the file menu, create the input file in.txt in project folder (not in package folder or source folder). The output file out.txt will be automatically created
in project folder (not in package folder or source folder) when you write to that file.
To make the file name out.txt show up in the package view in the left pane after program execution do the following:
Click on the project name in the package view in the left pane.
Click F5 to refresh contents.
The file name out.txt will show up in above package view if your program created it during execution.
Double Click in the file name out.txt to display its contents in Eclipse
Testing
Input
The in.txt file should contain the following:
15
1, John Adam, 3, 93, 91, 100, Letter
2, Raymond Woo, 3, 65, 68, 63, Letter
3, Rick Smith, 3, 50, 58, 53, Letter
4, Ray Bartlett, 3, 62, 64, 69, Letter
5, Mary Russell, 3, 93, 90, 98, Letter
6, Andy Wong, 3, 89,88,84, Letter
7, Jay Russell, 3, 71,73,78, Letter
8, Jimmie Wong, 3, 70,77,72, Letter
9, Jackie Chan, 3, 85,89,84, Letter
10, Susan Wu, 3, 80,88,84, Letter
11, Bruce Lee, 4, 74, 79, 72, 75, Credit
12, Chuck Norris, 5, 63, 64, 62, 60, 68, Credit
13, Jet Li, 3, 85, 83, 89, Credit
14, Jessica Lauser, 3, 82, 84, 87, Letter
15, Mahnoosh Nik-Ahd, 2, 98, 99, Letter
In the above data, in the first line, 15 is the number of students.
Each subsequent line contains one student data.
For example, the second line contains the first student data where: 1 is id, John Adam is name, 3 is the number of exxam taken, 93, 91, 100 are individual exxam scores and Letter is grade type.
Output
After running the program, out.txt file should contain the following:
1 John Adam (A)
5 Mary Russell (A)
15 Mahnoosh Nik-Ahd (A)
6 Andy Wong (B)
9 Jackie Chan (B)
10 Susan Wu (B)
14 Jessica Lauser (B)
7 Jay Russell (C)
8 Jimmie Wong (C)
2 Raymond Woo (D)
4 Ray Bartlett (D)
3 Rick Smith (F)
11 Bruce Lee (CR)
13 Jet Li (CR)
12 Chuck Norris (NCR)
Submit
Copy the following in a file and submit that file:
The contents of in.txt and out.txt files
The source code from all the source java file
Sample Code
//Sample code for the method main in class TestStudentExt
//The sample code below input student data from file “in.txt”
//and output student data in file “out.txt”
//Write the missing code
import java.io.*;
import javax.swing.*;
public class TestStudentExt
{
/*
To make File IO compile, add the phrase: throws Exception
at the end of the method header of the method in which do file IO.
Below, we do File IO in method main, so we added the phrase: throws Exception
*/
public static void main(String[] args) throws Exception
{
String in, outAll, line;
int studentCount;
//Create a BufferedReader object for inputting from a file in.txt
BufferedReader br = new BufferedReader(new FileReader("in.txt"));
//Create a PrintWriter object for outputting to a file out.txt.
PrintWriter pw = new PrintWriter (new FileWriter("out.txt"));
//input the first line of the file containing the number a number that specifies the number of students
in = br.readLine();
studentCount = Integer.parseInt(in);
//Set up a for loop that, in each pass through the loop, will input one student data, tokenize the data,
//and create the corresponding StudentExt object
for (int i=0; i<studentCount; i++)
{
//read one line containing one student data.
in = br.readLine();
//Create a StringTokenizer object to tokenize one student data.
//create the corresponding StudentExt object
}
//String variables for accumulating output lines
String outA="", outB="", outC="", outD="", outF="", outCr="", outNcr="";
String outAll;
//Set up a for loop that, in each pass through the loop, will find grade of one student data,
//and depending on the grade, add a line of output to the corresponding output variable
for (int i=0; i<studentCount; i++)
{
//Find one student grade
//Depending on the grade, accumulate a line of output to the corresponding String variable
}
//Concatenate all student output in a single String (say outAll)
outAll=outA+outB+outC+outD+outF+outCr+outNcr;
//Output outAll String to the the out using PrintWriter object.
//make sure to also call flush( ) after calling println()
pw.println(outAll);
pw.flush();
//Call close on File IO objects.
if (br != null)
br.close();
if (pw != null)
pw.close();
}
}
Discussion
Creating File IO Objects
For doing this assignment, you will create a BufferedReader object to input data from the text file line at a time. Also, you will create a PrintWriter object to write data to the file a String at a time.
Getting Student Count
It is suggested that you put student count as the first line of data in the input file.
Methods for Finding Student Count
Different methods of determining the total number of students are described below.
Prompt the User for the Count
Prompt the user for entering the total number of students in the file using JOptionPane.showInputDialog.
Put the Count in the File
Put the total number of students as the first line of data in the input file. Input this value from the file and create a StudentExt array of that size. Then input and process student data from the file.
It is suggested that you use this method as the sample code uses it.
Read File Twice
Create a file io object encapsulating the input file. Input data from the file line by line using the file io object. Keep a count of the number of lines read. Issue a close on the file io object. Create an array of StudentExt references of size above. Create a file io object encapsulating the input file a second time. Input data from the file line at a time. Each line will contain one student data. For each line read, create a StudentExt object and save its reference in the array created above. After all data is read and StudentExt objects are created, issue a close on the file io object. This will result in closing the file.
Here are the steps in detail.
· Create the BufferedReader object (encapsulating the input file).
· Input the file line by line using the BufferReader object. Keep a count of the number of lines read.
· Close the BufferReader object.
· Create an array of StudentExt references of size equal to count determined above.
· Create the BufferedReader object (encapsulating the input file) a second time. (This will put the read pointer at the start of the file).
· Read the file one line at a time. Each line contains one student data.
· Create a StudentExt object according to student data read.
· Save its reference in the array above.
· When all student data is read, close the BufferedReader object.
Ignore the following discussion (Not Needed)
Sample Code for CopyFile Program
You don’t need to use this code for doing this assignment. This is presented here merely as a use case for file IO.
The code below reads from one text file and writes it to another text file line at a time.
//Create a BufferedReader object for reading lines
// from the text file in.txt located in current directory.
BufferedReader br = new BufferedReader ( new FileReader( "in.txt" ) );
//create a PrinterWriter object for writing lines
//to the text file out.txt located in current directory.
PrintWriter pw =new PrintWriter ( new FileWriter( "out.txt" ) );
//Set up a read/write loop
//In this loop, you will read a line from the file
//using BufferedReader object
//and write the same line using the PrintWriter object.
//read a line (If there is no line left, readLine will return a null,
//other wise it will return a String containing the line).
line = br.readLine();
while (line != null)
{
//Write the line to the output file.
//Flush the line to send it to the file immediately.
//Otherwise, it will go to the output buffer
//and will go to the file when you issue close.
pw.println(line);
pw.flush();
//read the next line.
line = br.readLine();
}
//close the files.
//Closing the PrintWriter object will also cause a flush.
//It will write to the file any lines that may still
//be sitting in the output buffer.
if (br != null)
br.close();
if (pw != null)
pw.close();
}
Solution:
Givendata:
This assignment is a modification of the last assignment. In this assignment, you will input from a file called in.txt and output to a file called out.txt. Otherwise, the assignment will behave the same way as the last assignment.
For doing this assignment, the statement doing input and output will be changed. Otherwise, the code will mostly remain unchanged.
Creating Text IO Files
In Eclipse, using the file menu, create the input file in.txt in project folder (not in package folder or source folder). The output file out.txt will be automatically created
in project folder (not in package folder or source folder) when you write to that file.
To make the file name out.txt show up in the package view in the left pane after program execution do the following:
Click on the project name in the package view in the left pane.
Click F5 to refresh contents.
Answer:
//TestStudentExt.java//
import java.io.*;
import java.util.StringTokenizer;
public class TestStudentExt
{
public static void main(String[] args) throws Exception
{
String in, out,line;
int studentCount;
String name, grade, gradeType, token;
int id, exams;
int[] scores;
BufferedReader br = new BufferedReader(new
FileReader("in.txt"));
PrintWriter pw = new PrintWriter (new FileWriter("out.txt"));
in = br.readLine();
studentCount = Integer.parseInt(in);
StudentExt[] students = new StudentExt[studentCount];
out = "Student report:\n";
pw.println(out);
for (int i=0; i<studentCount; i++)
{
line = br.readLine();
StringTokenizer tokenizer = new StringTokenizer (line, ",");
token = tokenizer.nextToken().trim();
id = Integer.parseInt(token);
name = tokenizer.nextToken().trim();
token = tokenizer.nextToken().trim();
exams = Integer.parseInt(token);
scores = new int[exams];
for (int j = 0; j < scores.length; j++)
{
token = tokenizer.nextToken().trim();
scores[j] = Integer.parseInt(token);
}
gradeType = tokenizer.nextToken().trim();
students[i] = new StudentExt(id, name, scores, gradeType);
}
String outA="", outB="", outC="", outD="", outF="", outCr="",
outNcr="";
String outAll;
for (int i=0; i<studentCount; i++)
{
grade = students[i].findGrade();
if(grade.equalsIgnoreCase("A"))
{
outA += students[i].getId() + " " + students[i].getName() + " " +
"(" + grade + ")" + "\n";
}
else if(grade.equalsIgnoreCase("B"))
{
outB += students[i].getId() + " " + students[i].getName() + " " +
"(" + grade + ")" + "\n";
}
else if(grade.equalsIgnoreCase("C"))
{
outC += students[i].getId() + " " + students[i].getName() + " " +
"(" + grade + ")" + "\n";
}
else if(grade.equalsIgnoreCase("D"))
{
outD += students[i].getId() + " " + students[i].getName() + " " +
"(" + grade + ")" + "\n";
}
else if (grade.equalsIgnoreCase("F"))
{
outF += students[i].getId() + " " + students[i].getName() + " " +
"(" + grade + ")" + "\n";
}
else if (grade.equalsIgnoreCase("CR"))
{
outCr += students[i].getId() + " " + students[i].getName() + " " +
"(" + grade + ")" + "\n";
}
else if (grade.equalsIgnoreCase("NCR"))
{
outNcr += students[i].getId() + " " + students[i].getName() + " " +
"(" + grade + ")" + "\n";
}
}
outAll=outA+outB+outC+outD+outF+outCr+outNcr;
pw.println(outAll);
pw.flush();
if (br != null)
br.close();
if (pw != null)
pw.close();
}
}
//StudentExt.java//
public class StudentExt extends Student
{
private String gradeType;
public StudentExt(int id, String name, int[] scores, String
s)
{
super(id, name.trim(), scores);
gradeType = s.trim();
}
@Override
public String findGrade()
{
String g= super.findGrade();
if(gradeType.equals("Credit"))
{
if(g.equals("A") || g.equals("B") || g.equals("C"))
{
return "CR";
}
else
{
return "NCR";
}
}
else
{
return g;
}
}
}
//Student.java//
public class Student
{
private int id;
private String name;
private int[] scores;
public Student(int id, String name, int[] scores)
{
this.id = id;
this.name = name;
this.scores = scores.clone();
}
public String findGrade()
{
double result = 0;
for(int score : scores)
{
result += score;
}
result = result/scores.length;
if(result >= 90)
{
return "A";
}
else if(result < 90 && result >= 80)
{
return "B";
}
else if(result < 80 && result >= 70)
{
return "C";
}
else if(result < 70 && result >= 60)
{
return "D";
}
else{
return "F";
}
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
}
//in.txt//
15
1, John Adam, 3, 93, 91, 100, Letter
2, Raymond Woo, 3, 65, 68, 63, Letter
3, Rick Smith, 3, 50, 58, 53, Letter
4, Ray Bartlett, 3, 62, 64, 69, Letter
5, Mary Russell, 3, 93, 90, 98, Letter
6, Andy Wong, 3, 89,88,84, Letter
7, Jay Russell, 3, 71,73,78, Letter
8, Jimmie Wong, 3, 70,77,72, Letter
9, Jackie Chan, 3, 85,89,84, Letter
10, Susan Wu, 3, 80,88,84, Letter
11, Bruce Lee, 4, 74, 79, 72, 75, Credit
12, Chuck Norris, 5, 63, 64, 62, 60, 68, Credit
13, Jet Li, 3, 85, 83, 89, Credit
14, Jessica Lauser, 3, 82, 84, 87, Letter
15, Mahnoosh Nik-Ahd, 2, 98, 99, Letter
Output:-
Student report:
1 John Adam (A)
5 Mary Russell (A)
15 Mahnoosh Nik-Ahd (A)
6 Andy Wong (B)
9 Jackie Chan (B)
10 Susan Wu (B)
14 Jessica Lauser (B)
7 Jay Russell (C)
8 Jimmie Wong (C)
2 Raymond Woo (D)
4 Ray Bartlett (D)
3 Rick Smith (F)
11 Bruce Lee (CR)
13 Jet Li (CR)
12 Chuck Norris (NCR)
PLEASE GIVEME THUMBUP........