samedi 16 avril 2016

java method to log the row, column location

I am working on an assignment for an online JAVA class, and I have become stuck on a part of a problem. I am admittedly a complete newbie when it comes to JAVA, though I did well with BASIC I just can't wrap my head around some of this stuff. Here is what I need to do:

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. Some possibilities are:

-String

-Array

-2D Array

I am currently just unsure of what to do. Here is my code so far, where I believe I have createCoord down to where it divides by three and makes a count. What escapes me is the act of logging:

package lab14;

import java.util.Scanner;
import java.util.Random;

public class Lab14 {

public int[][] create2DArray()
{
    Random r = new Random();
    int[][] array = new int[10][10];
    for(int row = 1; row <= 10; row++)
    {
        for(int col = 1; col <= 10; col++)
        {
            array[row-1][col-1] = r.nextInt(100);
        }
    }
    return array;
}

public int createCoords(int[] array, int x)
        {
            int count = 0;
            for(int i = 0; i < x;i++)
            {
                if(array[i]% 3 == 0)
                {
                    count ++;
                }
            }
            return count;
        }

public void print2DArray(int[][] array)
{
    for(int row = 0; row < 10; row++)
    {
        for(int col = 0; col < 10; col++)
        {
            System.out.print(array[row][col] + "\t");
        }
        System.out.println("");
    }
}

public static void main(String[] args) 
{
    int ar[][];        
    Lab14 c = new Lab14();
    ar = c.create2DArray();
    c.print2DArray(ar);
}    
}




Aucun commentaire:

Enregistrer un commentaire