user2254768
user2254768

Reputation: 55

Return value in a class from another class and method

Note that I am new with Java. I have a class and and a method in this class hat computes some values and I want to return them in the first class. I have a code but it returns 0.

That's in class that computes the values

public class ImageProcessing{
int i,j;
int R[][]=new int[640][320];
int G[][]=new int[640][320];
int B[][]=new int[640][320];


public double meanR[]=new double[320];
public double meanG[]=new double[320];
public double meanB[]=new double[320];
public double varianceR[]=new double[320];
public double varianceG[]=new double[320];
public double varianceB[]=new double[320];
public double skewnessR[]=new double[320];
public double skewnessG[]=new double[320];
public double skewnessB[]=new double[320];

public double round(double value){
    //  int decimalPlace = 2;

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(2,BigDecimal.ROUND_UP);
    return (bd.doubleValue());
}

public void Mean(Bitmap image){

    int width = image.getWidth();
    int height = image.getHeight();
    int pixel = 0;


    for (i=0; i<width;i++){
        for (j=0; j<height; j++){
            pixel = image.getPixel(i,j);
            R[i][j] = Color.red(pixel);
            G[i][j]= Color.green(pixel);
            B[i][j] = Color.blue(pixel);


            meanR[j]=meanR[j]+R[i][j];
            meanG[j]=meanG[j]+G[i][j];
            meanB[j]=meanB[j]+B[i][j];
        }
    }

In the main class I have:

    method.Mean(rescaledBitmap1);

    meanR1=method.meanR;
    meanG1=method.meanG;
    meanB1=method.meanB;
    System.out.println(meanR1);

Upvotes: 1

Views: 29426

Answers (2)

0x6C38
0x6C38

Reputation: 7076

You must specify what you want to return in the definition of the method and actually return a value using the keyword return:

public int sum(int a, int b){
 int result = a + b;
 return result;
}

You've declared Mean() as void public void Mean(Bitmap image) and thus it returns no value.

Also, you can only return 1 variable so you should either put those 3 values in some sort of array or create a new class and encapsulate the variables inside an object. Here is an example:

public class MeanResult(){
private double meanR[]=new double[320];
private double meanG[]=new double[320];
private double meanB[]=new double[320];
//Maybe declare more stuff here

 public MeanResult(Bitmap image){
  //... code n stuff here to calculate width, height and pixel
 }
 public double getMeanR(){ return this.meanR[]; }
 public double getMeanG(){ return this.meanG[]; }
 public double getMeanB(){ return this.meanB[]; }
}

And you can use it like this:

MeanResult mean = new MeanResult(image);
meanR1=mean.getMeanR();
meanG1=mean.getMeanG();
meanB1=mean.getMeanB();

Upvotes: 1

Michael Auderer
Michael Auderer

Reputation: 159

The method mean(Bitmap image) is void. For it to return something, you must change void to what it will return, and at the end of the method return the variable.

Example:

public int add(int x, int y)
{
    return x + y;
}

Then if you called add(1,2) it would have a value of 3.

Upvotes: 0

Related Questions