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

CS1401 – Programming Assignment 6 Due: Monday March 21, 2016 by 8:00 a.m. To be

ID: 3678344 • Letter: C

Question

CS1401 – Programming Assignment 6

Due: Monday March 21, 2016 by 8:00 a.m.

To be submitted to “Instructors” via Piazza in the folder “lab6”

or on BlackBoard if you are a student of Dr. Villanueva

Welcome to Lab 6!

Important notes and pieces of advice:

Indent your code properly following guidelines available at: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html. 15 points will be reserved for correct indentation. Failing to indent properly puts you at risk of losing these 15 points.

Spend time working on your pseudocode as the amount of points you get for the pseudocode is bigger than the amount of points you get for your code (usually, close to a 60/40 ratio). Pseudocode comes first!

Do not submit more than the files that are requested from you: one docx file and one java file.

Activity 1. Student Grades. In this activity, you will read information from a file that contains information as follows:

John Doe        95.3

Stacy Jeer       99

Arturo Sanchez          94

Giovanna Peam          80

Carla Pion       90

You will then have to manipulate this information as follows: Below we describe each of the methods you have to implement. Do follow specifications. Code is also provided to you: do work from the provided code.

1/ Method BuildArrayNames

This method reads the above file (name of the file obtained from the main method) and produces an array of names.

Signature of the method:

BuildArrayNames (String fileName) à array of Strings

For the example above, the resulting array will be as follows:

John Doe

Stacy Peer

Arturo Sanchez

Giovanna Peam

Carla Pion

2/ Method BuildArrayGrades

This method reads the above file (name of the file obtained from the main method) and produces an array of grades.

Signature of the method:

BuildArrayGrades (String fileName) à array of double values

For the example above, the resulting array will be as follows:

95.3

99

94

80

90

3/ Method PrintInformation

This method takes both arrays and an index of an element in each array (same value for both arrays), and prints out the corresponding information.

Signature of the method:

PrintInformation (String[] names, double[] grades, int index) à void

For instance, calling PrintInformation on both of the above arrays and index 2 should result in printing:

“Arturo Sanchez’ grade is 94”

4/ Method PrintAllInformation

This method takes both arrays and prints out all of the information in the arrays in a specific format (see below).

Signature of the method:

PrintAllInformation (String[] names, double[] grades) à void

For instance, calling PrintAllInformation on both of the above arrays should result in printing:

“John Doe’s grade is 95.3

Stacy Peer’s grade is 99

Arturo Sanchez’ grade is 94

Giovanna Peam’s grade is 80

Carla Pion’s grade is 90”

Requirement: You have to implement this method by calling PrintInformation repeatedly using a for loop.

5/ Method FindBestGrade

This method takes the array of grades and returns the index of the best grade.

Signature of the method:

FindBestGrade (double[] grades) à int

For instance, calling FindBestGrade on the above array of grades will result in returning value 1.

6/ Method FindBestGradeInRange

This method takes the array of grades, and two indices that delimitate a range of interest (between indices i and j included), and returns the index of the best grade.

Signature of the method:

FindBestGradeInRange (double[] grades, int i, int j) à int

For instance, calling FindBestGradeInRange on the above array of grades and indices 1 and 3 will result in returning value 1.

7/ Method Swap

This method takes two indices and two arrays and swaps the two corresponding grades and the two corresponding names in the two arrays.

Signature of the method:

Swap (String[] names, double[] grades, int i, int j) à void

For instance, calling Swap of the above two arrays and indices 1 and 3 will cause the two arrays to become:

John Doe

Giovanna Peam

Arturo Sanchez

Stacy Peer

Carla Pion

95.3

80

94

99

90

Note: nothing is returned from this method.

8/ Method SortBestFirst

This method takes both arrays and sorts them by best student/best grade.

Signature of the method:

SortBestFirst(String[] names, double[] grades) à void

For instance, calling SortBestFirst on both of the above arrays should result in the two arrays becoming:

Stacy Peer

John Doe

Arturo Sanchez

Carla Pion

Giovanna Peam

99

95.3

94

90

80

Requirement: You have to implement this method by calling the FindBestGradeInRange and Swap methods, and by doing so repeatedly using a for loop.

9/ Main method:

In the main method, you will have to do the following:

1/ Ask the user for the name of the file that contains the students’ names and grades.

2/ Properly call method BuildArrayNames

3/ Properly call method BuildArrayGrades

4/ Print all information in the two arrays by calling method PrintAllInformation

