I'm trying to find out what is the output of the following code and I almost got all of them right except 2 lines. Line 3 and 7.
The expected output would be:
1 : 10
2 : 20
3 : 20
4 : 15
5 : 40
6 : 75
7 : 35
8 : 75
9 : 20
As I mentioned I got everything right except 3 and 7. Line 3 for me was 25 and Line 7 was 40. Could you guys please explain why is line 3: 20 and line 7: 35? I tried to look it up before but couldn't find a good explanation.
It's not a homework!
public class Alpha {
protected int x;
public Alpha() { this(10); }
public Alpha(int x) { this.x = x; }
public void f() { x = 20; }
public void f(int x) { x = 25; }
public void g(Object a) { x = 30; }
public void h(Object a) { x = 50; }
public void h(Integer a) { x = 55; }
}
public class Beta extends Alpha {
public Beta() { super(15); }
public void g(Object a) { x = 35; }
public void g(Integer a) { x = 40; }
public void h(Object b) { x = 70; }
public void h(Integer b) { x = 75; }
public static void main(String[] args) {
Alpha a = new Alpha();
System.out.println("1 : " + a.x);
a.f();
System.out.println("2 : " + a.x);
a.f(100);
System.out.println("3 : " + a.x);
Beta b = new Beta();
System.out.println("4 : " + b.x);
b.g(200);
System.out.println("5 : " + b.x);
b.h(300);
System.out.println("6 : " + b.x);
Alpha c = b;
c.g(400);
System.out.println("7 : " + c.x);
c.h(500);
System.out.println("8 : " + c.x);
c.f();
System.out.println("9 : " + b.x);
}
}
For problem #3, the variable a is an Alpha. Before a.f(100) is called, a.f() is called, which sets x to 20. While calling a.f(100), the local variable x gets set to 25 in the method f(int x), not the instance variable x. The instance variable remains 20.
For problem #7, the variable b, which is a Beta, gets assigned to c, an Alpha reference. When c.g(400) is called, the compiler must make a call to g(Object), because that is the only overload of g that is available in Alpha. At runtime, polymorphism dictates that because the object is a Beta, the override of g(Object) in Beta is chosen, which sets x to 35. This is because the compiler chooses the method overload while the method override is chosen at runtime with polymorphism.
The reason line 3 is 20 is because you are changing the value of 'x' passed into the method and not the class level variable 'x'. Change your code to the below. If you read up on variable scope you'll get a better understanding too.
public class Alpha {
protected int x;
public Alpha() { this(10); }
public Alpha(int x) { this.x = x; }
public void f() { x = 20; }
public void f(int x) { this.x = 25; }
public void g(Object a) { x = 30; }
public void h(Object a) { x = 50; }
public void h(Integer a) { x = 55; }
}
Related
There are two programs here . I am not able to understand the explanation of the problems as they have very contrasting explanations.
//Question1
What will be the output of the program?
class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}
void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}
ans: 7 14
explanation: The int x in the twice() method is not the same int x as in the start() method. Start()'s x is not affected by the twice() method. The instance variable s is updated by twice()'s x, which is 14.
//ANOTHER QUESTION
```
class Two
{
byte x;
}
class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
Two fix(Two tt)
{
tt.x = 42;
return tt;
}
}
```
ans= 0 42 42
explanation:
In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0.
i recently learned the use of public, private and double in my different classes. But for some reason i cant understand why this is not working. My intention was to use three different classes as an exercise: I want Do() to make numbers from 0 to 20 and show only the numbers 0 till 10 on my console using the method for1() in a different class. Can someone please fix this issue? I dont need a shorter code or a code in just 1 class since i need it to educate myself using many classes. I would thank anyone if you could fix this issue using this kind of setup. Thanks in advance.
public class MainM {
public static void main(String[] args) {
loop Q = new loop();
Q.Do();
}
}
//------------------------------------------------------
public class loop {
public double b;
Sum R = new Sum(); // Java shows the problem is here : at Sum.<init>(Sum.java:3)
public void Do() {
for (int i = 0; i < 10; i++) {
b = b + 2;
if (b <= 10) {
R.for1();
}
}
}
}
//--------------------------------------------------
public class Sum {
loop Q = new loop();
public void for1() {
System.out.println("b " + Q.b);
}
}
You can have your Sum class only with the print statement and the method for1() should have one parameter. Bellow is my suggestion
public class Sum {
public void for1(double b) {
System.out.println("b " + b);
}
}
And your loop class will be
public class loop {
public double b;
Sum R = new Sum();
public void Do() {
for (int i = 0; i < 10; i++) {
b = b + 2;
if (b <= 10) {
R.for1(b);
}
}
}
}
i know how it works here
Polymorphism does not include instance fields. Methods yes, fields no. Baap b is a reference of type Baap when accessing fields (b.h). When accessing methods, b morphs into a Beta type (b.getH()).
class Baap
{
public int h = 4;
public int getH()
{
System.out.println("Baap "+h);
return h;
}
}
public class Beta extends Baap
{
public int h = 44;
public int getH()
{
System.out.println("Beta "+h);
return h;
}
public static void main(String[] args)
{
Baap betaInBaap = new Beta();
System.out.println(betaInBaap.h+" "+betaInBaap.getH());
Beta castedBeta = (Beta) betaInBaap;
System.out.println(castedBeta.h+" "+castedBeta.getH());
}
}
What I don't understand is the order of output
As Here o/p must be 4(calling b.h) followed by Beta 44(calling method b.geth()) and the method returns 44 as well so the first line must be 4 Beta 44 44
public int h = 44;
public int getH()
{
System.out.println("Beta "+h); // print line 1
return h;
}
public static void main(String[] args)
{
Baap b = new Beta();
System.out.println(b.h+" "+b.getH()); // print line 2
Beta bb = (Beta) b;
System.out.println(bb.h+" "+bb.getH()); // print line 3
}
In your first print statement in your main method, you want it to print b.h+ " "+ b.getH(). It can't print this, before it knows the value returned by b.getH();
So, it runs b.getH() which prints (seperately) System.out.println("Beta "+h);
before returning a value to the main method. (print line 1)
That explains your first line.
After the value is returned, the main method can now print print line 2, because it now knows what b.getH() has as result:
System.out.println(b.h+" "+b.getH());
This explains your second line in the output.
The same order is executed when running print line 3.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I am trying to make a command line calculator using Java, I have come up with the code below it works without the if-else ladder,but I can't make it work with if-else, during debugging I tried printing the "z" which is indeed add when the arguments are passed as java name 1 2 add, But I can't seem to trigger the id (z == "add"), please suggest what I am missing.
public class commandlinecal {
public static void main(String arg[])
{
int x = Integer.parseInt(arg[0]);
int y = Integer.parseInt(arg[1]);
String z = arg[2];
if (z == "add")
{
add(x,y);
}
else if (z == "sub")
{
sub(x,y);
}
else if (z=="mul")
{
mul(x,y);
}
else if(z=="div")
{
div(x,y);
}
}
public static void add(int x,int y)
{
int result = x + y;
System.out.println("The sum is" + " "+result);
}
public static void sub (int x,int y)
{
int result = x-y;
System.out.println("The sub is"+" "+result);
}
public static void mul (int x,int y)
{
int result = x * y;
System.out.println("The multiplication is"+" "+result );
}
public static void div (int x,int y)
{
float result = x / y;
System.out.println("The division is"+" "+result);
}
}
Use if (z.equals("add")) { ... } instead.
When running the function foo1, why does the output for this code will be: 15 30 5
and not 15 15 5 ?
I underdtand that the pointer of the object v is now points to the object va1, so the output for the code line: System.out.print(v.getI() + " ");
should be 15. So why is it 30 ?
public class Value
{
private int _i;
public Value()
{
_i=15;
}
public int getI()
{
return _i;
}
public void setI (int i)
{
_i=i;
}
}
public class TestValue
{
public static void foo1()
{
int i=5;
Value v= new Value();
v.setI(10);
foo2(v,i);
System.out.print(v.getI() + " ");
System.out.print(i+ " ");
}
public static void foo2( Value v, int i)
{
v.setI(30);
i=10;
Value va1= new Value();
v=va1;
System.out.print (v.getI() + " ");
}
}
Java only supports pass-by-value. So when you pass an object "v" to the method foo2 a copy of the reference "v" is created. So when you set v = val1 in foo2 the copy of the reference is being changed in foo2 not the original reference "v" in foo1.