In my Java class, we have to randomly create basketball teams and random scores, then average the scores for each team, and average the scores for each game. I am not sure how to average the score for each game, meaning adding scores vertically in a vector versus adding scores horizontally. I have tried using a totaling mechanism in any of the for loops, but of course they all added across, not vertically. What I would like to do is just total the scores vertically, and use seperate code to get the average, much easier I think. The code so far is below:

import java.text.DecimalFormat;
import java.util.*;
public class Assignment7{
 
     public Vector boardScores = new Vector();
     public Vector avgVector = new Vector();
     public int numOfTeams = 0;
     public int numOfGames = 0;
     public int youScored;
     public int MintScore;
     public int RepeatNumbers;
 
     public static void main(String args[]){
          Assignment7 myStatsBoard = new Assignment7();
          myStatsBoard.killMe();
     }
 
     public Assignment7(){
     }
 
     public void killMe(){
          readNumberOfTeams();
          readNumberOfGames();
          getTheGameScores();
          calculateTheAverage();
          displayStatsBoard();
     }
 
     public void readNumberOfTeams(){
          int teamNumbers = 0;
          Random myRandomTeam = new Random();
          boolean stopTheRunaround = false;
          while(!stopTheRunaround){
               teamNumbers = myRandomTeam.nextInt(10);
               if (teamNumbers > 5){
                    stopTheRunaround = true;
               }
          }
          setNumberOfTeams(teamNumbers);
     }
 
     public void readNumberOfGames(){
          Random myRandomTeam = new Random();
          int gameNumber = myRandomTeam.nextInt(12);
          while(gameNumber <= 6){
               gameNumber = myRandomTeam.nextInt(12);
          }
          setNumberOfGames(gameNumber);
     }
 
     public void getTheGameScores(){
          RepeatNumbers = 0;                                //I initialize RepeatNumbers to
          for (int i = 0; i < getNumberOfTeams(); i ++){    //use to capture numbers from 
               Vector teamVector = new Vector();            //vector.
               RepeatNumbers = (Integer) teamVector.get(i); //<--  Test if this will get me 
               for (int j = 0; j < getNumberOfGames(); j++){//the numbers vertically, but 
                    teamVector.add("" + getMyNumberNow());  //creates error when
               }                                            //trying to build program.
               boardScores.add(teamVector);
          }
     }
 
     public int getMyNumberNow(){
          Random myRandomTeam = new Random();
          int nextStupidRandomNumber = 0;
          while(nextStupidRandomNumber < 1 || nextStupidRandomNumber > 150){
               nextStupidRandomNumber = myRandomTeam.nextInt(1500);
          }
          return nextStupidRandomNumber;
     }
 
     public void calculateTheAverage(){
          int TotalScore = 0;
          for (int i = 0; i < getNumberOfTeams(); i++){
               Vector teamVector = (Vector) boardScores.get(i);
               for (int j = 0; j < getNumberOfGames(); j++){
                    String whatScore = (String) teamVector.get(j);
                    MintScore = Integer.parseInt(whatScore);
                    youScored = youScored + MintScore;
               }
               double averageScore = (double) youScored / getNumberOfGames();
               avgVector.add("" + averageScore);
          }
     }
 
     public void displayStatsBoard(){
          DecimalFormat format = new DecimalFormat("###,###,##0.00");
          System.out.println("\n\nThis is Steven Peterson's Scorecard\n\n");
          System.out.println("Total Teams: " + getNumberOfTeams());
          System.out.println("Total Games: " + getNumberOfGames());
          System.out.println("\n\nTeam Scoreboard\n");
          System.out.print("\t\t");
          for (int k = 0; k < getNumberOfGames(); k++){
               System.out.print("Game" + (k + 1) + "\t");
          }
          System.out.print("Average");
          for (int i = 0; i < getNumberOfTeams(); i ++){
               System.out.print("\nTeam " + (i + 1) + "\t\t");
               Vector teamVector = (Vector) boardScores.get(i);
               for (int j = 0; j < getNumberOfGames(); j++){
                    String whatScore = (String) teamVector.get(j);
                    System.out.print(whatScore + "\t");
               }
               String avg = (String) avgVector.get(i);
               double dblAvg = Double.parseDouble(avg);
               System.out.print(format.format(dblAvg));
          }
          System.out.println(""); System.out.println("");
     }
 