4/ Properly call method FindBestGrade

5/ Based on the result of method FindBestGrade, you will call the method PrintInformation that will print out who has the best grade and what this grade is

6/ Properly call method SortBestFirst and call method PrintAllInformation that will print out the whole two arrays as follows:

Stacy Jeer 99

John Doe 95.3

Arturo Sanchez 94

Carla Pion 90

Giovanna Peam 80

Instructions:

Write the pseudocode of your algorithm in a docx file named “yourLastName-yourFirstName-lab6.docx”.

Modify the code provided in Lab6Spring16.java where prompted (and only where prompted) to make sure that the code follows the above instructions.

Note: inability to follow instructions will be penalized by 10 points.

Now, here is what you have to turn in:

The modified java file called Lab6Spring16.java.

The file “yourLastName-yourFirstName-lab6.docx”in which you have described your pseudocode (= your algorithm).

Bonus Activity. More Grades. (extra 50 points) In this activity, you will have to implement pretty much the same functionalities as in Activity 1, except that now each student has more grades (8 grades per student: exam1, exam2, exam3, final exam, lab grade, class and lab participation, quizzes and homework, and research experience).

Specifically, the file you will read will now be as follows:

John Doe        95.3 80 78 75 90 96 76 89

Stacy Jeer       99 100 97 64 78 52 67 90

Arturo Sanchez          94 65 46 23 87 67 78 76

Giovanna Peam          80 75 76 79 90 81 84 95

Carla Pion       90 65 76 89 86 90 56 78

You will then have to manipulate this information as follows: Below we describe each of the methods you have to implement. Do follow specifications. Code is also provided to you: do work from the provided code.

1/ Method BuildArrayNames

This method reads the above file (name of the file obtained from the main method) and produces an array of names.

Signature of the method:

BuildArrayNames (String fileName) à array of Strings

For the example above, the resulting array will be as follows:

John Doe

Stacy Peer

Arturo Sanchez

Giovanna Peam

Carla Pion

2/ Method BuildArrayGrades

This method reads the above file (name of the file obtained from the main method) and produces an array of grades.

Signature of the method:

BuildArrayGrades (String fileName) à 2D array of double values

For the example above, the resulting array will be as follows:

95.3

80

78

75

90

96

76

89

99

100

97

64

78

52

67

90

94

65

46

23

87

67

78

76

80

75

76

79

90

81

84

95

90

65

76

89

86

90

56

78

3/ Method PrintInformation

This method takes both arrays and an index of an element in each array (same value for both arrays), and prints out the corresponding information.

Signature of the method:

PrintInformation (String[] names, double[][] grades, int index) à void

For instance, calling PrintInformation on both of the above arrays and index 2 should result in printing:

“Arturo Sanchez’ grades is 94, 65, 46, 23, 87, 67, 78, 76”

4/ Method PrintAllInformation

This method takes both arrays and prints out all of the information in the arrays in a specific format (see below).

Signature of the method:

PrintAllInformation (String[] names, double[][] grades) à void

For instance, calling PrintAllInformation on both of the above arrays should result in printing:

“John Doe’s grades are 95.3 80 78 75 90 96 76 89

Stacy Peer’s grades are 99 100 97 64 78 52 67 90

Arturo Sanchez’ grades are 94 65 46 23 87 67 78 76

Giovanna Peam’s grades are 80 75 76 79 90 81 84 95

Carla Pion’s grades are 90 65 76 89 86 90 56 78”

Requirement: You have to implement this method by calling PrintInformation repeatedly using a for loop.

5/ Method FindBestGrade

This method takes the 2D array of grades and returns the index of the row of the best final grade. The final grade has to be computed as follows (just like in your syllabus):

exams 1, 2, and 3 together are worth 21% of the final grade

the final exam is worth 20% of the final grade

labs are worth 30% of the final grade

class and lab participation is worth 14% of the final grade

quizzes and homework are worth 12% of the final grade

research experience is worth 3% of the final grade

Signature of the method:

FindBestGrade (double[][] grades) à int

For instance, calling FindBestGrade the above array of grades will result in returning value 0 (because John Doe has the set of grades that yields the best final grade).

6/ Method Swap

This method takes two indices and two arrays and swaps the two corresponding rows of grades and the two corresponding names in the two arrays.

Signature of the method:

Swap (String[] names, double[][] grades, int i, int j) à void

For instance, calling Swap of the above two arrays and indices 1 and 3 will cause the two arrays to become:

