user5827762 user5827762 - 4 months ago 20
Java Question

Working with methods in Java

I've created 2 methods in Java declared public int where each of them returns a value. I want to create another public int method which returns a value that is sum of these the values of 2 first methods. When I execute the value isn't correct. Can someone help me with creating this method which returns the sum of the other method's values. Here are my methods just for example.



public int a() {
int nr=7;
int c=4;
sum=nr+c;
return sum;
}

public int b() {

int l=7;
int m=4;
sum=m+l;
return sum;

}

public int c() {
int sum = 0;
sum = a() + b();
return sum;
}




Answer

you program has many error which are corrected as follows: public class Af {

public Af() {
}

int sum1 = 0;

public int a() {
    int nr = 7;
    int c = 4;
    sum1 = nr + c;
    return sum1;
}

public int b() {

    int l = 7;
    int m = 4;
    int sum2 = m + l;
    return sum2;

}

public int c() {
    int sum3 = 0;
    sum3 = a() + b();
    return sum3;
}

}