topLM6
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Demo Program - Working with Arrays

Task:  Input 25 numerical grades into an array, print the grades, and print the average of the grades.
 

import java.util.Scanner;

public class Demo1
{
   public static void main(String[] args)
   {
     Scanner reply = new Scanner(System.in);
     final int sizeOfArray = 25;
     int [ ] grades = new int [sizeOfArray];
     int total = 0;

     // Fill the array
     for (int i = 0; i < sizeOfArray; i++)
     {
          System.out.println("Please enter grade: ");
          grades[i] = reply.nextInt();
          total += grades[i];
     }

     // Print the array
     for (int i = 0; i < sizeOfArray; i++)
     {
           System.out.println(grades[i]);
     }
   



What is the advantage of using a CONSTANT (final) to represent the size of the array?

 

What is the purpose of the counter total ?

 

 

 

 

What is the purpose of using a cast to (double) for finding the average?
      // Find the average
     
double average = (double) total/sizeOfArray;
      System.out.println("\nAverage of grades is " + average + ".");
      }  
}

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com