Question:
Student Averages Java Program Help!?!?
Jasmine
2009-03-28 14:01:24 UTC
I have a java assignment I need to complete:
I put this text into Notepad:
Agnes 56 82 95 100 68 52
Bufford 87 92 97 100 96 85 93 77 98 86
Julie 99 100 100 89 96 100 92 99 68
Alice 40 36 85 16 0 22 72
Bobby 100 98 92 86 88

Please write java code to take the file and print the names of the people with their grade averages.

For example:

Output = Agnes, average = 76

If you would like to help thank you, otherwise do not criticize me because I have no idea how to complete this problem.
Four answers:
Nesreen
2009-03-28 14:39:35 UTC
(1) Assuming the grades are stored in the file "C:\Grades.txt".



=====================================

import java.io.FileInputStream;



public class GradeCalculator {



private String gradesFilename;



public GradeCalculator(String filename) {

gradesFilename = filename;

}



public void printAverages() {

try {

FileInputStream fis = new FileInputStream(gradesFilename);

byte[] bytes = new byte[1024];

fis.read(bytes);

String text = new String(bytes);

fis.close();



String[] lines = text.split("\n");

for(int i=0; i
String[] studentData = lines[i].split(" ");

String name = studentData[0];

int total = 0;

int count = 0;

for(int j=1; j
int grade = Integer.parseInt(studentData[j]);

total += grade;

count++;

}

int average = total/count;

System.out.println("Output = " + name + ", Average = " + average);

}

} catch(Exception e) {

e.printStackTrace();

}

}



public static void main (String[] args) {

GradeCalculator calc = new GradeCalculator("C://Grades.txt");

calc.printAverages();

}

}

=====================================



(2) The output of the code will be:



=====================================

Output = Agnes, Average = 80

Output = Bufford, Average = 91

Output = Julie, Average = 96

Output = Alice, Average = 33

Output = Bobby, Average = 94

=====================================
Blackcompe
2009-03-28 14:19:39 UTC
If your really in this to learn , which you should be, then you need to start with Sun's tutorials. It's a bit hard to believe that you would have an assignment this complex without having to have done others that were easier. Try your luck at this and then ask questions bit by bit. We would be happy to help.
kraay
2016-11-03 15:44:42 UTC
i attempted to discover some style of educational on the thank you to seek Yahoo! solutions. No. Such. success. in case you will get somebody to coach you procedures the quest element works on Yahoo! solutions, this very question, the precise same question you're asking, has been asked 625 circumstances. seek for: java scholar application
deonejuan
2009-03-28 14:31:06 UTC
You need an Object. You need a datastore. You need a main program. Let's do the Object first.



class Student {

String name;

String[] grades;

public Strudent( String dataline ) {

String[] fields = dataline.split(" ");

this.name = fields[0];

grades = new String[ fields.length - 2 ];

for( int i = 1; i < fields.length; i++ ) {

grades[ i- 1] = fields[ i ];

}

fields = null;

}

private double gradeAvg() {

double avg = 0.0;

for( int i = 0; i
avg += Double.parseDouble( grades[i] );

}

return avg / (double)grades.length;

}

public String toString() {

return name + ", average = " + gradeAvg();

}

}

//.=========

class GradeReport {



public static void main( String[] args ) {

ArrayList gradebook = new ArrayList();

String filename = "students.txt";



FileReader fin = null;

try {

URL url = getClass().getResource( filename );

fin = new FileReader(url.toURI().getPath());

} catch (Exception e) {

System.out.println("Problem loading file " + filename + ": " + e);

}



Scanner sc = new Scanner(fin);

while (sc.hasNext()) {

gradebook.add(sc.nextLine());

}

sc.close();

try {

fin.close();



} catch (IOException ex) {

System.out.println("problem closing FileReader in");

}

//--------

for( Student s : gradebook ) {

System.out.println( s );

}

}

//==========

now note this code is untested. I did not do the import statements at the top of each class. the student.txt file would need to be in the same folder as your compiled class files.



good luck


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...