Results 1 to 12 of 12
  1. #1
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Studying for Finals

    Alright guys, I am trying to study for my Computer Programming 1 final on tuesday and I have a question. My question is you can pass an int as a double but you can't pass a double as an int? I want to know if that statement is correct.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Studying for Finals

    It might be quicker to test this yourself first with your handy Java compiler. Then check out the JLS or Java Language Specification on Widening Primitive Conversion. as well as the next chapter on Narrowing Primitive Conversion.
    Last edited by Fubarable; 01-12-2013 at 09:07 PM.

  3. #3
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Studying for Finals

    All I wanted to know if that statement was correct or not

  4. #4
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Studying for Finals

    But i think i figured it out on my own...thanks

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Studying for Finals

    Quote Originally Posted by hockey101 View Post
    But i think i figured it out on my own...thanks
    I'm of the "teach a man to fish" school. You may wish to bookmark the JLS link as it may come in handy. I know that it's helped me out quite a bit!

  6. #6
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Studying for Finals

    Ok now i have another question that I cant seem to solve...

    Given the following code, where x=0, what is the resulting value of x after the for-loop terminates?

    for (int i=0; i<5; i++)
    x += i;

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Studying for Finals

    What happens when you write out what happens on paper? It will take at most 5 lines to do.

  8. #8
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Studying for Finals

    Thats what I tried doing......
    x i
    0 0
    0 1
    1 2
    3 3
    6.....terminates....but thats not one of my answers...my answers are

    a 0
    b 4
    c 5
    d 10
    e 15

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Studying for Finals

    You may be forgetting that += will add the current number the number already held by x.

    So if x = 20, then x += 15 will make x == to 35.

    Edit: Ah, you may be doing your math wrong. Not

    0 0
    0 1
    1 2
    3 3
    6.... terminate

    but rather

    Java Code:
    i     x
    0    0
    1    1
    2    3
    3    6
    4   10
    terminate
    Last edited by Fubarable; 01-12-2013 at 10:03 PM.

  10. #10
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Studying for Finals

    I thought thats what I did?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Studying for Finals

    See edit above please.

  12. #12
    ekay90 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Studying for Finals

    Quote Originally Posted by hockey101 View Post
    Thats what I tried doing......
    x i
    0 0
    0 1
    1 2
    3 3
    6.....terminates....but thats not one of my answers...my answers are

    a 0
    b 4
    c 5
    d 10
    e 15

    for (int i=0; i<5; i++)
    x += i;

    Starting off with x = 0, and the formula is x += i
    This formula means to keep adding the values of i on to the value of x.

    Going through the values of 'i' up to 4 (because 4 is less than 5) here is what we get:
    When i = 0:
    0 + 0 = 0

    When i = 1:
    0 + 1 = 1

    When i = 2:
    1 + 2 = 3

    When i = 3:
    3 + 3 = 6

    When i = 4:
    6 + 4 = 10

    Therefore the last value of x before the loop terminates is 10.

Similar Threads

  1. Studying progress
    By tnrh1 in forum Forum Lobby
    Replies: 2
    Last Post: 02-25-2012, 10:10 PM
  2. Midterm studying
    By Bgreen7887 in forum New To Java
    Replies: 0
    Last Post: 03-15-2011, 02:04 PM
  3. Studying Directions
    By Deepwoods in forum New To Java
    Replies: 3
    Last Post: 08-19-2010, 08:53 PM
  4. Local Finals
    By Drun in forum New To Java
    Replies: 6
    Last Post: 11-20-2009, 06:50 AM
  5. Need a tutorial for Studying about Panels
    By ramachandran in forum New To Java
    Replies: 1
    Last Post: 10-25-2007, 09:05 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
  •