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

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-22-2013, 07:43 AM
manibby93
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.
• 09-22-2013, 07:47 AM
manibby93
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.
• 09-22-2013, 08:35 AM
JosAH
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
• 09-22-2013, 08:36 AM
manibby93
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.
• 09-22-2013, 08:49 AM
SurfMan
Re: What are the values of first and second at the end of the following code?
Quote:

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...
• 09-22-2013, 08:51 AM
manibby93
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...
• 09-22-2013, 09:03 AM
SurfMan
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:
Code:

```int first = 8; int second = 19; first = first + second; second = first - second; first = first - second;```
On paper:
Code:

```int first = 8; int second = 19; first = 8 + 19; second = {what is first?} - {what is second?}; first = {what is first?} - {what is second?};```
• 09-22-2013, 09:06 PM
manibby93
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.
• 09-22-2013, 09:41 PM
SurfMan
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:
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.```
• 09-23-2013, 04:17 AM
manibby93
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");
}
}
• 09-23-2013, 05:34 AM
manibby93
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!!
• 09-23-2013, 05:37 AM
manibby93
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!
• 09-23-2013, 05:38 AM
manibby93
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.
• 09-23-2013, 05:43 AM
DarrylBurke
Re: URGENT!! HELP!! What are the values of first and second at the end...
• 09-23-2013, 05:53 AM
manibby93
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. -__-
• 09-23-2013, 09:13 AM
JosAH
Re: URGENT!! HELP!! What are the values of first and second at the end...
Quote:

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
• 09-23-2013, 09:34 AM
gimbal2
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.
• 09-23-2013, 10:06 AM
SurfMan
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:
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:
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 :))
• 09-23-2013, 11:08 AM
JosAH
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
• 09-23-2013, 02:36 PM
manibby93
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?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last