     public int getNumberOfTeams(){
          return numOfTeams;
     }
 
     public int getNumberOfGames(){
          return numOfGames;
     }
 
     public void setNumberOfGames(int _numberOfGames){
          numOfGames = _numberOfGames;
     }
 
     public void setNumberOfTeams(int _numberOfTeams){
          numOfTeams = _numberOfTeams;
     }
}

You need to keep a vector of vectors for this. The main vector to hold a vector for each team. The team vector would hold the score of each game the team has played. Averaging these by game and team is then just a matter of nesting the inner and outer loops in the correct order.

how would I go about this? I don't totally understand vectors, or where the vector of vectors should go. I thank you for your suggestions. In the mean time, I will take what you have suggested and see if I get it to work.

A vector is nothing more than an indexed collection of objects. A vector is also an object, which can therefore reside in a vector - a collection of collections. It's the same as a two dimensional array.

Vector teams = new Vector(numberOfTeams);
for (int i=0; i<numberOfTeams; i++){
  Vector team = new Vector(numberOfGames);
  for (int j=0; j<numberOfGames; j++){
    int gameScore = generateScore(); // some method to generate a random score
    team.add( gameScore );
  }
  teams.add( team );
}

Doing the math on these is now just a matter of writing the loops over the teams and their scores.

I am such a newb at programming. Where exactly would I put this code? There are several for loops already or is this a totally different method that I would have to create? I am just not grasping this whole thing, sorry guys if I come off like a moron, I am just trying to learn. I thank everyone for their help so far though.

Well, it kind of replaces all of these methods

readNumberOfTeams();
readNumberOfGames();
getTheGameScores();

to generate the data for team scores for each game. Your averages method(s) needs to loop through this info to make its calculations.

Alright, I have tried to rewrite it from scratch again with some help. There are two problems, but I think much smaller. The first is error correction, because sometimes the random will go above the limits, and the other is that it will average for each individual game now, but for the individual teams, it will average one correctly and the rest will copy that average. Sorry about the runtime errors that you get sometimes when trying to run the program. Anyways, the new code is below:

import java.text.DecimalFormat;
import java.util.*;

public class Assignment7 {
	public Vector sumTeams = new Vector();
	public Vector getNumGames = new Vector();
	public double teamAverate = 0;
    private Vector vecTeams = new Vector();
    private int NumGames;
    private int NumTeams;
    private static final int MinGames = 1;
    private static final int MinTeams = 1;
    private static final int MinScore = 0;
    private static final int MaxScore = 100;

    public static void main(String[] args) {
        Assignment7 gameBoard = new Assignment7();
        gameBoard.execute();
    }

    public Assignment7(){
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to the scoreboard\n\n");
        System.out.print("Enter maximum number of teams: ");
        setNumTeams(getRandom(MinTeams,input.nextInt()));
        System.out.print("Enter maximum number of games: ");
        setNumGames(getRandom(MinGames,input.nextInt()));
    	System.out.println("\n\n");
    }

    public void execute(){
        PopulateVector();
        ShowResults();
    }

    private void PopulateVector () {
        Vector vecGames;
        for (int i = 0; i < getNumTeams(); i++) {
            vecGames = new Vector();
            for (int j = 0; j < getNumGames(); j++) {
                vecGames.add(getRandom(MinScore, MaxScore));
            }
            vecTeams.add(vecGames);
        }
    }

