So I'm making a program for an assignment in class. We are working with array's. Basically, you have to have three attributes in the class: Number of players (which I made five), an array with the number of at-bats per player, and the number of hits per player.
My goal is to prompt the use to enter the number of at-bats and hits for each of the five players. Then the batting average is calculated. Then I check to see if the player has made the all star team. I then proceed to total value of hits for all the players (Having trouble with this part). Lastly, I put the total hits in smallest value to biggest value using a sorting algorithm.
Here's the code. I put the client in the same class.
import java.util.Scanner;
public class Test
{
static int numberOfPlayers = 5;
int [] numberOfHits;
int [] numberOfAtBats;
int battingAverage;
int [] totalHits;
public Test(int [] numberOfHits, int [] numberOfAtBats)
{
this.numberOfAtBats = numberOfAtBats;
this.numberOfHits = numberOfHits;
battingAverage = CalcBattingAverageInd(numberOfHits, numberOfAtBats);
System.out.println("Batting average for this player is " + battingAverage);//print batting average for that player
CheckForAllStar(battingAverage);//Call check for allstar method
totalHits = TotalHits(numberOfHits);//Evertime I add a "+" symbol, it responds with an error, so I can't add more to the total
}
//Manipulator method
public int CalcBattingAverageInd(int [] numberOfHits, int [] numberOfAtBats)
{
int battingAverage = numberOfAtBats.length/numberOfHits.length;
return battingAverage;
}
public int [] TotalHits(int [] totalHits)
{
totalHits = new int[numberOfHits.length];
return totalHits;
}
public void CheckForAllStar(int battingAverage)
{
this.battingAverage = battingAverage;
if (battingAverage >= 3)
System.out.println("Your an all-star!");
else
System.out.println("Sorry, you didn't make the all-star team");
}
public void Sort(int [] totalHits)//Selection Sort method the total Hits
{
int temp;
int max;
for (int i = 0; i < totalHits.length; i++)
{
max = indexOfLargestElement(totalHits, totalHits.length);
temp = totalHits[max];
totalHits[max] = totalHits[totalHits.length - i - 1];
totalHits[totalHits.length-i-1] = temp;
}
}
public static int indexOfLargestElement(int [] array, int size)
{
int index = 0;
for(int i = 0; i < size; i++ )
{
if (array[i] > array[index])
index = i;
}
return index;
}
public static void main(String args[])
{
int check = 0;
Scanner scan = new Scanner(System.in);
int [] inputHits = {};
int [] inputAtBats = {};
while(check < numberOfPlayers)
{
System.out.println("There are " + Test.numberOfPlayers + " players");
for(int i = 0; i < 1; i++)
{
System.out.println("Enter the number of at-bat attempts: ");
inputAtBats[i] = scan.nextInt();
System.out.println("Now enter the number of Hits: ");
inputHits[i] = scan.nextInt();
Test(inputHits, inputAtBats);//Error here: Cannot find symbol
}
++check;
}
}
}
I'm using all variables and arrays of type int because I'm still learning converting between array data types. I commented in the code where I'm having trouble. I made this same program without arrays, and it ran fine. But for some reason, I can't run it with arrays. Any help and correction would be great. Thanks.