Question

In: Computer Science

Description This assignment is a modification of the last assignment. In this assignment, you will input...

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();

}

Solutions

Expert Solution

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........


Related Solutions

Assignment Description For this assignment you will complete a literature review on a topic related to...
Assignment Description For this assignment you will complete a literature review on a topic related to public health or health administration (depending on your major). You can pick any topic within your field that you find interesting and would like to know more about! You will locate a minimum of 4 journal articles on the topic and 1 online source and write a 1 page literature review based on your topic, your question and the sources you’ve selected. The purpose...
*********C++ Program************ This assignment is the continuation of project 4, with the modification and improvement with...
*********C++ Program************ This assignment is the continuation of project 4, with the modification and improvement with the following modifications: In Frac.h, replace the methods addition (const Fraction & ), subtraction( const Fraction& ), multiply( const Fraction & ), divide( const Fraction & ), void printFraction() by overloading operators +, -, *, /, << respectively. Add overloading operators > and < so that they can compare two Fractions. Write your own implementation file which is named Frac2.cpp to replace Frac.cpp and...
In this assignment, which continues to build on last week's assignment, you will create a mind...
In this assignment, which continues to build on last week's assignment, you will create a mind map identifying the steps in the training process for a CRM program to help you understand the important role training plays in an organization. It further helps you understand the importance of training as you explore how training is designed, delivered, and measured for success I need more info on what a CRM is and Which training evaluation methods would you recommend to assess...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Candy Calculator [50 points] The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR. The calories needed for a woman to maintain her weight is: BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 *...
In this assignment you will be given an input file containing a series of universities along...
In this assignment you will be given an input file containing a series of universities along with that universities' location and rating. You must write a program that uses user defined functions to output a list of all school's above a certain rating to the terminal. Your program should do the following: - Prompt the user for the name of the input file to open - Prompt the user for the rating threshold ( Note: we print if the rating...
Assignment Description and Instructions: As a group of prospective entrepreneurs, you are required to prepare a...
Assignment Description and Instructions: As a group of prospective entrepreneurs, you are required to prepare a business plan for a new skin care business (not an existing business/company) to acquire financing. ALL group members are expected to collaborate and contribute to the business plan. Each member will be required to complete peer evaluation of the other team members using the rubric provided. The plan is expected to include: Title page. This should include the company’s name, logo, address and contact...
This writing assignment will involve implementing and analyzing a behavior modification program on yourself. The behavior...
This writing assignment will involve implementing and analyzing a behavior modification program on yourself. The behavior is ANXIETY OF MEETING NEW PEOPLE. This assignment is to develop your own functional behavior assessment. A minimum of 3 paragraphs per question is required. Modification Plan: In this section, you need to describe your assessment and intervention plan. How will you collect data on the behavior in order to evaluate whether or not it is changing? Describe any forms or graphs you plan...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments: % bstsort [-c] [-o output_file_name] [input_file_name] If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name is given with the -o option,...
Assignment Description: You will be required to conduct analysis of a technology/system application and write a...
Assignment Description: You will be required to conduct analysis of a technology/system application and write a formal paper. The analysis need to be of an application technology used in either (a) a clinical setting for managing health data, patient care, etc or (b) educational setting for improvement of teaching and learning of students, patients, etc. The Technology Application Analysis will be an independent activity for each student to be completed using resources available in your agency, IT department, on the...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT