-9

I want to declare an integer variable in MainActivity.java and then pass it to a class,in order to use that integer variable as one of the attributes of that class and I don't know how to do it.

Is it possible?Anyone has any idea?

Here is the class.I wanna set M and N from MainActivity.Java

public class Determinant {

//Atributes
private int M = 2;
private int N = M;
private double FinalAnswers[];



//Constructors
public Determinant() {
}

//methods
public void solve(double[][] A, double[] B) {
    int N = B.length;
    for (int k = 0; k < N; k++) {
        /** find pivot row **/
        int max = k;
        for (int i = k + 1; i < N; i++)
            if (Math.abs(A[i][k]) > Math.abs(A[max][k]))
                max = i;

        /** swap row in A matrix **/
        double[] temp = A[k];
        A[k] = A[max];
        A[max] = temp;

        /** swap corresponding values in constants matrix **/
        double t = B[k];
        B[k] = B[max];
        B[max] = t;

        /** pivot within A and B **/
        for (int i = k + 1; i < N; i++) {
            double factor = A[i][k] / A[k][k];
            B[i] -= factor * B[k];
            for (int j = k; j < N; j++)
                A[i][j] -= factor * A[k][j];
        }
    }
    /** back substitution **/
    double[] solution = new double[N];
    for (int i = N - 1; i >= 0; i--) {
        double sum = 0.0;
        for (int j = i + 1; j < N; j++)
            sum += A[i][j] * solution[j];
        solution[i] = (B[i] - sum) / A[i][i];
    }
    /** Print solution **/
    FinalAnswers = solution;
}


//setters


//getters
public double getAnswers(int i) {
    return FinalAnswers[i];
}

public int whatIsM() {
    return M;
}

public int whatIsN() {
    return N;
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Create a new class called common.class

import android.app.Application;

/**
 * Created by webcastman on 9/8/16.
 */

public class common_class extends Application
{
private static String gstring;
private static Integer gnumber;

public static  String webURL  = "http://localhost:81/";

public static String getgstring()
{
    return gstring;
}
public static void   putgstring(String str)
{
    gstring = str;
}

public static Integer getgnumber()
{
    return gnumber;
}
public static void putgnumber(Integer nor)
{
    gnumber = nor;
}
}

then in the main class, write the code.

common_class.putgnumber(..your number..);

then from any other java class

value = common_class_getgnumber()
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
user6804473
  • 165
  • 2
  • 2
  • 6
0

I found it.

We can add a setter method to that class for assigning the value to an attribute.