Sum of Vectors

Thread Solved

Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Sum of Vectors

 
0
  #1
Oct 28th, 2007
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:


  1.  
  2. import java.text.DecimalFormat;
  3. import java.util.*;
  4. public class Assignment7{
  5.  
  6. public Vector boardScores = new Vector();
  7. public Vector avgVector = new Vector();
  8. public int numOfTeams = 0;
  9. public int numOfGames = 0;
  10. public int youScored;
  11. public int MintScore;
  12. public int RepeatNumbers;
  13.  
  14. public static void main(String args[]){
  15. Assignment7 myStatsBoard = new Assignment7();
  16. myStatsBoard.killMe();
  17. }
  18.  
  19. public Assignment7(){
  20. }
  21.  
  22. public void killMe(){
  23. readNumberOfTeams();
  24. readNumberOfGames();
  25. getTheGameScores();
  26. calculateTheAverage();
  27. displayStatsBoard();
  28. }
  29.  
  30. public void readNumberOfTeams(){
  31. int teamNumbers = 0;
  32. Random myRandomTeam = new Random();
  33. boolean stopTheRunaround = false;
  34. while(!stopTheRunaround){
  35. teamNumbers = myRandomTeam.nextInt(10);
  36. if (teamNumbers > 5){
  37. stopTheRunaround = true;
  38. }
  39. }
  40. setNumberOfTeams(teamNumbers);
  41. }
  42.  
  43. public void readNumberOfGames(){
  44. Random myRandomTeam = new Random();
  45. int gameNumber = myRandomTeam.nextInt(12);
  46. while(gameNumber <= 6){
  47. gameNumber = myRandomTeam.nextInt(12);
  48. }
  49. setNumberOfGames(gameNumber);
  50. }
  51.  
  52. public void getTheGameScores(){
  53. RepeatNumbers = 0; //I initialize RepeatNumbers to
  54. for (int i = 0; i < getNumberOfTeams(); i ++){ //use to capture numbers from
  55. Vector teamVector = new Vector(); //vector.
  56. RepeatNumbers = (Integer) teamVector.get(i); //<-- Test if this will get me
  57. for (int j = 0; j < getNumberOfGames(); j++){//the numbers vertically, but
  58. teamVector.add("" + getMyNumberNow()); //creates error when
  59. } //trying to build program.
  60. boardScores.add(teamVector);
  61. }
  62. }
  63.  
  64. public int getMyNumberNow(){
  65. Random myRandomTeam = new Random();
  66. int nextStupidRandomNumber = 0;
  67. while(nextStupidRandomNumber < 1 || nextStupidRandomNumber > 150){
  68. nextStupidRandomNumber = myRandomTeam.nextInt(1500);
  69. }
  70. return nextStupidRandomNumber;
  71. }
  72.  
  73. public void calculateTheAverage(){
  74. int TotalScore = 0;
  75. for (int i = 0; i < getNumberOfTeams(); i++){
  76. Vector teamVector = (Vector) boardScores.get(i);
  77. for (int j = 0; j < getNumberOfGames(); j++){
  78. String whatScore = (String) teamVector.get(j);
  79. MintScore = Integer.parseInt(whatScore);
  80. youScored = youScored + MintScore;
  81. }
  82. double averageScore = (double) youScored / getNumberOfGames();
  83. avgVector.add("" + averageScore);
  84. }
  85. }
  86.  
  87. public void displayStatsBoard(){
  88. DecimalFormat format = new DecimalFormat("###,###,##0.00");
  89. System.out.println("\n\nThis is Steven Peterson's Scorecard\n\n");
  90. System.out.println("Total Teams: " + getNumberOfTeams());
  91. System.out.println("Total Games: " + getNumberOfGames());
  92. System.out.println("\n\nTeam Scoreboard\n");
  93. System.out.print("\t\t");
  94. for (int k = 0; k < getNumberOfGames(); k++){
  95. System.out.print("Game" + (k + 1) + "\t");
  96. }
  97. System.out.print("Average");
  98. for (int i = 0; i < getNumberOfTeams(); i ++){
  99. System.out.print("\nTeam " + (i + 1) + "\t\t");
  100. Vector teamVector = (Vector) boardScores.get(i);
  101. for (int j = 0; j < getNumberOfGames(); j++){
  102. String whatScore = (String) teamVector.get(j);
  103. System.out.print(whatScore + "\t");
  104. }
  105. String avg = (String) avgVector.get(i);
  106. double dblAvg = Double.parseDouble(avg);
  107. System.out.print(format.format(dblAvg));
  108. }
  109. System.out.println(""); System.out.println("");
  110. }
  111.  
  112. public int getNumberOfTeams(){
  113. return numOfTeams;
  114. }
  115.  
  116. public int getNumberOfGames(){
  117. return numOfGames;
  118. }
  119.  
  120. public void setNumberOfGames(int _numberOfGames){
  121. numOfGames = _numberOfGames;
  122. }
  123.  
  124. public void setNumberOfTeams(int _numberOfTeams){
  125. numOfTeams = _numberOfTeams;
  126. }
  127. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,427
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Sum of Vectors

 
0
  #2
Oct 29th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Sum of Vectors

 
0
  #3
Oct 29th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,427
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Sum of Vectors

 
0
  #4
Oct 29th, 2007
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.
  1. Vector teams = new Vector(numberOfTeams);
  2. for (int i=0; i<numberOfTeams; i++){
  3. Vector team = new Vector(numberOfGames);
  4. for (int j=0; j<numberOfGames; j++){
  5. int gameScore = generateScore(); // some method to generate a random score
  6. team.add( gameScore );
  7. }
  8. teams.add( team );
  9. }
Doing the math on these is now just a matter of writing the loops over the teams and their scores.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Sum of Vectors

 
0
  #5
Oct 30th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,427
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Sum of Vectors

 
0
  #6
Oct 31st, 2007
Well, it kind of replaces all of these methods
  1. readNumberOfTeams();
  2. readNumberOfGames();
  3. 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.
Last edited by Ezzaral; Oct 31st, 2007 at 11:58 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Sum of Vectors

 
0
  #7
Nov 1st, 2007
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:


  1. import java.text.DecimalFormat;
  2. import java.util.*;
  3.  
  4. public class Assignment7 {
  5. public Vector sumTeams = new Vector();
  6. public Vector getNumGames = new Vector();
  7. public double teamAverate = 0;
  8. private Vector vecTeams = new Vector();
  9. private int NumGames;
  10. private int NumTeams;
  11. private static final int MinGames = 1;
  12. private static final int MinTeams = 1;
  13. private static final int MinScore = 0;
  14. private static final int MaxScore = 100;
  15.  
  16. public static void main(String[] args) {
  17. Assignment7 gameBoard = new Assignment7();
  18. gameBoard.execute();
  19. }
  20.  
  21. public Assignment7(){
  22. Scanner input = new Scanner(System.in);
  23. System.out.println("Welcome to the scoreboard\n\n");
  24. System.out.print("Enter maximum number of teams: ");
  25. setNumTeams(getRandom(MinTeams,input.nextInt()));
  26. System.out.print("Enter maximum number of games: ");
  27. setNumGames(getRandom(MinGames,input.nextInt()));
  28. System.out.println("\n\n");
  29. }
  30.  
  31. public void execute(){
  32. PopulateVector();
  33. ShowResults();
  34. }
  35.  
  36. private void PopulateVector () {
  37. Vector vecGames;
  38. for (int i = 0; i < getNumTeams(); i++) {
  39. vecGames = new Vector();
  40. for (int j = 0; j < getNumGames(); j++) {
  41. vecGames.add(getRandom(MinScore, MaxScore));
  42. }
  43. vecTeams.add(vecGames);
  44. }
  45. }
  46.  
  47. private void ShowResults () {
  48.  
  49. Vector sumGames = new Vector();
  50. Integer tempSumGames = 0;
  51. Integer tempSumTeams = 0;
  52. for (int i = 0; i < getNumGames(); i++) {
  53. sumGames.add((Integer) 0);
  54. }
  55.  
  56. for (int i = 0; i < getNumTeams(); i++) {
  57. Vector myVec = (Vector) vecTeams.elementAt(i);
  58. for (int j = 0; j < getNumGames(); j++) {
  59. tempSumTeams = tempSumTeams + (Integer) myVec.elementAt(j);
  60. tempSumGames = (Integer) myVec.elementAt(j);
  61. sumGames.set(j, (Integer) sumGames.elementAt(j) + (Integer) myVec.elementAt(j));
  62. }
  63. sumTeams.add(tempSumTeams);
  64. tempSumTeams = 0;
  65. }
  66.  
  67. report(sumGames, sumTeams, vecTeams);
  68. }
  69.  
  70. private void report (Vector sumGame, Vector sumTeam, Vector Data){
  71. DecimalFormat format = new DecimalFormat("###,###,##0.00");
  72. boolean isFirst = true;
  73. for (int i = 0; i < getNumTeams(); i++) {
  74. Vector myVec = (Vector) Data.elementAt(i);
  75. if (isFirst) {
  76. System.out.print("\t\t");
  77. for (int j = 0; j < getNumGames(); j++) {
  78. System.out.print("Game" + (j + 1) + "\t\t");
  79. }
  80. System.out.print("Average\n");
  81. isFirst = false;
  82. }
  83. System.out.print("Team" + (i + 1) + "\t\t");
  84. int teamTotal = 0;
  85. int gamePlay = 0;
  86. double DteamTotal = 0;
  87. double DgamePlay = 0;
  88. double teamAverage = 0;
  89. for (int j = 0; j < getNumGames(); j++) {
  90. teamTotal = (Integer) sumTeams.elementAt(j);
  91. System.out.print("s" + (j +1) + ": " + sumTeams.elementAt(i) + " ");
  92. gamePlay = getNumGames();
  93. DteamTotal = teamTotal;
  94. DgamePlay = gamePlay;
  95. teamAverage = (DteamTotal / DgamePlay);
  96.  
  97. System.out.print(myVec.elementAt(j).toString() + "\t\t");
  98. }
  99. System.out.print(teamAverage + "\n");
  100. }
  101. System.out.print("\nAverage\t\t");
  102. for (int i = 0; i < getNumGames(); i++) {
  103. int finaltotal = (Integer) sumGame.elementAt(i);
  104. int numberofteams = (Integer) getNumTeams();
  105. double finalD = finaltotal;
  106. double teamsD = numberofteams;
  107. double avgGame = (finalD / teamsD);
  108. System.out.print(format.format(avgGame) + "\t\t");
  109. }
  110.  
  111. System.out.println("\n\n\n");
  112. System.out.println(sumTeams);
  113. System.out.println(getNumGames());
  114. }
  115.  
  116. private int getRandom(int min, int max) {
  117. return (int)Math.round( Math.random() * max + min );
  118. }
  119.  
  120. private int getNumGames() {
  121. return NumGames;
  122. }
  123.  
  124. private void setNumGames(int _NumGames) {
  125. NumGames = _NumGames;
  126. }
  127.  
  128. private int getNumTeams() {
  129. return NumTeams;
  130. }
  131.  
  132. private void setNumTeams(int _NumTeams) {
  133. NumTeams = _NumTeams;
  134. }
  135.  
  136. }
Last edited by speterson; Nov 1st, 2007 at 4:36 pm. Reason: Private Info
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Sum of Vectors

 
0
  #8
Nov 1st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Sum of Vectors

 
0
  #9
Nov 1st, 2007
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:

  1.  
  2. import java.text.DecimalFormat;
  3. import java.util.*;
  4.  
  5. public class Assignment7 {
  6. public Vector sumTeams = new Vector();
  7. public Vector getNumGames = new Vector();
  8. public double teamAverate = 0;
  9. private Vector vecTeams = new Vector();
  10. private int NumGames;
  11. private int NumTeams;
  12. private static final int MinGames = 1;
  13. private static final int MinTeams = 1;
  14. private static final int MinScore = 0;
  15. private static final int MaxScore = 100;
  16.  
  17. public static void main(String[] args) {
  18. Assignment7 gameBoard = new Assignment7();
  19. gameBoard.execute();
  20. }
  21.  
  22. public Assignment7(){
  23. Scanner input = new Scanner(System.in);
  24. System.out.println("Welcome to the scoreboard\n\n");
  25. System.out.print("Enter maximum number of teams: ");
  26. setNumTeams(getRandom(MinTeams,input.nextInt()));
  27. System.out.print("Enter maximum number of games: ");
  28. setNumGames(getRandom(MinGames,input.nextInt()));
  29. System.out.println("\n\n");
  30. }
  31.  
  32. public void execute(){
  33. PopulateVector();
  34. ShowResults();
  35. }
  36.  
  37. private void PopulateVector () {
  38. Vector vecGames;
  39. for (int i = 0; i < getNumTeams(); i++) {
  40. vecGames = new Vector();
  41. for (int j = 0; j < getNumGames(); j++) {
  42. vecGames.add(getRandom(MinScore, MaxScore));
  43. }
  44. vecTeams.add(vecGames);
  45. }
  46. }
  47.  
  48. private void ShowResults () {
  49.  
  50. Vector sumGames = new Vector();
  51. Integer tempSumGames = 0;
  52. Integer tempSumTeams = 0;
  53. for (int i = 0; i < getNumGames(); i++) {
  54. sumGames.add((Integer) 0);
  55. }
  56.  
  57. for (int i = 0; i < getNumTeams(); i++) {
  58. Vector myVec = (Vector) vecTeams.elementAt(i);
  59. for (int j = 0; j < getNumGames(); j++) {
  60. tempSumTeams = tempSumTeams + (Integer) myVec.elementAt(j);
  61. tempSumGames = (Integer) myVec.elementAt(j);
  62. sumGames.set(j, (Integer) sumGames.elementAt(j) + (Integer) myVec.elementAt(j));
  63. }
  64. sumTeams.add(tempSumTeams);
  65. tempSumTeams = 0;
  66. }
  67.  
  68. report(sumGames, sumTeams, vecTeams);
  69. }
  70.  
  71. private void report (Vector sumGame, Vector sumTeam, Vector Data){
  72. DecimalFormat format = new DecimalFormat("###,###,##0.00");
  73. boolean isFirst = true;
  74. for (int i = 0; i < getNumTeams(); i++) {
  75. Vector myVec = (Vector) Data.elementAt(i);
  76. if (isFirst) {
  77. System.out.print("\t\t");
  78. for (int j = 0; j < getNumGames(); j++) {
  79. System.out.print("Game" + (j + 1) + "\t\t");
  80. }
  81. System.out.print("Average\n");
  82. isFirst = false;
  83. }
  84. System.out.print("Team" + (i + 1) + "\t\t");
  85. int teamTotal = 0;
  86. int gamePlay = 0;
  87. double DteamTotal = 0;
  88. double DgamePlay = 0;
  89. double teamAverage = 0;
  90. for (int j = 0; j < getNumGames(); j++) {
  91. teamTotal = (Integer) sumTeams.elementAt(i);
  92. gamePlay = getNumGames();
  93. DteamTotal = teamTotal;
  94. DgamePlay = gamePlay;
  95. teamAverage = (DteamTotal / DgamePlay);
  96.  
  97. System.out.print(myVec.elementAt(j).toString() + "\t\t");
  98. }
  99. System.out.print(format.format(teamAverage) + "\n");
  100. }
  101. System.out.print("\nAverage\t\t");
  102. for (int i = 0; i < getNumGames(); i++) {
  103. int finaltotal = (Integer) sumGame.elementAt(i);
  104. int numberofteams = (Integer) getNumTeams();
  105. double finalD = finaltotal;
  106. double teamsD = numberofteams;
  107. double avgGame = (finalD / teamsD);
  108. System.out.print(format.format(avgGame) + "\t\t");
  109. }
  110.  
  111. System.out.println("\n\n\n");
  112.  
  113. }
  114.  
  115. private int getRandom(int min, int max) {
  116. return (int)Math.round( Math.random() * max + min );
  117. }
  118.  
  119. private int getNumGames() {
  120. return NumGames;
  121. }
  122.  
  123. private void setNumGames(int _NumGames) {
  124. NumGames = _NumGames;
  125. }
  126.  
  127. private int getNumTeams() {
  128. return NumTeams;
  129. }
  130.  
  131. private void setNumTeams(int _NumTeams) {
  132. NumTeams = _NumTeams;
  133. }
  134.  
  135. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,427
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Sum of Vectors

 
0
  #10
Nov 1st, 2007
Ok, for the randoms I would use
  1. private java.util.Random random = new java.util.Random(System.currentTimeMillis());
and in your method
  1. private int getRandom(int min, int max) {
  2. return random.nextInt(max);
  3. }
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.
Last edited by Ezzaral; Nov 1st, 2007 at 5:01 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC