Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

FORMULAS FOR TOTAL AND AVERAGE IN JAVA ARRAYS w/RANDOM GENERATORS

Attached is a run of the current state of my Java program. I have added formulas to calculate totals and a row average. Could someone review my code to see if works and also maybe where I should put it. Also, the formulas are supposed to output their results into my array, but I'm a little unclear as to how to get my format right so the results go to the right places (in the Total column (the total of the row (quarter) values)) and in the Final Total row (the total of the columns (games) values)). Also, I need a suggestion on how to store the results of each updated game, until the user chooses to "Quit" and then the results of any and all updated games should display.

Thorough and prompt assistance is very, very much appreciated.

-----------------------------------HERE IS A RUN OF MY PROGRAM-------------------------------

COLLEGE FOOTBALL TEAM GAME SCORES

__________________________________________________________
Games#                                      Quarters                                     Totals
_______________1________2________3________4_______________

1                           0               0               0               0                  0

2                           0               0               0               0                  0

3                           0               0               0               0                  0

4                           0               0               0               0                  0

5                           0               0               0               0                  0

6                           0               0               0               0                  0

7                           0               0               0               0                  0

8                           0               0               0               0                  0

9                           0               0               0               0                  0

10                           0               0               0               0                  0

11                           0               0               0               0                  0

12                           0               0               0               0                  0

13                           0               0               0               0                  0

14                           0               0               0               0                  0

15                           0               0               0               0                  0

16                           0               0               0               0                  0

17                           0               0               0               0                  0

18                           0               0               0               0                  0

19                           0               0               0               0                  0

20                           0               0               0               0                  0

__________________________________________________________
Final Totals
__________________________________________________________


Enter the number of the game you want to update:  20
The game that will be updated is: 20

                              COLLEGE FOOTBALL TEAM GAME SCORES
          UPDATED BY USER ON: Fri Apr 28 19:39:06 PDT 2006

__________________________________________________________
Games#                                      Quarters                                     Totals
_______________1________2________3________4_______________
1                           0               0               0               0                  0

2                           0               0               0               0                  0

3                           0               0               0               0                  0

4                           0               0               0               0                  0

5                           0               0               0               0                  0

6                           0               0               0               0                  0

7                           0               0               0               0                  0

8                           0               0               0               0                  0

9                           0               0               0               0                  0

10                           0               0               0               0                  0

11                           0               0               0               0                  0

12                           0               0               0               0                  0

13                           0               0               0               0                  0

14                           0               0               0               0                  0

15                           0               0               0               0                  0

16                           0               0               0               0                  0

17                           0               0               0               0                  0

18                           0               0               0               0                  0

19                           0               0               0               0                  0

20                           5               21               6               27                  0
__________________________________________________________
Final Totals
__________________________________________________________


-----------------------------------HERE IS MY PROGRAM CODE-------------------------------

// import java packages
import java.lang.Math; //use to avoid generating negative numbers
import java.util.*;
import java.io.*;