John Doe

Giovanna Peam

Arturo Sanchez

Stacy Peer

Carla Pion

95.3

80

78

75

90

96

76

89

80

75

76

79

90

81

84

95

94

65

46

23

87

67

78

76

99

100

97

64

78

52

67

90

90

65

76

89

86

90

56

78

Note: nothing is returned from this method.

7/ Method Average

This method takes the 2D array of grades and a column index and returns the average of the grades for this column (namely the average of the grades in exam1 or exam2 or etc.).

Signature of the method:

Average(double[][] grades, int column) à double

For instance, calling Average on the array of grades and column 2 should result in returning 74.6.

8/ Main method:

In the main method, you will have to do the following:

1/ Ask the user for the name of the file that contains the students’ names and grades.

2/ Properly call method BuildArrayNames

3/ Properly call method BuildArrayGrades

4/ Print all information in the two arrays by calling method PrintAllInformation

4/ Properly call method FindBestGrade

5/ Based on the result of method FindBestGrade, you will call the method PrintInformation that will print out who has the best grade and what this grade is

6/ Properly call method Average on each column of the array of grades (using a for loop) and display the results properly (e.g., “the average for exam1 is…”, “the average for exam2 is…”, etc.).

Instructions:

Write the pseudocode of your algorithm in a docx file named “yourLastName-yourFirstName-lab6Bonus.docx”.

Modify the code provided in Lab6Spring16Bonus.java where prompted (and only where prompted) to make sure that the code follows the above instructions.

Note: inability to follow instructions will be penalized by 10 points.

Now, here is what you have to turn in:

The modified java file called Lab6Spring16Bonus.java.

The file “yourLastName-yourFirstName-lab6Bonus.docx” in which you have described your pseudocode (= your algorithm).

John Doe        95.3

Stacy Jeer       99

Arturo Sanchez          94

Giovanna Peam          80

Carla Pion       90

Explanation / Answer

java program for operations on student details:

import java.util.*;
import java.io.*;
public class Stdopr{

public static void main(String []args){

String [] names;

Double [] grades;

int index;
Studentdet std=new Studentdet();
System.out.println("ENTER FILE NAME THAT CONTAINS STUDENT NAMES AND GRADES");
Scanner infile = new Scanner(System.in);
String filename = infile.nextLine();
System.out.println("dsfd"+filename);
names[]=std.BuildArrayNames (filename);
grades[]=std.BuildArrayGrades (filename);

std.PrintInformation (names, grades, index);

std.PrintAllInformation (names,grades);

int ibgr=FindBestGrade (grades);

int i=2,j=5;

int ibg=FindBestGradeInRange (grades, i, j) ;

Swap ( names, grades, i, j);

SortBestFirst( names, grades);
}
}


class Studentdet
{
int i=0,j=0;
String str,names[],grades[];
BufferedReader br,br1;
public String BuildArrayNames (String fileName)
{
  
try
{
br = new BufferedReader(new FileReader(fileName));   
br1 = new BufferedReader(new FileReader(fileName));
while ((str = br.readLine() ) != null)
{
               System.out.println(i+str);

names [i]=str.split(" ",1);
              
           }
          
  
}
catch (IOException e) {
           e.printStackTrace();
}

return names;

}

public Double BuildArrayGrades (String fileName)
{
  
try
{
while ((str = br.readLine() ) != null)
{
               System.out.println(str);

grades [i]=str.split(" ",2);
           }
}
catch (IOException e) {
           e.printStackTrace();
}

return grades;

}

public void PrintInformation (String [] names, Double [] grades,int index)

{

while((str=names.readLine()) != null)

{

System.out.println(str+"grade is"+grades.readLine());

}

}

public void PrintAllInformation (String [] names,Double[] grades)

{

while((str=names.readLine()) != null)

{

System.out.println(str+"'s grade is"+grades.readLine());

}

}

public int FindBestGrade (Double grades)

{

Double bg=0;

int i=0;

while(grades.nextLine() != null)

{

i++;

if(bg > Double(grades.nextLine()));

else

bg = Double(grades.nextLine());

}

return i;

}

public int FindBestGradeInRange (Doble [] grades,int i,int j)

{

Double bg=0.0;

while(i<=j)

{

i++;

if(bg > Double(grades[i]));

else

bg = Double(grades[i]);

}

return i;

}

public void Swap ( String [] names, Double [] grades,int i, int j)

{

}

public void SortBestFirst( names, grades)

{

}

}