This is a multi file assignment have the input file as well as the output and th
ID: 3657726 • Letter: T
Question
This is a multi file assignment have the input file as well as the output and the 2 files used to create the program. It keeps tossing up random errors the most current now is telling me:
Ch7_PrExercise14.java:58: error: missing return statement
}
^
1 error
1st File
import java.io.*;
import java.util.*;
public class Ch7_PrExercise14
{
public static void main(String[] args) throws FileNotFoundException
{
//Variables
int counter;
DoubleClass courseAvg = new DoubleClass();
double classAvg;
String name;
char grade;
//Creates Scanner(inFile) and Printwriter(outFile)
Scanner inFile = new Scanner(new FileReader("c:\Ch7_Ex14Data.txt"));
PrintWriter outFile = new PrintWriter ("c:\Ch7_Ex14out.txt");
//Creates labels in outfile
outFile.println("Student Test1 Test2 Test3 Test4 Test5 Average Grade");
//Main Calculation and Output to File
while(inFile.hasNext())
{
{
name = inFile.next();
outFile.printf("%-10s", name);
classAvg = calculateAverage(inFile, outFile, courseAvg);
outFile.printf("%6.2f ", courseAvg.getNum());
classAvg = classAvg + courseAvg.getNum();
grade = calculateGrade(courseAvg.getNum());
outFile.printf("%3s%n", grade);
}
classAvg = classAvg / 10;
outFile.println(classAvg);
outFile.close();
}
}
//Calculates Average
public static double calculateAverage(Scanner inF, PrintWriter outF, DoubleClass CAvg)
{
int score, sum, count;
sum = 0;
count = 0;
for(int i=0 ; i!=5 ; i++)
{
{
score = inF.nextInt();
outF.printf("%i ", score);
sum = sum + score;
}
CAvg.setNum(sum);
CAvg.divideToNum(5.0);
}
}
//this method determines and returns each student's grade.
public static char calculateGrade(double avg)
{
if(avg>=90)
{
return 'A';
}
else if(avg>=80)
{
return 'B';
}
else if(avg>=70)
{
return 'C';
}
else if(avg>=60)
{
return 'D';
}
else
{
return 'F';
}
}
}
2nd File
public class DoubleClass
{
private double x;
public DoubleClass()
{
x = 0;
}
public DoubleClass(double num)
{
x = num;
}
public void setNum(double num)
{
x = num;
}
public double getNum()
{
return x;
}
public void addToNum(double num)
{
x = x + num;
}
public void divideToNum(double num)
{
x = x / num;
}
public double compareTo(double num)
{
return (x - num);
}
public boolean equals(double num)
{
if (x == num)
return true;
else
return false;
}
public String toString()
{
return (String.valueOf(x));
}
}
Ch7_Ex14Data.txt(This is what the inFile reads):
Ch7_Ex14out.txt Example(this is what the Outfile should look like):
Explanation / Answer
Your calculateAverage() method requires a return statement. It is declared to return a double.
public static double calculateAverage(Scanner inF, PrintWriter outF, DoubleClass CAvg)
{
int score, sum, count;
sum = 0;
count = 0;
for(int i=0 ; i!=5 ; i++)
{
score = inF.nextInt();
outF.printf("%i ", score);
sum = sum + score;
CAvg.setNum(sum);
CAvg.divideToNum(5.0);
count++;
}
return (double)sum/count;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.