In: Computer Science
Methods – Compute Grade
Please write a complete Java program, given the following information about (a few lines of code in) main: projectAverage = getAverage(”Project”); // returns average of 2 grades testAverage = getAverage(”Test”); // returns average of 2 grades displayGrade(projectAverage, testAverage); // test are 70% & projects 30%
Solution for the above question are as follows -
Code :
import java.util.Scanner;
public class Main
{
// calculate average and return
public static int getAverage(int a[]) {
int sum = 0;
for(int i = 0; i < a.length; i++)
{
sum += a[i];
}
return (sum / a.length);
}
// print the grades Average
public static void displayGrade(int p , int t) {
System.out.println("Project Grades Average : " + p + "%");
System.out.println("Test Grades Average : " + t + "%");
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
// get input from user
int projects[] = new int[2];
int tests[] = new int[2];
// get the project data
System.out.println("Enter 2 grades of projects:");
for(int i = 0; i < 2; i++)
{
projects[i] = s.nextInt();
}
// get the test data
System.out.println("Enter 2 grades of tests:");
for(int i = 0; i < 2; i++)
{
tests[i] = s.nextInt();
}
// calculate projects average
int projectAverage = getAverage(projects);
// calculate tests average
int testAverage = getAverage(tests);
// display grades of projects and tests
displayGrade(projectAverage, testAverage);
}
}
Code Screen Shot :
Output :