Hey everyone,
I'm working on a program dealing w/ 2-dimensional arrays. Many times when I ask for help, DIC members ask me to post the assignment instructions so I will post that first, followed by my code. I believe I have the majority of the code written, but I'm getting lost logically and need help getting at least step 6 from the following instructions coded, because I'm lost lost lost.
This lab deals with 2 dimensional arrays. Your class must implement a matrix that is 20X4.
1. Ask user for the name of a file. A sample file, baseball.txt is available for testing the program. Your file contains the following data:
• player number
• hits
• walks
• outs
Then the next player’s number, hits, walks and outs, etc.
2. Notice that each player is in the list only once. The player numbers are not in order.
3. Remember that the first element of a 2 dimensional array has index [0][0]. Once all players are read into the matrix, the matrix is to be sorted. The smallest player’s number should go into the matrix at position [0][0], the next smallest player’s number should be in position [1][0], etc.
4. When sorting, elements you must move the entire row. That means that to swap player 6 with player 2, you must swap 4 things, player number, hits, walks and outs.
1 2 2 2
6 0 5 1
3 0 0 6
2 4 2 0
when sorted becomes:
1 2 2 2
2 4 2 0
3 0 0 6
6 0 5 1
5. Your program should have a method to calculate the sum of the number of hits, a method to calculate the sum of the number of walks, and a method to calculate the number of outs total for a team of 9 players. If you are clever about writing the code, one method will do, and you just have to sum different columns.
6. Your program will extract nine players from the players read in by randomly generating the player’s number. The subteam of 9 of the players can be stored in a 2nd multidimensional array (defined to hold 9 team members).
7. Your class, Baseball, will hold a 2-dimensional array of integers. Nothing about hits, runs, etc. will appear in this class. It will also hold the count of the number of players read from the file.
8. Print all players in order and their statistics. Then print the team you have generated and the totals for the subteam. The printed players output should look like the following:
Player Hits Walks Outs
-------- ----- -------- ------
1 2 2 2
2 0 0 6
etc. Put 6 other players here.
17 4 2 0
Totals 17 14 20
//BELOW IS MY CODE SO FAR
CODE
import java.io.*;
import java.util.*;
public class Baseball
{
final int ROWS=20; //This is the max amount of rows possible
final int COLUMNS=4; //This is the max amount of columns possible
private int array1[][]; //1st array
private int size; //this will keep track of how many element in an array
private int array2[][]; //2nd array
public Baseball()
{
array1= new int [ROWS][COLUMNS]; //This array holds all data read from txt file
int size=0;
array2=new int [9][4]; //This array holds nine players in it
}
/*This method reads the txt file into array1
*/
public void readFromFile(BufferedReader inputFile)throws IOException
{
String str="string";
int row=0;
int col=0;
int temp;
int count=0;
while(row<ROWS && str!=null){
for(col=0;col<COLUMNS && str!=null;col++){
str=inputFile.readLine();
if(str!=null){
temp=Integer.parseInt(str);
array1[row][col]=temp;
}
}
row++;
count++;
}
count=count-1;
size=count; //keep track of elements
}
public void print() //prints contents of array
{
for (int row = 0; row < size; row++){
for (int col = 0; col < COLUMNS; col++){
System.out.println(array1[row][col]);
}
}
}
/*bubble sorts array1 in ascending order by first # in rows
*/
public void sort(int[][]array1)
{
int maxElement;
int index;
int temp;
int k;
for(maxElement=size-1;maxElement>=0;maxElement--)
{
for(index=0;index<=maxElement-1;index++)
{
if(array1[index][0]>array1[index+1][0])
{
for(k=0;k<=4)
{
temp=array1[index][k];
array1[index][k]=array1[index +1][k];
array1[index+1][k]=temp;
}
}
}
}
return temp;
}
/*This adds all of any particular column
*/
public int columnSum(int k)
{
total=0;
for(int i=0;i<size;i++)
total+=total+array1[i][k];
return k;
}
}
import java.io.*;
import java.util.*;
public class BaseballDriver
{
/*This method is asking user to input filename
*/
public static String getFileName()
{
Scanner scan=new Scanner(System.in);
String myFile;
System.out.print("Please Enter the File Name and Path: ");
myFile=scan.nextLine();
return myFile;
}
public static void main(String[]args)throws IOException
{
Baseball temporary=new Baseball();
String myFile=getFileName();
FileReader freader=new FileReader(myFile);
BufferedReader inputFile=new BufferedReader(freader);
temporary.readFromFile(inputFile);
inputFile.close();
temporary.print();
int hits=temporary.columnSum(1); //column 1 values summed
int walks=temporary.columnSum(2);//column 2 values summed
int outs=temporary.columnSum(3);// column 3 values summed
}
}
Thanks so much for your programming expertise, and always being a great resource for a green programmer.