In: Computer Science
public class OOPExercises { public static void main(String[] args) { A objA = new A(); B objB = new B(); System.out.println("in main(): "); System.out.println("objA.a = "+objA.getA()); System.out.println("objB.b = "+objB.getB()); objA.setA (222); objB.setB (333.33); System.out.println("objA.a = "+objA.getA()); System.out.println("objB.b = "+objB.getB()); } } |
Output: |
public class A { int a = 100; public A() { System.out.println("in the constructor of class A: "); System.out.println("a = "+a); a = 333; System.out.println("a = "+a); } public void setA( int value) { a = value; } public int getA() { return a; } } //class A |
|
public class B { double b = 123.45; public B() { System.out.println("-----in the constructor of class B: "); System.out.println("b = "+b); b = 3.14159; System.out.println("b = "+b); } public void setB( double value) { b = value; } public double getB() { return b; } } //class B |
get the output of the following application
ANSWER
YOUR CODE PRODUCES ERROR AS A OUTPUT BECAUSE CODE CAN HAVE ONLY ONE PUBLIC CLASS. AND IT IS HAVING MORE THAN ONE PUBLIC CLASS OUTSIDE A SINGLE CLASS. SO RESULTS INTO ERROR.
OUTPUT
####################################################
HOWEVER AFTER REMOVING public KEYWORD FROM CLASS A AND B, YOU WILL GET OUTPUT.
MODIFIED CODE ALONG WITH OUTPUT IS SHOWN BELOW.
CODE
public class OOPExercises {
public static void main(String[] args) {
A objA = new A();
B objB = new B();
System.out.println("in main(): ");
System.out.println("objA.a = "+objA.getA());
System.out.println("objB.b = "+objB.getB());
objA.setA (222);
objB.setB (333.33);
System.out.println("objA.a = "+objA.getA());
System.out.println("objB.b = "+objB.getB());
}
}
class A {
int a = 100;
public A() {
System.out.println("in the constructor of class A: ");
System.out.println("a = "+a);
a = 333;
System.out.println("a = "+a);
}
public void setA( int value) {
a = value;
}
public int getA() {
return a;
}
} //class A
class B {
double b = 123.45;
public B() {
System.out.println("-----in the constructor of class B: ");
System.out.println("b = "+b);
b = 3.14159;
System.out.println("b = "+b);
}
public void setB( double value) {
b = value;
}
public double getB() {
return b;
}
} //class B
OUTPUT
##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####