I am having trouble grasping four different return statement problems in java which deal with integers. They usually go by the names of "mystery" and "enigma". I have tried solving them numerous times, and the web and my text book do not contain any similar examples for me to work with. My gut feeling is I am missing something with the logic. If someone can maybe just explain on of the problems (preferably the hardest one), I am sure I will understand it. I apoligise in advance if the format is not on par with this site's requirements, since it is my first time posting.
1 public class Program 1{
2
3 public static int y = 2;
4
5 public static int mystery(int x, int y) {
6 y = y + x;
7 return x + y;
8 }
9
10 public static void main(String[] args) {
11 int x = 1;
12 x = mystery(x, y);
13 y = mystery(y, x);
14 System.out.println(x + " " + y);
15 }
16}
17 // Answer : 4 8
I get x to be 4, but I struggle to get y to be 8.
1 public class Program 2{
2
3 public static int y = 2;
4
5 public static int mystery(int a, int b) {
6 y = b + a;
7 return a + b;
8 }
9
10 public static void main(String[] args) {
11 int x = 1;
12 x = mystery(x, y);
13 y = mystery(y, x);
14 System.out.println(x + " " + y);
15 }
16}
17 // Answer : 3 6
I get x to be 3, but I struggle to get y to be 6.
1 public class Program 3{
2
3 public static int x = 1;
4 public static int y = 2;
5
6 public static int mystery1(int a, int b) {
7 x = a + b;
8 return b + a;
9 }
10
11 public static int mystery2(int a, int b) {
12 y = b + a;
13 x = mystery1(a, b);
14 return a + b;
15 }
16
17 public static void main(String[] args) {
18 x = mystery2(x, y);
19 System.out.println(x + " " + y);
20 }
21
22}
23 // Answer : 3 3
I get x to be 3, but I struggle to get y to be 3.
public class Enigma {
public static int x = 1;
public static int y = 2;
public static int n = 0;
public static int aaa(int a, int b) {
n++;
return a + b;
}
public static int bbb(int a, int b) {
n++; x = aaa(x, a); y = aaa(y, b);
return x + y;
}
public static void ccc(int x, int q) {
n++; x = bbb(1, x); y = bbb(2, q);
}
public static void main(String[] args) {
int x = aaa(3, y);
y = bbb(x, y); ccc(x, 1);
System.out.println(x + " " + y + " " + n);
// Answer : 5 25 11
I get x to be 5, but I struggle to get y = 25 and n = 11, although incrementing n at each method is probably the reason why.
Try to make things less confusing by not keep using the same variables. For example, in main program, create new variables (lets say a and b) and instead write "int a=mystery(x,y);" and "int b=mystery(y,x);" for program 1.
1) Lets first solve the problem you have in Program 1. First the program runs code line 12, which is x=mystery(x,y), and x is 1, y=2.
2) So x=mystery(1,2). When you run that, you assign y=y+x, y is still 2, and x is still 1, 2+1=3, therefore now y is 3 and x is still 1.
3) Then you tell the program to return y+x, or 3+1, and you assign it to x. Now x=4 and y is 3.
4)Your program runs line 13, with y=3 and x=4, therefore y=(3,4). So y=y+x is y=3+4, or 7.
5) THIS IS WHEN YOU MESSED UP! you expected x to still be 1, but it isn't anymore, it is 4, and therefore you get 11.
Please leave a comment if you don't understand. If possible, please tell me where you get stuck by using the numbering I provided :) Good luck
Program1:
x = mystery(x, y) -> returning 4 , so x=4, your class y stays 2(if you want to access class y, use 'this' keyword.
y = mystery(y, x) -> mystery(2,4) returning 2+6=8
Program2:
x = mystery(x, y) -> class y is now 3, returning 3, x=3
y = mystery(y, x) -> mystery(3,3) returning 6
Program3:
x = mystery2(x, y) -> class y is now 3, mystery1(1, 2) -> returning 3, so class x=3
Enigma:
int x = aaa(3, y) -> returning 5, n=1
y = bbb(x, y) -> n=4, x=6, y=4, returns 10. So class y=10
In method ccc, n becomes 11 (1+3+3).
Note that in method ccc you're shadowing the class member x. instead it is the local one from the parameter.
Related
Why is the output 25?
// CODE 1
public class YourClassNameHere {
public static void main(String[] args) {
int x = 8;
System.out.print(x + x++ + x);
}
}
Hi!
I am aware that the above code will print 25. However, I would like to clarify on how x++ will make the statement be 8 + 9 + 8 = 25.
If we were to print x++ only as such, 8 will be printed while x will be 9 in-memory due to post incrementation.
// CODE 2
public class YourClassNameHere {
public static void main(String[] args) {
int x = 8;
System.out.print(x++);
}
}
But why is it that in code 1 it becomes 9 ultimately?
I thank you in advance for your time and explanation!
Here is a good way to test the reason that equals to 25 is because the third x is equal to 9.
public class Main {
public static void main(String[] args) {
int x = 8;
System.out.println(printPassThrough(x, "first") + printPassThrough(x++, "second") + printPassThrough(x, "third"));
}
private static int printPassThrough(int x, String name) {
System.out.println(x + " => " + name);
return x;
}
}
Result
8 => first
8 => second
9 => third
25
I think it's worth of clarify:
x++ -> operate x , and then increment x (meaning x=x+1)
++x -> increment x (meaning x=x+1) , and then operate x
x-- -> operate x , and then decrement x (meaning x=x-1)
--x -> decrement x (meaning x=x-1) , and then operate x
I need an explanation for the following loop:
public static void main(String[] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 6;
y = y - 2;
if (x == 6) {
break;
}
x = x + 3;
}
y = y - 2;
}
System.out.println(x + " " + y);
}
The way I'm seeing it is that the 'outer' loop is running 3 times and the 'inner' loop is running 9 times. When I go through the 'inner' loop, x becomes 6, and we break out of the 'inner' loop after we get the value for y, which is 28. So now, the value of x is 6, and now I go through the 'outer' loop which runs 3 times, so 3 times 2 is equal to 6, so I subtract that from 28, and I end up with 22.
Output:
6 22
Does anyone know what I am doing wrong? I know the output should be 60 10, but I am not getting this. Thanks for your help.
I have copied and pasted your code in my pc and I get the out put
60 10
I think your are running a different file in your IDE.
What IDE do you use?
Why is the output of the following code: 13 15 17
I think it should be: 15 17 19
Here's the code:
package com.example.barker;
class dog {
}
public class Bark {
public static void main(String[] args) {
Bark o = new Bark();
o.go();
}
void go(){
int y =7;
for(int x = 1; x<8; x++) {
y++;
if(x>4) {
System.out.print(++y + " ");
}
}
}
}
I think the Anwer is right.
I will Explain the working of the code to get understand.
First
y=7
x=0
and after first iteration
y=8 (y++;) and x=1 (int x = 1;) (not printing because x not greater then 4)
after Second Iteration
y=9 (y++;) and x=2 (x++;) (not printing because x not greater then 4)
after Third Iteration
y=10 (y++;) and x=3 (x++;) (not printing because x not greater then 4)
after Fourth Iteration
y=11 (y++;) and x=4 (x++;) (not printing because x not greater then 4)
after Fifth Iteration
y=12 (y++;) and x=5 (x++;)
Now x is greater than 4 and going to System.out.print(++y + " ");
Here you are writing ++y ,means pre-increment
ie, increment y and printing
ie, y=13 and x=6 printing(13)
After next Iteration
y=14(y++;) and before printing the value of y doing ++y
ie,
y=15 (++y;) printing(15)
After next Iteration
y=16(y++;) and before printing the value of y doing ++y
ie,
y=17 (++y;) printing(17)
So the Output is 13 15 17
Thanks and happy coding.
Change the conditions like below, to get your desired result.
for(int x = 1; x<10; x++) {
y++;
if(x>6) {
System.out.print(++y + " ");
}
}
Let's first understand the operations happening within the for loop:
y++ will perform the +1 operation after the line of code finishes executing
++y will first perform the +1 operation and then execute the rest of the line of code
Now, let's look at the for loop:
for(int x = 1; x<8; x++) {
y++;
if(x>4) {
System.out.print(++y + " ");
}
}
For each iteration, we will look at the values for x and y:
x = 1, y = 7 (start of first iteration)
x = 1, y = 8 (end of iteration)
x = 2, y = 8 (start of first iteration)
x = 2, y = 9 (end of iteration)
x = 3, y = 9 (start of first iteration)
x = 3, y = 10 (end of iteration)
x = 4, y = 10 (start of first iteration)
x = 4, y = 11 (end of iteration)
x = 5, y = 11 (start of first iteration)
x = 5, y = 12 (y++ line)
x = 5, y = 13 (x > 4, and because of that, ++y executes)
The logic has become pretty apparent, but you can continue to debug this yourself.
Hope this helps.
Here, goes the explanation :
y=7 //At Start
When you enter in the loop:
x = 1 and y = 8
x = 2 and y = 9
x= 3 and y = 10
x = 4 and y = 11
x = 5 // which makes the if condition true
y will become 12 and when it goes into if System.out.print(++y + " ")
It will first increment then prints.
So, it will print y = 13.
Similarly, when x = 6 y will be 14 and in if it becomes 15 and so on.
public class Main {
public static void main (String[] args) {
int x = 0;
int y = 0;
while (x < 5) {
y = x - y;
System.out.print(x + y);
x = x + 1;
}
}
}
When I calculate this math myself. I get these answers:
y = 0 - 0 = 0
y = 1 - 0 = 1
y = 2 - 1 = 1
y = 3 - 1 = 2
y = 4 - 2 = 2
01122
But when I compile it. I get the answer
02356
I just don't get it. Could someone explain?
You are printing x+y not y
0+0=0 ; 1+1=2; 1+2=3; 2+3=5; 2+4=6
02356
In first iteration x=0,y=0 so x-y = 0 = y and x+y=0 so 0 will be printed.
In 2nd iteration x=1,y=0 so x-y = 1 = y and x+y=2 so 2 will be printed.
thus x and y will be updated.
In your calculation, you are not updating y
Use the debug, you will see that the iterations are correct because you are manipulating the x value after the x+y operation addition
I have been struggling with an exercise in the Java Headfirst book( CH5: p121 for reference). It's a loop inside another loop which adds/substracts some values from instance variables.
Input:
x = x + 3
Outputs:
x= 54 y = 6
public class MixFor5 {
public static void main(String[] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 3;
y = y - 2;
if (x == 6) {
break;
}
x = x + 3;
}
y = y - 2;
}
System.out.println(x + " " + y);
}
}
My result is when doing it by myself with a notepad is x=42 y = 8 because then both loop conditions are met. What am i doing wrong? where did I go wrong in my thoughtprocess?
these are my notes -> pastebin note
I have not tried debugging first because I want to figure this by myself first so that I don't make the same mistakes in the future.
Thanks in advance,
tvanderv
if(x == 6) will never get true. The reason behind this is,
When inner = 4
x = x + 3 executes two times i.e. means x = 6.
then, inner = 3
now first x = x + 3 (before if(x == 3) condition) will give output x = 9. So x > 6 it will not break loop.
You did this step wrong in your notes.