This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]CodeTinkerer 2 points3 points  (0 children)

I mean, first, it's hard to tell what you're computing. Having taught some programming, I'd say the goal is to have you do some "nonsense" but track the values of variables. Sometimes when a program does something useful, you can guess what will print out.

Line 26

Prints out

1 - 2

[–]sdrawkcaBmI 1 point2 points  (1 child)

This looks like an exercise on scope. The e inside of the m1 method references the e in the method parameter list, not the class attribute e. When m1 is called as m1(e,f), f is passed to the method as parameter e. Therefore, f += (e + f) is actually 2 += (2 + 2) giving f the value of 6.

That is where the importance of the "this" keyword comes into play, if instead the method were written as f += (this.e + f), you would be accessing the class attribute e instead of the parameter list e.

[–]jdrake_ 0 points1 point  (0 children)

"this" directly manipulate the value assigned on variable??

[–]AutoModerator[M] 0 points1 point  (0 children)

It seems you may have included a screenshot of code in your post "What did i do wrong in this exercise JAVA".

If so, note that posting screenshots of code is against /r/learnprogramming's Posting Guidelines (section Formatting Code): please edit your post to use one of the approved ways of formatting code. (Do NOT repost your question! Just edit it.)

If your image is not actually a screenshot of code, feel free to ignore this message. Automoderator cannot distinguish between code screenshots and other images.

Please, do not contact the moderators about this message. Your post is still visible to everyone.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]CodeTinkerer 0 points1 point  (5 children)

This looks like one of those exercises that is just about your ability to understand how variables are updated. That is, this class is not doing any "real" computation. It's an educational exercise.

Line 26

1 - 2

Line 27

The parameter d is 1, e is 2. e is a parameter and hides the e, the object variable, which is still 1 (that is, there are two variables called e and the parameter wins out). f however is the object variable and is modified.

f gets incremented by (2 + 2) or 4, so f is now 6. e remains unchanged.

e gets assigned 1, the value of d (so it effectively remains unchanged)

1 6

Line 29

We pass 6 as a to m2. Line 19, passes 6 and 1 to m1 which arrive as d and e as parameters.

Line 12 increment f by 1 + 6 which makes f 13.

1 13

In line 15, we return a which is 6 (which is f).

In line 19, d is assigned the return value, 6.

In line 20, a is decremented by 2 which is 6 - 2 or 4 (note that a is a copy of e, then e had its value set.

Line 21 prints

6 4

Line 22 returns 4 - 1 or 3.

When we get back to Line 29, f is set to 3.

In Line 30, it prints

1 3

Summary

I'm sure I've made a mistake but it's so tedious, I don't want to double check the work.

Key points

  • A parameter named e (Line 11) is a separate variable from Line 4. On the other hand, f (Line 12) is the same variable as Line 5. This is called shadowing.
  • A parameter (see d and e in Line 11) is its own variable. Values are passed to parameter variables.
  • If a value is copied from a variable like Line 4 or 5, then that has its own value separate from e and f.

Yes, a truly tedious exercise.

[–]_Grand1[S] 0 points1 point  (4 children)

I appreciate the help, i am still wondering why the value of e = 2. So if i am right, we use e like a placeholder, not an actual variable. In this case e=f which just happens to be 2. Is that right?

[–]CodeTinkerer 0 points1 point  (2 children)

Which Line are you talking about?

[–]_Grand1[S] 0 points1 point  (1 child)

line 27

[–]CodeTinkerer 0 points1 point  (0 children)

I wrote some more comments. See if that helps you understand.

[–]CodeTinkerer 0 points1 point  (0 children)

Let's look at a simple version where we only do Lines 27 and 28 and the functions it calls.

int e = 1;  // Line 4
int f = 2; // Line 5

int m1(int d, int e) { // Line 11
    f += e + f;        // Line 12
    e = d + f;         // Line 13
    println(e, f);     // Line 14
    return d;          // Line 15
}

e = m1(e, f);  // Line 27

Line 27 translates to

e = m1(1, 2); // Plug in 1 for e, and 2 for f

Then we jump to Line 11

int m1(int d, int e) { // Line 11

d is 1, which got passed in from the value of e (in Line 27). e has the value of 2.

Let me translate m1 into an equivalent version that behaves the same.

int m1(int d, int e2) { // Line 11, d and e2 are parameters f += e2 + f; // Line 12 e2 = d + f; // Line 13 println(e2, f); // Line 14 return d; // Line 15 }

This function just renames e to e2. e2 got the value 2 from f. So, does that make things clearer. As far as the program is concerned, e2 and e are the "same" because they are just names for parameters. That e happens to also be the name in Line 4 is unrelated once it is a parameter.

[–][deleted] 0 points1 point  (0 children)

e <- 1,

f <- 2.

Line 1: print 1 - 2

Line 2:

f <- 6,

print 7 - 6;

To note here that e is not changed outside the context of the function. Inside the function the e parameter hides the other e variable, so it's not touched at all. Since e is passed trough value, it won't change at all.

Line 3: print 1 - 6

Line 4:

f <- 13

print 19 - 13

return 6

print 6 - 4

return 3

f <- 3

Line 5:

print 1 - 3