Reputation: 27
This is the method thats i built:
public static int opposite(int num){
if(num >= 0 && num <= 255)
num = Math.abs(num - 255);
return num;}
and now, I'm trying to use it on the next method which is supposed
to make a negative picture of a 2 dimensional array.
thats means that if Matrix[0][0] = int 0
it will switch it to 255
or 1 > 254, 2 > 253 and so on...
public Matrix makeNegative(){
int num;
Matrix[][] negative = new Matrix[Matrix.length][Matrix[0].length];
for(int i = 0; i < negative.length; i++){
for(int j = 0; j < negative[0].length;j++){
negative[i][j] = negative.opposite(num);}}
return negative;}
So, the first question is why i can't use my method? java bluejay suggest that:
canot find symbol - method opposite(int)
Second question is that ,I for some reason thought maybe if my code doesn't work this way ,maybe i shall try to make a method.
so I'm also trying to make a method which will bring me the int
inside a specific box inside the Matrix.
so i would like if someone can show me how this can be done.
Thanks ahead for anybody who can help, much apologize for my poor english and programing skills.
Edit was asked to put the whole class code here:
public class Matrix{
private int[][] Matrix;
/**
* builder that copys an array of 2 dimensions
* @param gets and int[][] array
*/
public Matrix(int[][] array){
Matrix = new int [array.length][array[0].length];
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[0].length; j++){
Matrix[i][j] = array [i][j];
}
}
}
/**
* builder that builds an array of 2 dimensions with the sizes given
* @param size1 = rows size2 = column
*/
public Matrix( int size1, int size2){
Matrix = new int [size1][size2];
}
public String toString(){
String result ="";
for(int i = 0; i < Matrix.length; i++){
for(int j = 0; j < Matrix[0].length;j++){
result += Matrix[i][j];
if(j != Matrix[0].length - 1){
result += "\t";
}
}
result += "\n";
}
return result;
}
public static int opposite(int num){
if(num >= 0 && num <= 255)
num = Math.abs(num - 255);
return num;}
/*
public int getColor(Matrix array){
int color;
color = }
*/
public Matrix makeNegative(){
int num;
Matrix[][] negative = new Matrix[Matrix.length][Matrix[0].length];
for(int i = 0; i < negative.length; i++){
for(int j = 0; j < negative[0].length;j++){
negative[i][j] = negative.opposite(num);}}
return negative;}
Upvotes: 1
Views: 83
Reputation: 6721
You have declared negative
as a two-dimensional array of type Matrix in your makeNegative()
method:
Matrix[][] negative = new Matrix[Matrix.length][Matrix[0].length];
and then you are trying to invoke negative.opposite(num);
which is not valid.
opposite
is a static method on the Matrix class not on the two-dimensional array of type Matrix. Simply remove negative.
in your call and it'll work!
Just keep:
negative[i][j] = opposite(num);
Update
This is going to cause lot of confusion:
public class Matrix{
private int[][] Matrix;
You have declared a matrix of integers. Plus you are defining Matrix[][] inside your makeNagative()
method. Your Matrix
class contains a matrix of integers already. You might not need a matrix of Matrix!
I have altered your makeNegative()
method in the best way possible. Take a look:
public Matrix makeNegative() {
int num;
int[][] negative = new int[Matrix.length][Matrix[0].length];
for (int i = 0; i < Matrix.length; i++) {
for (int j = 0; j < Matrix[i].length; j++) {
num = Matrix[i][j];
negative[i][j] = opposite(num);
}
}
return new Matrix(negative);
}
Update 2
I'm posting the entire code with an added main
method. Looks like this one works:
public class Matrix {
private int[][] Matrix;
/**
* builder that copys an array of 2 dimensions
*
* @param gets
* and int[][] array
*/
public Matrix(int[][] array) {
Matrix = new int[array.length][array[0].length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
Matrix[i][j] = array[i][j];
}
}
}
/**
* builder that builds an array of 2 dimensions with the sizes given
*
* @param size1
* = rows size2 = column
*/
public Matrix(int size1, int size2) {
Matrix = new int[size1][size2];
}
public String toString() {
String result = "";
for (int i = 0; i < Matrix.length; i++) {
for (int j = 0; j < Matrix[0].length; j++) {
result += Matrix[i][j];
if (j != Matrix[0].length - 1) {
result += "\t";
}
}
result += "\n";
}
return result;
}
public static int opposite(int num) {
if (num >= 0 && num <= 255)
num = Math.abs(num - 255);
return num;
}
/*
* public int getColor(Matrix array){ int color; color = }
*/
public Matrix makeNegative() {
int num;
int[][] negative = new int[Matrix.length][Matrix[0].length];
for (int i = 0; i < Matrix.length; i++) {
for (int j = 0; j < Matrix[i].length; j++) {
num = Matrix[i][j];
negative[i][j] = opposite(num);
}
}
return new Matrix(negative);
}
public static void main(String[] args) {
Matrix m = new Matrix(new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
});
Matrix negM = m.makeNegative();
System.out.println("Original Matrix");
System.out.println(m);
System.out.println("Negative Matrix");
System.out.println(negM);
}
}
The output looks like this:
Original Matrix
1 2 3
4 5 6
7 8 9
Negative Matrix
254 253 252
251 250 249
248 247 246
Hope this helps!
Upvotes: 3