2D arrays, Strings, doubles and addition
My assignment is to create a program that will scan in a text file that
contains the students details and grades for 4 assignments which will then
be broken down into both Strings (the details) and doubles (the students
grades) and to finally calculate the average for each students grade and
then an average for each Assignment. The Final output should look like
this(got it from my assignment sheet):
Student Name FAN Part 1 Part 2 Part 3 Part 4 Mark Grade
Adam Adamson adam0001 85.4 79.8 82.4 86.1 82.77% HD
Bethany Bright brig0001 89.7 85.6 84.2 82.9 84.92% DN
Cameron Carlson carl0001 55.45 49.82 60.4 42.27 50.23% P
David Dawson daws0001 72.6 78.49 80.2 65.88 74.46% CR
Evelyn Ellis elli0001 50.2 35.88 48.41 58.37 46.57% FA
Frances Fitz fitz0001 78.9 75.67 82.48 79.1 78.38% DN
Greg Gregson greg0001 24.3 32.88 29.72 28.4 30.05% F
Harriett Hope hope0001 52.2 58.93 61.5 63.44 60.12% P
Ivan Indigo indi0001 88.4 91.23 90.05 92.46 91.08% HD
Jessica Jones jone0001 82.33 89.74 81.3 84.85 85.84% HD
Average 67.948 67.804 70.066 68.377 68.44% CR
StdDev 19.4441
In the text file there are 10 lines, first line is:
Adam Adamson,adam0001,85.4,79.8,82.4,86.1
and so on for 10 randomly created students. I have 3 classes, a main class
called Topic Management, a StudentMarks class (for the students grades)
and a Student class (for the name and the FAN (the adam0001 part)). I have
created an array that stores all the scores called marks. I am able to
print out the name and the FAN and am also able to assign the grades into
doubles using double score1 = double.parseDouble(); etc. each score has
it's own method which I am then calling in the main class.
The trouble I am having is getting each column of results to print out
next to the name and the fan. At the moment they just print out in single
column below. Then I need to calculate the averages, i'm not sure how to
go about it. Any help would be greatly appreciated. This is my program so
far:
This is my Main Class - Topic Management:
public class TopicManagement
{
public static void main(String[] args) throws IOException
{
System.out.println("Hello, Welcome to the Student Assesment Calculator");
System.out.println("Student Name \t FAN \t\tScore 1\tScore2\tScore
3\tScore 4\tTotal");
Student student = new Student();
StudentMarks studentMarks = new StudentMarks();
student.student();
studentMarks.score1();
studentMarks.score2();
studentMarks.score3();
studentMarks.score4();
}//end of method
}//end of class
The Student Class:
public class Student
{ //ROW, COL
String[][] nameFan = new String[10][6];
//this method outputs the name and fan
public void student() throws IOException
{
Scanner scan = new Scanner (new File ("TestResults.txt"));
for (int row = 0, col; row < nameFan.length; row++)
{
Scanner lineRead = new Scanner(scan.nextLine());
lineRead.useDelimiter(",");
col = 0; // Starting at column 0 for each row
while (lineRead.hasNext()) //Check for next
{
nameFan[row][col]=lineRead.next();
col++; // Move on to the next column
}
}
for (int row = 0; row < nameFan.length; row++)
{
System.out.print(nameFan[row][0] + "\t " + nameFan[row][1] + "\t ");
System.out.print("\n");
}
}//end of nameFan method
public String [][] nameFan()
{
return nameFan;
}
}//end of class
Then finally the StudentsMarks class:
public class StudentMarks
{
//ROW, COL
String[][] marks = new String[10][6];
//was going to call the method 'student' but would have been
confusing with the student class
public String[][] studentMarks() throws IOException
{
Scanner scan = new Scanner (new File ("TestResults.txt"));
for (int row = 0, col; row < marks.length; row++)
{
Scanner lineRead = new Scanner(scan.nextLine());
lineRead.useDelimiter(",");
col = 0; //Starting at column 0 for each row
while (lineRead.hasNext()) //Check for next
{
marks[row][col]=lineRead.next();
col++; // Move on to the next column
}
}
return marks;
}
public void score1() //method for score1
{
for (int row = 0; row < marks.length; row++)
{
double score1 = Double.parseDouble(marks[row][2]); //col
remains 2 because all of column 2 is assignment 1, it doesn't
change
System.out.print(score1);
System.out.println("\t1"); //Had a 1 print out so i knew which
was part of the first group of scores
}
}
public void score2() //method for score2
{
for (int row = 0; row < marks.length; row++)
{
double score2 = Double.parseDouble(marks[row][3]);
System.out.print(score2);
System.out.println("\t2"); //same as in score 1
}
}
public void score3()
{
for (int row = 0; row < marks.length; row++)
{
double score3 = Double.parseDouble(marks[row][4]);
System.out.print(score3);
System.out.println("\t3");
}
}
public void score4()
{
for (int row = 0; row < marks.length; row++)
{
double score4 = Double.parseDouble(marks[row][5]);
System.out.print(score4);
System.out.println("\t4");
}
}
}
No comments:
Post a Comment