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.

Printable View

- 01-12-2013, 09:50 PMhockey101Studying 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.

- 01-12-2013, 10:05 PMFubarableRe: 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.

- 01-12-2013, 10:06 PMhockey101Re: Studying for Finals
All I wanted to know if that statement was correct or not

- 01-12-2013, 10:07 PMhockey101Re: Studying for Finals
But i think i figured it out on my own...thanks

- 01-12-2013, 10:18 PMFubarableRe: Studying for Finals
- 01-12-2013, 10:27 PMhockey101Re: 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; - 01-12-2013, 10:30 PMFubarableRe: Studying for Finals
What happens when you write out what happens on paper? It will take at most 5 lines to do.

- 01-12-2013, 10:41 PMhockey101Re: 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 - 01-12-2013, 11:00 PMFubarableRe: 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

Code:`i x`

0 0

1 1

2 3

3 6

4 10

terminate

- 01-12-2013, 11:02 PMhockey101Re: Studying for Finals
I thought thats what I did?

- 01-12-2013, 11:04 PMFubarableRe: Studying for Finals
See edit above please.

- 01-13-2013, 04:21 PMekay90Re: Studying for Finals

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.