简体   繁体   中英

Java - Two-Dimensional Arrays - Plotting Points

I have an assignment for a JAVA class I am taking. We are discussing two-dimensional arrays, however on this particular assignment, I can not figure out how to return back specific points and set those points with a specific value. Here is the assignment:

Write a method called create2DArray that will fill, create, and return a 10 x 10 2d array with random numbers in the range of 1 to 100. Write a method called print2DArray that will print a 10 x 10 2D array in row column fashion. Write a method called createCoords that will search the 2D array looking for any value that is evenly divisible by 3. Once you have found a number you should log the row, column location. This means when your method finishes it should produce a list of coordinates that I can use to plot my graph. This method must also return the number of coordinates that are divisible by 3 so that I know how many points there are to plot. I am not particular as to how the coordinates are returned back as long as I get a list of the row, column locations. So, I will leave it to you to work out a mechanism for returning the values. To test that you have logged the coordinates create another function called fillLocations that will fill the locations in the array you have logged with -1. So, your program should flow in this order

  1. create2DArray
  2. print2DArray
  3. createCoords
  4. fillLocations
  5. print2DArray

I understand and have completed create2DArray and print2DArray, but I can not figure out createCoords and fillLocations. Here is what I have so far, but it does not work and there are errors present:

public int createCoords(int row1, int col1){
int[][] coords = new int[row1][col1];  
int[][] count = new int[0][0];
int co = 0;
for(int row = 0; row < 10; row++)
{
    for(int col = 0; col < 10; col++)
    { 
    if(coords[row][col] % 3 == 0)
        co++;
        return count[row][col];
    }
}
return co;}
public int fillLocations(int[][] count){
int x = 0;
int y = 0;
for(int row = 0; row < 10; row++)
{
    for(int col = 0; col < 10; col++)
    {
    if(count[row][col] % 3 == 0)
        x = row;
        y = col;
        break;
    }
}
return (x, y);}

As a programmer you'll nearly always need to research for doing different things. This research will be easier when you divide your problem to smaller problems.

For example you need to generate random numbers? So search on google that and you'll find this: How do I generate random integers within a specific range in Java? .

You need to create and return a 2D array? Google and see Syntax for creating a two-dimensional array

And with your art, put the pieces of the puzzle together in a way that gives your desired result.

public int[][] create2DArray() {        
    int[][] newArray = new int[10][10];
    Random random = new Random();
    int range = 100;
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0;j<arr[0].length;j++)
        {
            newArray[i][j]= (random.nextInt(range) + 1);
        }
    }
    return newArray;
 }

This method, creates and returns a 10*10 2D array filled with random generated numbers between 1-100. I'm sure you can write the rest of your program and enjoy from it by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM