homepage Welcome to WebmasterWorld Guest from 54.237.213.31
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

    
beginner java
matilda

10+ Year Member



 
Msg#: 1077 posted 9:32 am on Oct 2, 2001 (gmt 0)

Hey everybody!
Could anyone help with this? All the <, ?, and : are fairly confusing........

int x,y,z;
x = 1; y = 1, Z = 1;
x += y += z;
System.out.println(x < y ? y : x);
System.out.println(x < y ? x ++ : y ++);
System.out.println(x);
System.out.println(y);

The result is supposed to be one of these:
a) 4 3 2 1
b) 3 2 3 3
c) 3 3 3 3

Any tips welcome. Thank You!!!
Mat*

 

Xoc

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1077 posted 2:51 pm on Oct 2, 2001 (gmt 0)

x += y means add y to x and put the result back into x

x < y ? y : x means

if x < y then
y
else
x
end if

x++ means add 1 to x and put the result back into x, but only after doing the rest of the operation that x is involved in

mivox

WebmasterWorld Senior Member mivox us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1077 posted 4:26 pm on Oct 2, 2001 (gmt 0)

The result is supposed to be one of these:
a) 4 3 2 1
b) 3 2 3 3
c) 3 3 3 3

Y'know, statistically speaking, I've heard that you'll tend to get the best scores on multiple-choice exams by sticking with your first gut-instinct answer. ;)

agerhart

WebmasterWorld Senior Member agerhart us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1077 posted 5:49 pm on Oct 2, 2001 (gmt 0)

I remember hearing that on multiple choice it was always safe to go with "C".........so:

c) 3 3 3 3 hehe :)

MikeFoster

10+ Year Member



 
Msg#: 1077 posted 6:58 pm on Oct 2, 2001 (gmt 0)

You'll get an error with this because "z" is not declared... "Z" is declared but not used. So...

int x=1,y=1,z=1;
x += y += z;
System.out.println(x < y ? y : x);
System.out.println(x < y ? x++ : y++);
System.out.println(x);
System.out.println(y);

Be careful with this: x += y += z;
What you really have is this: x += (y += z);
And that's ok if that's what you want.
Solve it from the inside out...

z is 1
y becomes 2
x becomes 3

Now the output statements...

x is not < y, so print x, which is 3
x is not < y, so print y, which is 2,
then increment y by 1, and y becomes 3
print x, which is 3
print y, which is 3

The result is...

3
2
3
3

The answer is B.

MikeFoster

10+ Year Member



 
Msg#: 1077 posted 7:00 pm on Oct 2, 2001 (gmt 0)

BTW, Xoc gave a very good description of how the operators work.

ggrot

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1077 posted 11:40 pm on Oct 2, 2001 (gmt 0)

Sure sounds to me that you are doing someone's homework for them.

matilda

10+ Year Member



 
Msg#: 1077 posted 8:33 am on Oct 3, 2001 (gmt 0)

thanx very much for your help. It is indeed part of my homework but I'm doing a correspondence course and my teacher is not very helpful, if ever he replies to a question.
ciao
*M*

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved