How to combine these 3 void methods?..
My assignment is to make a programm to read student's name and tests from a file.
1)method to calculate average.
2)method to evaluate grade.
This is my java code.
Code Java:
import java.util.*;
import java.io.*;
public class average {
public static void main(String[] args) throws FileNotFoundException{
double test1, test2, test3, test4, test5;
String student;
Scanner inFile = new Scanner(new FileReader
("C:\\Users\\Owner\\Desktop\\CSCI 112\\Class.txt"));
PrintWriter outFile = new PrintWriter
("C:\\Users\\Owner\\Desktop\\CSCI 112\\Class.out");
outFile.println("Student Test1 Test2 Test3 Test4 Test5 Average Grade");
while (inFile.hasNext()){
student = inFile.next();
test1 = inFile.nextDouble();
test2 = inFile.nextDouble();
test3 = inFile.nextDouble();
test4 = inFile.nextDouble();
test5 = inFile.nextDouble();
outFile.printf("%-9s %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %n",
student, test1, test2, test3, test4, test5);
}
inFile.close();
outFile.close();
}
---->OR<---- But i dont think this apply to my assignment
Code Java:
public class calaverage {
public static void main(String[] args) throws FileNotFoundException {
double test1, test2, test3, test4, test5;
double average;
String student;
Scanner inFile = new Scanner
(new FileReader("C:\\Users\\Owner\\Desktop\\CSCI 112\\Class.txt"));
PrintWriter outFile = new PrintWriter ("testavg.out");
student = inFile.next();
outFile.println("Student Name: " + student);
test1 = inFile.nextDouble();
test2 = inFile.nextDouble();
test3 = inFile.nextDouble();
test4 = inFile.nextDouble();
test5 = inFile.nextDouble();
outFile.printf("Test scores: %5.2f %5.2f %5.2f %5.2f %5.2f %n"
, test1, test2, test3, test4, test5);
average = (test1 + test2 + test3 + test4 + test5) / 5.0;
outFile.printf("Average test score: %5.2f %n", average);
inFile.close();
outFile.close();
}
}
method to calculate average
(wondering how i can turn this into a value returning method or combime with my first code)
Code Java:
public static void calculateAverage(double average, double test1,
double test2, double test3, double test4, double test5)
{
average = (test1 + test2 + test3 + test4 + test5) / 5.0;
}
method to evaluate the grade
(wondering how i can turn this into a value returning method or combime with my first code)
Code Java:
public static void calculateGrade(final int score, char grade){
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'F';
System.out.printf ("The grade is %c.%n", grade);
}
Re: How to combine these 3 void methods?..
You should declare a return type and return the value you want from that method. Also, I would recommend using an array to simplify your average calculations.
Code Java:
// example implementation of calculateGrade
public static char calculateGrade(final int score){
char grade;
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'F';
return grade;
}
Then, in your main method:
Code Java:
System.out.println("The grade for test 1 was: " + calculateGrade(test1));
Re: How to combine these 3 void methods?..
i need to read and print certian files, but i can not test my programm because my computer doesnt allow acess to my programm to run. I use netbeans and run it as administrator, but it doesnt work.
This is the file class.text on my computer. The output most add average, grade and class avgerage
Student Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63
Re: How to combine these 3 void methods?..
Thanks! HelloWorld922
I think this is what my teacher is looking for:
new improve code:
Code Java:
import java.util.*;
import java.io.*;
public class average {
public static double calculateAverage(double average, double test1,
double test2, double test3, double test4, double test5){
average = (test1 + test2 + test3 + test4 + test5) / 5.0;
return average;}
public static char calculateGrade(double score){
char grade;
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'F';
return grade;}
public static void main(String[] args) throws FileNotFoundException{
double test1, test2, test3, test4, test5, sAverage, classAverage;
double average = 0;
String student;
char sAvgGrade;
double sum = 0;
double count = 0;
Scanner inFile = new Scanner(new FileReader
("C:\\Class.txt"));
PrintWriter outFile = new PrintWriter
("C:\\Class.out");
outFile.println("Student Test1 Test2 Test3 Test4 Test5 Average Grade");
while (inFile.hasNext()){
student = inFile.next();
test1 = inFile.nextDouble();
test2 = inFile.nextDouble();
test3 = inFile.nextDouble();
test4 = inFile.nextDouble();
test5 = inFile.nextDouble();
sAverage = calculateAverage(average, test1, test2, test3, test4, test5);
sAvgGrade = calculateGrade(sAverage);
outFile.printf("%-7s %5.0f %5.0f %5.0f %5.0f %5.0f %8.2f %3c %n",
student, test1, test2, test3, test4, test5,sAverage, sAvgGrade);
sum = sum + sAverage;
count++;}
outFile.println("");
outFile.println("Class Average = "+ sum/count);
inFile.close();
outFile.close(); } }
i cant test this out tho so i have no clue if its working or not. T_T