Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I need Someone who can make a program by using sample code I posted. Thanks

ID: 3911163 • Letter: H

Question

Hi, I need Someone who can make a program by using sample code I posted. Thanks

Student.java

package as11;

public class Student {

       private int id;

       private String name;

       private int [] exams;

       public Student(int id, String n, int [] ex){

       this.id = id;

       name = n;

       exams = new int [ex.length];

       System.arraycopy(ex, 0, exams, 0, ex.length);

       }

       public String findGrade(){

       String grade="";

       int avg=0;

       for(int i=0;i<exams.length;i++)

       avg+=exams[i];

       avg/=exams.length;

       if(avg>=90 && avg<=100) grade="A";

       else if(avg>=80 && avg<=89) grade="B";

       else if(avg>=70 && avg<=79) grade="C";

       else if(avg>=60 && avg<=69) grade="D";

       else if(avg>=0 && avg<=59) grade="F";

       return grade;

       }

       public int getId ( )

       {

       return id;

       }

       public String getName ( )

       {

       return name;

       }

}

(StudentExt.java)

package as11;

import as11.Student;

public class StudentExt extends Student{

       private String gradeType;

       public StudentExt(int id, String n, int[] ex, String s) {

        super(id, n.trim(), ex);

        gradeType = s.trim();

    }

    public String findGrade() {

        String s = super.findGrade();

        if(gradeType.equals("Credit")) {

            if(s.equals("A") || s.equals("B") || s.equals("C")) {

                return "CR";

            }

            else {

                return "NCR";

            }

        }

        else {

            return s;

        }

    }

}

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, you can create the file in.txt in your project.

The output file out.txt will be automatically created when you write to it in your program.

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.

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 exam taken, 93, 91, 100 are individual exam 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

//The sample code below input student data from file “in.txt”

//and output student data in file “out.txt”

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

   

    out = "Student report: ";

    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.

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.

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

}

Explanation / Answer

// This is the change code

package as11;

import java.io.*;

import javax.swing.*;
import java.util.*;

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("E:\trivium\java\in.txt"));

//Create a PrintWriter object for outputting to a file out.txt.

PrintWriter pw = new PrintWriter (new FileWriter("E:\trivium\java\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);
StudentExt S[]=new StudentExt[studentCount];
//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

String out = "Student report: ";
int Id;
int ex[]=new int[8];
int j;
int num_exam;
String name,gradetype,n;
  
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.
StringTokenizer st1 =new StringTokenizer(in,",");
Id=Integer.parseInt(st1.nextToken());
name=st1.nextToken();
num_exam=Integer.parseInt(st1.nextToken().trim());
System.out.println(num_exam);
for(j=0;j<num_exam;j++)
{
ex[j]=Integer.parseInt(st1.nextToken().trim());
}
gradetype=st1.nextToken();  
//create the corresponding StudentExt object
//System.out.println(Id+name+gradetype);
S[i]=new StudentExt(Id,name,ex,gradetype);
}

  
//String variables for accumulating output lines

String outA="", outB="", outC="", outD="", outF="", outCr="", outNcr="";

String Grade;
//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
Grade=S[i].findGrade();
//Depending on the grade, accumulate a line of output to the corresponding String variable
if(Grade.equals("A"))
outA=outA+S[i].getId()+S[i].getName()+"("+Grade+")"+System.lineSeparator();
else if(Grade.equals("B"))
outB=outB+S[i].getId()+S[i].getName()+"("+Grade+")"+System.lineSeparator();
else if(Grade.equals("C"))
outC=outC+S[i].getId()+S[i].getName()+"("+Grade+")"+System.lineSeparator();
else if(Grade.equals("D"))
outD=outD+S[i].getId()+S[i].getName()+"("+Grade+")"+System.lineSeparator();
else if(Grade.equals("F"))
outF=outF+S[i].getId()+S[i].getName()+"("+Grade+")"+System.lineSeparator();
else if(Grade.equals("Cr"))
outCr=outCr+S[i].getId()+S[i].getName()+"("+Grade+")"+System.lineSeparator();
else
outNcr=outNcr+S[i].getId()+S[i].getName()+"("+Grade+")"+System.lineSeparator();
}

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

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote