In: Computer Science
I'm looking to calculate grades that allows me to enter a number of grades and allows me to also calculate the average using java script collections/array list. If possible, can someone lead me in the direction on how to go about doing this with some explanation. Thank you !!!
My code so far;
package com.company; import java.util.*; import java.util.ArrayList; class Main { public static void main(String args[]) { Set names = new HashSet(); names.add("Christopher"); names.add("Pedro"); names.add("James"); names.add("Jess"); } }
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Test {
//method to calculate the average of grades in ArrayList
static double calAverage(ArrayList<Integer> a)
{
int n =a.size();
int sum =0;//to store sum
for(int i=0;i<n;i++)//calculating sum of all grades
sum=sum+a.get(i);
//finding average
double av = sum/(n*(1.0));
return av;//returning average
}
public static void main(String argv[])
{
ArrayList<Integer> R = new
ArrayList<Integer>();//creating array list with integer
object
int n;//to store number of grades
//to read input
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of grades you to enter:");
n = sc.nextInt();
//reading grades
System.out.println("Enter grades :");
for(int i=0;i<n;i++)
{
int k =sc.nextInt();
R.add(k);//adding to array list
}
//displaying output
System.out.println("The average of grades :"+calAverage(R));
}
}
output:
run:
Enter number of grades you to enter:5
Enter grades :
80
74
69
55
92
The average of grades :74.0
BUILD SUCCESSFUL (total time: 16 seconds)