    private void ShowResults () {

        Vector sumGames = new Vector();
        Integer tempSumGames = 0;
        Integer tempSumTeams = 0;
        for (int i = 0; i < getNumGames(); i++) {
            sumGames.add((Integer) 0);
        }

        for (int i = 0; i < getNumTeams(); i++) {
            Vector myVec = (Vector) vecTeams.elementAt(i);
            for (int j = 0; j < getNumGames(); j++) {
                tempSumTeams = tempSumTeams + (Integer) myVec.elementAt(j);
                tempSumGames = (Integer) myVec.elementAt(j);
                sumGames.set(j, (Integer) sumGames.elementAt(j) + (Integer) myVec.elementAt(j));
            }
            sumTeams.add(tempSumTeams);
            tempSumTeams = 0;
        }

        report(sumGames, sumTeams, vecTeams);
    }

    private void report (Vector sumGame, Vector sumTeam, Vector Data){
        DecimalFormat format = new DecimalFormat("###,###,##0.00");
        boolean isFirst = true;
        for (int i = 0; i < getNumTeams(); i++) {
            Vector myVec = (Vector) Data.elementAt(i);
            if (isFirst) {
                System.out.print("\t\t");
                for (int j = 0; j < getNumGames(); j++) {
                    System.out.print("Game" + (j + 1) + "\t\t");
            	}
            	System.out.print("Average\n");
                isFirst = false;
            }
			System.out.print("Team" + (i + 1) + "\t\t");
            int teamTotal = 0;
            int gamePlay = 0;
           double DteamTotal = 0;
           double DgamePlay = 0;
           double teamAverage = 0;
           for (int j = 0; j < getNumGames(); j++) {
				teamTotal = (Integer) sumTeams.elementAt(j);
				System.out.print("s" + (j +1) + ": " + sumTeams.elementAt(i) + " ");
				gamePlay = getNumGames();
				DteamTotal = teamTotal;
				DgamePlay = gamePlay;
				teamAverage = (DteamTotal / DgamePlay);

			System.out.print(myVec.elementAt(j).toString() + "\t\t");
			}
        	System.out.print(teamAverage + "\n");
        }
        System.out.print("\nAverage\t\t");
        for (int i = 0; i < getNumGames(); i++) {
      		int finaltotal = (Integer) sumGame.elementAt(i);
      		int numberofteams = (Integer) getNumTeams();
			double finalD = finaltotal;
			double teamsD = numberofteams;
			double avgGame = (finalD / teamsD);
          	System.out.print(format.format(avgGame) + "\t\t");
        }

		System.out.println("\n\n\n");
		System.out.println(sumTeams);
		System.out.println(getNumGames());
	}

    private int getRandom(int min, int max) {
        return (int)Math.round( Math.random() * max + min );
    }

    private int getNumGames() {
        return NumGames;
    }

    private void setNumGames(int _NumGames) {
        NumGames = _NumGames;
    }

    private int getNumTeams() {
        return NumTeams;
    }

    private void setNumTeams(int _NumTeams) {
        NumTeams = _NumTeams;
    }

}

Ok, I feel like an idiot...

I found out what was wrong with the new code. Line 90 shows a pointer j, but originally it was i. When I changed the pointer to i, the averages were working, which unless I am having some weird luck, there should be no runtime errors.

Now, I hope no one is trying to take the code for themselves, as this is for my class, and the last thing I want to see is everyone using this code, but use it as an example. The new corrected code looks like this:

import java.text.DecimalFormat;
import java.util.*;

public class Assignment7 {
	public Vector sumTeams = new Vector();
	public Vector getNumGames = new Vector();
	public double teamAverate = 0;
    private Vector vecTeams = new Vector();
    private int NumGames;
    private int NumTeams;
    private static final int MinGames = 1;
    private static final int MinTeams = 1;
    private static final int MinScore = 0;
    private static final int MaxScore = 100;

    public static void main(String[] args) {
        Assignment7 gameBoard = new Assignment7();
        gameBoard.execute();
    }

    public Assignment7(){
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to the scoreboard\n\n");
        System.out.print("Enter maximum number of teams: ");
        setNumTeams(getRandom(MinTeams,input.nextInt()));
        System.out.print("Enter maximum number of games: ");
        setNumGames(getRandom(MinGames,input.nextInt()));
    	System.out.println("\n\n");
    }