// classname
public class FootballScoresArray2
{
      public static void main(String[] args) throws IOException
      {
      
            
      //hard code constants and declare variables
      int userInputGameNumber = 0; // number of the game  user enters into keyboard
      int QUARTERS_4 = 4;  //4 quarters per game
      int GAMES_20 = 20;  //20 games per season
      int oneGameQuartersTotal;//Total=1 game 4 quarter results
      int allGameQuartersFinalTotal;//Final Totals=20 games results
      double average; // computes average of the scores
      String quartersName, gameNumber;
      
      //parameterless constructor creates instance of random class
      Random generator = new Random();
      
      
      // Declare array of integers (base type).
      // Length = 4 elements (indices) by 20 elements (indices).
      // Initialize the elements of the array.
      
      
      System.out.println("                        \tCOLLEGE FOOTBALL TEAM GAME SCORES");
      System.out.println();
      System.out.println("__________________________________________________________ ");      
      System.out.println("Games#                                      Quarters                                     Totals");
      System.out.println("_______________1________2________3________4_______________ ");      
      System.out.println();
      int [][] scores = new int[GAMES_20][QUARTERS_4];
      for(int g = 0; g < scores.length; g++)
      {
       for(int q = 0; q < scores[g].length; q++)
        {
              System.out.print((g + 1)             +                   "                           "); //g+1=columns at 1
              for (q = 0; q < scores[g].length; q++)
              System.out.print(scores[g][q]             +                   "               ");
              System.out.println("   0"); //IMPORTANT--THIS PUTS INFO IN THE TOTALS COLUMN
              System.out.println(); //changes games col
                                  
        }
      }
 
       System.out.println("__________________________________________________________ ");
      System.out.println("Final Totals");
      System.out.println("__________________________________________________________ ");
      System.out.println();
      System.out.println();
            
      //user inputs game number      
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter the number of the game you want to update:  ");
      userInputGameNumber = keyboard.nextInt();
      System.out.println("The game that will be updated is: " + userInputGameNumber);
      System.out.println();
      if (userInputGameNumber > 0) //start loop
      {
            //update array with user input
      System.out.println("                        \tCOLLEGE FOOTBALL TEAM GAME SCORES");
      //Calendar.getInstance().getTime() returns a java.util.Date object
      java.util.Calendar today = java.util.Calendar.getInstance();
      java.util.Date[] date = new java.util.Date[] { today.getTime() };
      System.out.println("    \tUPDATED BY USER ON: " + date[0]);
                              
      System.out.println();
      System.out.println("__________________________________________________________ ");      
      System.out.println("Games#                                      Quarters                                     Totals");
      System.out.println("_______________1________2________3________4_______________ ");      
            //System.out.println();
      
      for(int g = 0; g < scores.length; g++)
      {
       for(int q = 0; q < scores[g].length; q++)
        {
        System.out.print((g + 1)             +                   "                           "); //g+1=columns at 1
        for (q = 0; q < scores[g].length; q++)
        
        System.out.print(scores[g][q]             +                   "               ");
        System.out.println("   0"); //IMPORTANT--THIS PUTS INFO IN THE TOTALS COLUMN
        System.out.println(); //changes games col.
                
              //get an integer for random number generator
      scores[userInputGameNumber - 1][0]= Math.abs (generator.nextInt() % 33);
      scores[userInputGameNumber - 1][1]= Math.abs (generator.nextInt() % 33);
       scores[userInputGameNumber - 1][2]= Math.abs (generator.nextInt() % 33);
       scores[userInputGameNumber - 1][3]= Math.abs (generator.nextInt() % 33);
        } //this brace ends if loop -- DON'T DELETE
      
             
       
              
        }
      }
            
---------------------HERE IS THE SEPARATE CODE FOR MY FORMULAS------------------------

                //compute quarters totals (Total)
        int oneGameQuartersTotal = 0;
        for(int g = 0; g < scores.length; g++)
      {
       for(int q = 0; q < scores[g].length; q++)
        {
             oneGameQuartersTotal += scores[q];
        }
        oneGameQuartersTotal = oneGameQuartersTotal + scores.length;
        System.out.println(+ oneGameQuartersTotal);
       
       //compute games totals (Final Totals)
       int allGameQuartersFinalTotal = 0;
       for(int g = 0; g < scores.length; g++)
      {
       for(int q = 0; q < scores[g].length; q++)
        {
             allGameQuartersFinalTotal += scores[g];
        }
        allGameQuartersFinalTotal = allGameQuartersFinalTotal + scores.length;
        System.out.println(+ allGameQuartersFinalTotal);
        
        // compute Average
        double sumAverage = 0.0;
        for(int g = 0; g < scores.length; g++)
      {
       for(int q = 0; q < scores[g].length; q++)
        {
             sumAverage += scores[g];
        }
        double average = sumAverage/scores.length;
        System.out.println(+average);
            

      
0
computerese
Asked:
computerese
  • 2
1 Solution
 
ozoCommented:
             //compute quarters totals (Total)       for(int g = 0; g < scores.length; g++)
     {
       oneGameQuartersTotal = 0;      for(int q = 0; q < scores[g].length; q++)
       {
           oneGameQuartersTotal += scores[g][q];
       }
       System.out.println(+ oneGameQuartersTotal);
     }
      //compute games totals (Final Totals)
      allGameQuartersFinalTotal = 0;
      for(int g = 0; g < scores.length; g++)
     {
      for(int q = 0; q < scores[g].length; q++)
       {
           allGameQuartersFinalTotal += scores[g][q];
       }
       System.out.println(+ allGameQuartersFinalTotal);
     }
       // compute Average
       double sumAverage = 0.0;
       for(int g = 0; g < scores.length; g++)
     {
      for(int q = 0; q < scores[g].length; q++)
       {
           sumAverage += scores[g][q];
       }
     }
       average = sumAverage/scores.length;
       System.out.println(+average);
0
 
computereseAuthor Commented:
@ozo

Thanks for fixing the errors in my above formulas.
Just wondering if you (or someone) could suggest how to get the total for just one row, after the user has inputted the game to be updated and the random generator has run. Not sure how the loop would look. Here is some preliminary code I've written:

int total = 0;
for (int q = 0; q < scores.length; q++)
for (int g = 0; g < scores[q].length; g++)
total += scores[q][g];

And also how might I get the sum of the columns?

Your continued assistance is appreciated.
0
 
computereseAuthor Commented:
Can someone please address my last question? I would appreciate very much the assistance.

Thanks in advance.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now