Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I am writing this lab in java to find the average of three numbers that were

ID: 3822489 • Letter: H

Question

Hi, I am writing this lab in java to find the average of three numbers that were pulled from a file. The numbers are 95, 90, ans 85. I was supposed to add five to each number and also get the average. I think I have the code set, except for the method for average. I have it returning 93.33 as a place holder, but I'm not sure if this is correct.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class ReGrade {
public static double average(double[] grades2){

return 93.33;
}

public static double[] addFive(double[] grades2){
return grades2;
}

public static void main(String[] args) throws IOException {
double[] grades = new double[3];

File myFile = new File("/Users/mcclainreggish/Documents/workspace/AverageGrades/src/myGrades.txt");
Scanner inputFile = new Scanner(myFile);
//write code to read and print each number from the file

double myGrade;

   //counter

int x = 0;

   while (inputFile.hasNext()){

  myGrade = inputFile.nextDouble();

//put myGrade into the correct spot in the grades array

grades [x] = myGrade;
x++;

System.out.println(myGrade);}

grades = addFive(grades);
double avg = 0

avg = average(grades);


System.out.print("The average of the grades is" + avg);

inputFile.close();

}

}

Explanation / Answer

ReGrade.java

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class ReGrade {

public static double average(double[] grades2){
double sum = 0;
for(int i=0; i<grades2.length; i++){
   sum = sum + grades2[i];
}
return sum/grades2.length;
}
  
  
public static double[] addFive(double[] grades2){
for(int i=0; i<grades2.length; i++){
   grades2[i] = grades2[i] + 5;
}
  
return grades2;
}
  
public static void main(String[] args) throws IOException {
double[] grades = new double[3];
  
  
  
File myFile = new File("D:\myGrades.txt");
Scanner inputFile = new Scanner(myFile);
  
//write code to read and print each number from the file
double myGrade;
  
//counter
int x = 0;
  
while (inputFile.hasNext()){
myGrade = inputFile.nextDouble();
//put myGrade into the correct spot in the grades array
grades [x] = myGrade;
x++;
System.out.println(myGrade);
}
grades = addFive(grades);
double avg = 0;
avg = average(grades);
System.out.print("The average of the grades is" + avg);
inputFile.close();
}
}

Output:

95.0
90.0
85.0
The average of the grades is95.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote