# Thread: What are the values of first and second at the end of the following code?

1. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## What are the values of first and second at the end of the following code?

I'm done bothering you guys for like a week, lol, but I need help with this...

What are the values of first and second at the end of the following code? How would you describe the net effect of the code statements in this exercise?

int first = 8;

int second = 19;

first = first + second;

second = first - second;

first = first - second;

I might be overthinking, but would the values be 8 & 19? I have no clue what the second question is about though.

2. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: What are the values of first and second at the end of the following code?

Then, it says to rewrite the code from this exercise to be shorter, by declaring the variables together and by using the special assignment operators (e.g., +=, -=, *=, and /=) as appropriate.

3. ## Re: What are the values of first and second at the end of the following code?

Take a piece of paper and a pencil and perform those three last lines of code by hand. Hint: it's an old trick to swap the value in those two variables without using a third variable.

kind regards,

Jos

4. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: What are the values of first and second at the end of the following code?

I honestly just don't understand what to do though.

5. ## Re: What are the values of first and second at the end of the following code?

Originally Posted by manibby93
I honestly just don't understand what to do though.
See Jos's post: "Take a piece of paper and a pencil and perform those three last lines of code by hand.". I know it's old-skool, but for the love of James Gosling, DO IT! You'll be surprised that a scribbled A4 paper can give you more insight than a 27" monitor...

6. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: What are the values of first and second at the end of the following code?

How can I do that if I don't understand what to do...

7. ## Re: What are the values of first and second at the end of the following code?

Ok, I''ll give you a nudge...

In code:
Java Code:
```int first = 8;
int second = 19;
first = first + second;
second = first - second;
first = first - second;```
On paper:
Java Code:
```int first = 8;
int second = 19;
first = 8 + 19;
second = {what is first?} - {what is second?};
first = {what is first?} - {what is second?};```

8. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: What are the values of first and second at the end of the following code?

The multiple "firsts" and "seconds" are throwing me off.

So, for line 3, it'd be 8+19??
Line 4 is 8-19?
Line 5 is 8-19 too?

I'm a COSC minor, but I only worked with Visual Basic before, so DrJava is WEIRD, lol.

9. ## Re: What are the values of first and second at the end of the following code?

Well, if you learn Java, then you will find out Visual Basic is actually the weird one. :)

first = first + second simply means: variable first gets assigned a new value. The new value will be first + second. So calculate first + second, then that will be the new value of first.

So for example:
Java Code:
```int a = 1;
int b = 2;
a = a + b; //assign the result of a + b to a. a+b = 3, so after this line, a wil contain the value 3.```

10. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: What are the values of first and second at the end of the following code?

So, how can I make this a Java program because the next assignment says to "rewrite the code from the previous exercise to be shorter, by declaring the variables together and by using the special assignment operators (e.g., +=, -=, *=, and /=) as appropriate??"

I know this isn't right because I'm lost, but I have:

public class Question12 {
public static void main(String[] args) {

System.out.println("int first = 8");
System.out.println("int second = 19");
System.out.println("first = first + second");
System.out.println("second = first - second");
System.out.println("first = first - second");
}
}

11. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: What are the values of first and second at the end of the following code?

Also, how would you describe the net effect of the code statements in this exercise?

I NEED TO FINISH THESE BY 8:15 TOMORROW MORNING! HELP!!

12. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## URGENT!! HELP!! What are the values of first and second at the end...

I NEED TO MAKE A CODE FOR THIS:

What are the values of first and second at the end of the following code? How would you describe the net effect of the code statements in this exercise?

int first = 8;

int second = 19;

first = first + second;

second = first - second;

first = first - second;

I NEED TO FINISH THIS BY 8:15 TOMORROW MORNING! HELP!!

I HAVE ZERO CLUE WHAT TO DO, HONESTLY. I DO NOT KNOW HOW TO MAKE A CODE FOR THIS, & I DO NOT KNOW WHAT NET EFFECT IS! THE CLASSMATES I ASKED FOR HELP SAID THEY DIDN'T EVEN DO THEM! I NEED A HUGE FAVOR!! PLEASE!

13. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: URGENT!! HELP!! What are the values of first and second at the end...

I've completed the other like 15 assignments, but this one I'm clueless on how to complete.

15. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: URGENT!! HELP!! What are the values of first and second at the end...

If it's old, no one's going to see it. -__-

16. ## Re: URGENT!! HELP!! What are the values of first and second at the end...

Originally Posted by manibby93
If it's old, no one's going to see it. -__-
I merged the two threads; don't open multiple threads for a single problem. Have you tried to play 'computer' and do those (few) steps by hand? Note: an assignment is not an equation.

kind regards,

Jos

17. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: What are the values of first and second at the end of the following code?

Its weird. When I was in school learning this whenever I didn't know something I went to study my books (you know, student - study; there is a link there). Apparently nowadays, people go to forums and ask others to read it out to them.

18. ## Re: What are the values of first and second at the end of the following code?

I am pretty sure you're not supposed to System.out.println the desired result... int first = 8 is a Java statement, which should be in your code, not in a println call... Here's another nudge in the right direction:
Java Code:
```public class Question12 {
public static void main(String[] args) {
int first = 8;
//the rest is omitted as an exercise to the OP :)
}
}```
This is not really rocket science you know. If you have done Visual Basic before, however shit VB is, you *must* have run into variables and assignments one day in VB too. It's not even all that different:
Java Code:
```   public sub blah()
dim first as Integer
first = 8
end sub```
Cheers,

SurfMan (*ducks* for the flames he gets for using a VB example :))

19. ## Re: What are the values of first and second at the end of the following code?

I find this all a bit strange: I don't understand at all why the OP doesn't understand this at all (which makes it extremely difficult to communicate about the little problem) ...

kind regards,

Jos

20. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: What are the values of first and second at the end of the following code?

Thanks for the smart-aleck comment . Please, do team up on me. Anyways, I can't study without the book that she reads slide-for-slide from, and costs too much more money than I am willing to spend. Also, it's all foreign to me, as I last took COSC classes two years ago working with Microsoft Access and VB with an easy teacher who could actually teach. It's too late to add/drop btw. it's like the 5th week of school, so I'm trying to go along. I understand that I caused you discomfort or something, but please be direct. I thought that maybe this would be better than Google, somehow?

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•