Day 2- Question no 16 | Selenium Forum
V
Vimala Matlapudi Posted on 07/12/2021

public class loop10 {

public static void main(String[] args) {
A a=new A();
a.b=10;
a.increment(a.b);
System.out.print(a.b);
B b = new B();
b.increment(a);
System.out.println(a.b);
}

}

class A{
int b;
public void increment(int x) {
x=x+100;
System.out.println(x);
}
}

class B{
public void increment(A a) {
a.b=a.b+200;
System.out.println(a.b);
}
}

 

 

Error :

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
b cannot be resolved or is not a field
b cannot be resolved or is not a field
b cannot be resolved or is not a field
b cannot be resolved or is not a field

at loop10.main(loop10.java:5)

 

but the answer is : 11010210210


N
Nanda T S Replied on 08/12/2021

package loops;

 

public class Loop16

{

 

publicstaticvoidmain(String[] args)

{

A a=new A();

a.b=10;

a.inc(a.b);

System.out.print(a.b);;

B b=new B();

b.inc(a);

System.out.print(a.b);;

 

}

 

}

 

class A{

intb;

publicvoidinc(int x)

{

x=x+100;

System.out.print(x);

}

}

 

class B{

publicvoidinc(A a)

{

a.b=a.b+200;

System.out.print(a.b);

}

 

}

the answer is 11010210210