Welcome to Dream.In.Code
Become a Java Expert!

Join 149,596 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,888 people online right now. Registration is fast and FREE... Join Now!




Using arrays, tons of loops, etc.

 
Reply to this topicStart new topic

Using arrays, tons of loops, etc., Please help me work this logically

jreibe3000
9 Oct, 2007 - 10:45 AM
Post #1

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

jreibe3000
RE: Using Arrays, Tons Of Loops, Etc.
9 Oct, 2007 - 04:08 PM
Post #2

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
Hello everyone,

I was just wondering if there is a particular reason why my post isn't getting any replies... am I doing something wrong, or not following the proper procedures??

Thanks,
Jake
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Using Arrays, Tons Of Loops, Etc.
9 Oct, 2007 - 04:28 PM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
NO. This is a forum be patient... and it would be nice of you to upload the sample .txt file, so that some one could play around with it
User is offlineProfile CardPM
+Quote Post

jreibe3000
RE: Using Arrays, Tons Of Loops, Etc.
9 Oct, 2007 - 06:56 PM
Post #4

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
QUOTE(PennyBoki @ 9 Oct, 2007 - 05:28 PM) *

NO. This is a forum be patient... and it would be nice of you to upload the sample .txt file, so that some one could play around with it

Thank you for the suggestion PennyBoki. I have attached the accompanying txt file for this assignment that I'm stuck on.


Attached File(s)
Attached File  baseball.txt ( 225bytes ) Number of downloads: 11
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Using Arrays, Tons Of Loops, Etc.
10 Oct, 2007 - 08:20 AM
Post #5

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Well I believe that you're wrong about that most of the code is done. But take it easy, in time it will be completely done. But it will be done only if you try to. Now this code you've posted in the first post, has it's errors and only reads the file and prints it. Now I didn't see any matrix in the except in the sorting. So what I think the assignment wants from you is to read the file and the first four elements would be the elements of the first row, the second four elements would be the elements of the second row... and the 20-th four elements would be the 20-th row. AND THEN do the sorting printing whatever... Now I cleaned the code a little, it still doesn't do what is suppose to, but it compiles. So think this though.

Baseball.java
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
        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;k++)
                    {
                        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)
    {
      int  total=0;
        for(int i=0;i<size;i++)
            total+=total+array1[i][k];
            
            return total;
    }

    
}



BaseballDriver.java
CODE
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();
        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
        
        //System.out.println("\n===================================\n");  
        //System.out.println("hits: "+hits+" walks: "+walks+" outs: "+outs);
        String myFile=getFileName();
        
        FileReader freader=new FileReader(myFile);
        BufferedReader inputFile=new BufferedReader(freader);    
            
        temporary.readFromFile(inputFile);
        inputFile.close();
        temporary.print();
      
        
          
    }
    
    
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month