    public void execute(){
        PopulateVector();
        ShowResults();
    }

    private void PopulateVector () {
        Vector vecGames;
        for (int i = 0; i < getNumTeams(); i++) {
            vecGames = new Vector();
            for (int j = 0; j < getNumGames(); j++) {
                vecGames.add(getRandom(MinScore, MaxScore));
            }
            vecTeams.add(vecGames);
        }
    }

    private void ShowResults () {

        Vector sumGames = new Vector();
        Integer tempSumGames = 0;
        Integer tempSumTeams = 0;
        for (int i = 0; i < getNumGames(); i++) {
            sumGames.add((Integer) 0);
        }

        for (int i = 0; i < getNumTeams(); i++) {
            Vector myVec = (Vector) vecTeams.elementAt(i);
            for (int j = 0; j < getNumGames(); j++) {
                tempSumTeams = tempSumTeams + (Integer) myVec.elementAt(j);
                tempSumGames = (Integer) myVec.elementAt(j);
                sumGames.set(j, (Integer) sumGames.elementAt(j) + (Integer) myVec.elementAt(j));
            }
            sumTeams.add(tempSumTeams);
            tempSumTeams = 0;
        }

        report(sumGames, sumTeams, vecTeams);
    }

    private void report (Vector sumGame, Vector sumTeam, Vector Data){
        DecimalFormat format = new DecimalFormat("###,###,##0.00");
        boolean isFirst = true;
        for (int i = 0; i < getNumTeams(); i++) {
            Vector myVec = (Vector) Data.elementAt(i);
            if (isFirst) {
                System.out.print("\t\t");
                for (int j = 0; j < getNumGames(); j++) {
                    System.out.print("Game" + (j + 1) + "\t\t");
            	}
            	System.out.print("Average\n");
                isFirst = false;
            }
			System.out.print("Team" + (i + 1) + "\t\t");
            int teamTotal = 0;
            int gamePlay = 0;
           double DteamTotal = 0;
           double DgamePlay = 0;
           double teamAverage = 0;
           for (int j = 0; j < getNumGames(); j++) {
				teamTotal = (Integer) sumTeams.elementAt(i);
				gamePlay = getNumGames();
				DteamTotal = teamTotal;
				DgamePlay = gamePlay;
				teamAverage = (DteamTotal / DgamePlay);

			System.out.print(myVec.elementAt(j).toString() + "\t\t");
			}
        	System.out.print(format.format(teamAverage) + "\n");
        }
        System.out.print("\nAverage\t\t");
        for (int i = 0; i < getNumGames(); i++) {
      		int finaltotal = (Integer) sumGame.elementAt(i);
      		int numberofteams = (Integer) getNumTeams();
			double finalD = finaltotal;
			double teamsD = numberofteams;
			double avgGame = (finalD / teamsD);
          	System.out.print(format.format(avgGame) + "\t\t");
        }

		System.out.println("\n\n\n");

	}

    private int getRandom(int min, int max) {
        return (int)Math.round( Math.random() * max + min );
    }

    private int getNumGames() {
        return NumGames;
    }

    private void setNumGames(int _NumGames) {
        NumGames = _NumGames;
    }

    private int getNumTeams() {
        return NumTeams;
    }

    private void setNumTeams(int _NumTeams) {
        NumTeams = _NumTeams;
    }

}

Ok, for the randoms I would use

private java.util.Random random = new java.util.Random(System.currentTimeMillis());

and in your method

private int getRandom(int min, int max) {
        return random.nextInt(max);
    }

Your assignment indicated the score should be between 0-150, so just use 151 for the max value (the nextInt(n) returns 0 to n-1).
There was no requirement stated to randomize the number of teams and games so you needn't bother with that.

For the averages, split the calculations out of your report() method. Make one method that for a given team number "i" returns the team average across all games. Make another method that for a given game number "i" returns the game average across all teams. In your report method, call those functions as needed to generate the data for each of your table entries.

Remove the declared Vector getNumGames - you don't use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.