Write a method that takes a 2D array of integers as an argument. The array returns a new 2D array of integers of the same size (same number of rows and columns). The values in the returned array are the same as those in the argument array EXCEPT that any negative values are made positive.
My method will not compile the compiler keeps complaining about my method that its illegal start of expression. It appears to fails to recognize that I have written a method whats so ever.
This is what I have done:
public class Examprep1
{
public static void main (String[] args)
{
int [][] array = new int[2][4];
array[0][0]=1;
array[0][1]=5;
array[0][2]=-7;
array[0][3]=9;
array[1][0]=-2;
array[1][1]=4;
array[1][2]=6;
array[1][3]=-8;
int x;
// Using for loops to create the 2D array
for (int rows=0; rows<2; rows++)
{
for (int cols=0; cols<4;cols++)
{
System.out.print( array[rows][cols]+ " ");
}
System.out.println("");
}
System.out.println(newArray(array));
private static int[][] newArray(int[][] old)
{
int y;
int[][] current = new int [2][4];
for (int rows=0; rows<2; rows++)
{
for (int cols=0; cols<4;cols++)
{
if (old[rows][cols]<0)
{
y=Math.abs(old[rows][cols]);
old[rows][cols] = (int)Math.pow(old[rows][cols],2)/x;
}
old[rows][cols] = current[rows][cols];
}
}
return current;
}
}
}
There are lot of compilation issue. You should starting writing code step by step.
Here is the working model: (Advice you for reference)
class Examprep1 {
public static void main(String[] args) {
int[][] array = new int[2][4];
array[0][0] = 1;
array[0][1] = 5;
array[0][2] = -7;
array[0][3] = 9;
array[1][0] = -2;
array[1][1] = 4;
array[1][2] = 6;
array[1][3] = -8;
int x = 0;
// Using for loops to create the 2D array
System.out.println("Printing old array");
printArray(array);
System.out.println("Printing new array");
printArray(newArray(array, x));
}
private static void printArray(int[][] a) {
for (int rows = 0; rows < 2; rows++) {
for (int cols = 0; cols < 4; cols++) {
System.out.print(a[rows][cols] + " ");
}
System.out.println("");
}
}
private static int[][] newArray(int[][] old, int x) {
int y;
int[][] current = new int[2][4];
for (int rows = 0; rows < 2; rows++) {
for (int cols = 0; cols < 4; cols++) {
y = Math.abs(old[rows][cols]);
current[rows][cols] = y;
}
}
return current;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments