In: Computer Science
This project will be an exercise in using all that we have covered in these past two weeks: Arrays (one and multidimensional arrays), branching, and input and output files.
Components:
A class called YourLastNameGradebook (Yes, YourLastName IS your last name!!!)
And an external class called SomeNeatExternalSoundingName. This external class will have at least 2 methods: readData and writeData
This project will include
The class with the main method and the other useful methods that will 1) display the arrays, 2) find the averages and store them, 3) find the grades and store them, 4) and display a chart of the final array results to the terminal window.
The external class will 1) read from the input file and 2) write to an output file.
YourLastNameGradebook class's main method will be made up of declarations and method calls. There should be no other code in the main method. (Except for one single print statement stating that the program is finished!)
Declarations:
Actions needed: Note - Do use the size
of the array for your for loops, notthe value
5.
1) Read in (Invoke the readData method in the external class ):
The input file looks like this: project2IN.txt
Leia Organa 98.0 95.5 100.0
Fred Flintstone 65.0 74.5 76.25
Sheldon Cooper 100.0 100.0 99.0
Stan Marsh 89.0 84.0 80.5
Lisa Simpson 93.0 94.5 92.0
When you open the input file, echo this to the terminal window.
The array of names will be read in from the input file. (The
names will be read in as first last but need to be rearranged to
last, first when being read in)
This array of scores will be read in from the input file.
The last element in the scores array will have a zero in it. This will be reserved for the averages (later).
When you close the input file, echo this to the terminal window.
After reading in the data: Row zero from the
names array corresponds to row zero from the
scores array:
names array contains 5 names:
element: Contents:
0 |
Organa, Leia |
1 |
Flintstone, Fred |
2 |
Cooper, Sheldon |
3 |
Marsh, Stan |
4 |
Simpson, Lisa |
Scores array contains 5 rows of 4 elements each
row |
column 0 |
column 1 |
column 2 |
column 3 |
||
0 |
98.0 |
95.5 |
100.0 |
|||
1 |
65.0 |
74.5 |
76.25 |
|||
2 |
100.0 |
100.0 |
99.0 |
|||
3 |
89.0 |
84.0 |
80.5 |
|||
4 |
93.0 |
94.5 |
92.0 |
|||
2) In YourLastNameGradebook class, invoke the method to display the data using a displayData method:
Display the data (using a displayData method) in the terminal window as it is stored in both of the arrays.
Data read in:
last, first: score1, score2, score3
last, first: score1, score2, score3
last, first: score1, score2, score3
last, first: score1, score2, score3
last, first: score1, score2, score3
(Formatting at this point is not important. Include at least one space! This is just echoing what was entered!)
3) Invoke a method that will compute the average in the scores array
4) Invoke a method that will compute the grade for each student
For each student, determine the average score from the three grades and the grade of A, B, C, D, or F
5) Invoke another displayData method that will print out the names, scores, and grades for each student in the terminal window.
6) Invoke the method in the External class that will write the clean chart to the output file project2OUT.txt
7) When done, the last statement of the main is to be an output statement telling the user that the program is finished.
Output is to be neat and clean. Use blank
spaces for readability in both the output file and in the terminal
window.
Upload (to one of your pairs’ Canvas account) ALL the files that
are needed for grading including BOTH .java files and both .txt
files:
Extra Credit:
1) Use printf to create a neat chart with a chart title (centered
above the output) and headers for the data similar to the
following. (2 points)
==========================================================================
Student Grades
Name Score1
Score2
Score3
Average
Grade
==========================================================================
Organa, Leia
98.00
95.50 100.00
.00
x
Flintstone, Fred 65.00
74.50
76.25 .00
x
and so on...
2) Insert a first new line into your input file. Alter this first line to include an integer value to represent the number of rows to be read in as a reference that will declare the sizes of the arrays to be of that size.
(2 points)
Be SURE that both names appear in the beginning
comments! Otherwise only one of you will get
credit.
This is what will be displayed to the terminal
window: This could be cleaned up to be more
readable.
Reading from input file: project2IN.txt
Closing input file: project2IN.txt
Displaying data as stored:
Organa, Leia 98.0 95.5 100.0 0.0
Flintstone, Fred 65.0 74.5 76.25 0.0
Cooper, Sheldon 100.0 100.0 99.0 0.0
Marsh, Stan 89.0 84.0 80.5 0.0
Simpson, Lisa 93.0 94.5 92.0 0.0
Computing Averages...
Average for Organa, Leia is 97.83333333333333
Average for Flintstone, Fred is 71.91666666666667
Average for Cooper, Sheldon is 99.66666666666667
Average for Marsh, Stan is 84.5
Average for Simpson, Lisa is 93.16666666666667
=================================================================
Name Score1 Score2 Score3 Average Grade
=================================================================
Organa, Leia 98.00 95.50 100.00 97.83 A
Flintstone, Fred 65.00 74.50 76.25 71.92 C
Cooper, Sheldon 100.00 100.00 99.00 99.67 A
Marsh, Stan 89.00 84.00 80.50 84.50 B
Simpson, Lisa 93.00 94.50 92.00 93.17 A
Opening the output file: project2OUT.txt
Writing data to the output file.
Closing the output file project2OUT.txt
This program is now terminating. Have a nice day.
Output file: project2OUT.txt
=================================================================
Name Score1 Score2 Score3 Average Grade
=================================================================
Organa, Leia 98.00 95.50 100.00 97.83 A
Flintstone, Fred 65.00 74.50 76.25 71.92 C
Cooper, Sheldon 100.00 100.00 99.00 99.67 A
Marsh, Stan 89.00 84.00 80.50 84.50 B
Simpson, Lisa 93.00 94.50 92.00 93.17 A
Program Code :
-----------
//import io package
import java.io.*;
//main class
class YourLastNameGradeBook
{
//main method
public static void main(String args[]) throws IOException
{
//create an object to GetStuff
GetStuff ob = new GetStuff();
//invoke cal() method
ob.cal();
//invoke displayData() to print the data
ob.displayData();
}
}
public class GetStuff
{
//local declarations
private String st[] = new String [5];
private String Lnames[] = new String[5];
private String Fnames[] = new String[5];
private double marks[][] = new double [5][4];
private int j = 0;
private int row = 0, col = 0;
private double avg = 0;
private int i;
char [] grade = new char[5];
//cal method
public void cal()
{
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("input.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
//split the line basing on space delimiter
st= strLine.split("\\s+");
//read the First name
Fnames[j] = st[0];
//read the First name
Lnames[j] = st[1];
//intialize sum to 0
double sum =0;
//loop runs for 3 times for each subject
for( i=0;i<3;i++)
{
//convert the marks(in String format) to double
marks[row][i] = Double.parseDouble(st[i+2]);
//add the marks to total
sum += marks[row][i];
}
//calculate the average
avg = (double)sum / (i);
//assign the avg to last column of the array
marks[row][i]= avg;
//increment the row
//if avg>90 then grade is A
if(avg > 90)
grade[j]='A';
//if avg>80 then grade is B
else if(avg>80)
grade[j]='B';
//if avg>70 then grade is C
else if(avg>70)
grade[j]='C';
//if avg>60 then grade is D
else if(avg>60)
grade[j]='D';
//if avg>50 then grade is E
else if(avg>50)
grade[j]='E';
//else grade is F
else
grade[j]='F';
//increment the row
row++;
//increment the j
j++;
}
// Close the input stream
in.close();
}
catch (Exception e)
{
// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public void displayData() throws IOException
{
//create an object to file writer
FileWriter fstream = new FileWriter("output.txt");
//create an object to BufferedWriter
BufferedWriter br = new BufferedWriter(fstream);
//print the headings
System.out.println("LastName FirstName\tMarks1 \tMarks2 \tMarks3\t Average\t Grade");
//write the names into file
br.write(Lnames[1]+""+Fnames[1]);
//for each student
for(row=0;row<5;row++)
{
//print a new line
System.out.println();
br.write("\n");
//write the names into output file
br.write(Lnames[row]+","+Fnames[row]);
//print the marks
System.out.print(""+Lnames[row]+","+Fnames[row]);
for(col=0;col<4;col++)
{
//print the marks
System.out.print("\t\t"+marks[row][col]);
//write the marks
br.write("\t\t"+marks[row][col]);
}
//print the grade
System.out.println("\t\t"+grade[row]+"\t\t");
//write the grade in to file
br.write(grade[row]);
}
}
}
----------------------
input
output