Page 1 of 2 12 LastLast
Results 1 to 20 of 29
Like Tree2Likes

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

  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #2
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default 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
    SurfMan likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

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

    Quote Originally Posted by manibby93 View Post
    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. #6
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default 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. #8
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default 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. #10
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #11
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #12
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #13
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

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

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by manibby93 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default 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. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default 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
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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 LastLast

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  2. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-11-2009, 12:35 AM
  3. Replies: 3
    Last Post: 12-10-2008, 02:38 AM
  4. Replies: 0
    Last Post: 05-22-2008, 11:53 AM

Posting Permissions

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