Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > my thread of basic Java questions

Reply
Thread Tools

my thread of basic Java questions

 
 
Arne Vajhj
Guest
Posts: n/a
 
      04-13-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Apr 13, 11:51 am, Arne Vajhj <(E-Mail Removed)> wrote:
>> Arne Vajhj wrote:
>>> (E-Mail Removed) wrote:
>>>> Why is the following true?:
>>>> (ok || (n1 < 0))
>>>> And what the heck is "ok"?
>>>> My guess: any String or character or whatever is "true" on its own,
>>>> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
>>>> answer to the question would still be true.
>>> What is ok declared as ?

>> And what is its value ?

>
> boolean ok = true;
> int n1 = 100;
> int n2 = 0;
>
> They forgot to put that in the question. I guess it makes sense now.


Yes.

(ok || (n1 < 0)) = (true || (100 > 0)) = (true || false) = true

Arne
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      04-13-2008
Lew wrote:
> (E-Mail Removed) wrote:
>>> Why is the following true?:
>>> (ok || (n1 < 0))

>
>>> boolean ok = true;
>>> int n1 = 100;
>>> int n2 = 0;
>>>
>>> They forgot to put that in the question. I guess it makes sense now.

>
> Arne Vajhøj wrote:
>> Yes.
>>
>> (ok || (n1 < 0)) = (true || (100 < 0)) = (true || false) = true

>
> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
> never evaluated once 'ok' is found to be true.
>
> (ok || (n1 < 0)) == (true || <unevaluated>) == true


Good point.

If it is one of those Java language test, then that could be
considered important.

Arne
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      04-13-2008
Arne Vajhøj wrote:
> Lew wrote:
>> (E-Mail Removed) wrote:
>>>> Why is the following true?:
>>>> (ok || (n1 < 0))

>>
>>>> boolean ok = true;
>>>> int n1 = 100;
>>>> int n2 = 0;
>>>>
>>>> They forgot to put that in the question. I guess it makes sense now.

>>
>> Arne Vajhøj wrote:
>>> Yes.
>>>
>>> (ok || (n1 < 0)) = (true || (100 < 0)) = (true || false) = true

>>
>> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
>> never evaluated once 'ok' is found to be true.
>>
>> (ok || (n1 < 0)) == (true || <unevaluated>) == true

>
> Good point.
>
> If it is one of those Java language test, then that could be
> considered important.


Yup, if( ok || (n1++ < 0) ){}, what is the value of n1 after this
statement?

Still 100 because the ++ never got evaluated.

I'd like to see a link to this "answer key" the OP is referring too. If
it's really Java and not some other language, I don't think it's a very
good one. But I'd like to check it out myself.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      04-13-2008
Mark Space wrote:
> Arne Vajhøj wrote:
>> Lew wrote:
>>> (E-Mail Removed) wrote:
>>>>> Why is the following true?:
>>>>> (ok || (n1 < 0))
>>>
>>>>> boolean ok = true;
>>>>> int n1 = 100;
>>>>> int n2 = 0;
>>>>>
>>>>> They forgot to put that in the question. I guess it makes sense now.
>>>
>>> Arne Vajhøj wrote:
>>>> Yes.
>>>>
>>>> (ok || (n1 < 0)) = (true || (100 < 0)) = (true || false) = true
>>>
>>> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
>>> never evaluated once 'ok' is found to be true.
>>>
>>> (ok || (n1 < 0)) == (true || <unevaluated>) == true

>>
>> Good point.
>>
>> If it is one of those Java language test, then that could be
>> considered important.

>
> Yup, if( ok || (n1++ < 0) ){}, what is the value of n1 after this
> statement?
>
> Still 100 because the ++ never got evaluated.
>
> I'd like to see a link to this "answer key" the OP is referring too. If
> it's really Java and not some other language, I don't think it's a very
> good one. But I'd like to check it out myself.


That type of code is not good code, but is exactly what some tests
like to check.

Arne
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-13-2008
Arne Vajhøj wrote:

> That type of code is not good code, but is exactly what some tests
> like to check.


Yup, exactly what I was thinking. Not a good pattern, but something
that you would expect to see on a test.
 
Reply With Quote
 
denim_genes@hotmail.com
Guest
Posts: n/a
 
      04-14-2008
On Apr 13, 4:19 pm, Mark Space <(E-Mail Removed)> wrote:
> Arne Vajhj wrote:
> > That type of code is not good code, but is exactly what some tests
> > like to check.

>
> Yup, exactly what I was thinking. Not a good pattern, but something
> that you would expect to see on a test.


This is an introductory programming course, hence the poor quality of
questions (if that's any excuse).

I found myself handing in assignments with increased efficiency (not
having variables I didn't need) and error-checking when they weren't
even on the grading criteria. Really a basic course.

Which is why I'm ****ed... I totally didn't study anything related to
making classes (no time), so I may have lost marks on that part of the
exam I just wrote. I assumed I would handle their basic questions, but
they did ask some rather weird ones.

Anyway, thnx.
 
Reply With Quote
 
Lionel van den Berg
Guest
Posts: n/a
 
      04-14-2008
On Sun, 13 Apr 2008 13:00:38 -0400, Lew wrote:
> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
> never evaluated once 'ok' is found to be true.
>
> (ok || (n1 < 0)) == (true || <unevaluated>) == true



Partly related I often use:

if (object != null && object.hasSomeCondition) {
executeFooBaa();
}

I rely on the fact that the second part of the condition never gets
executed if object == null. This seems a little sloppy on my behalf but
it looks nicer than:

if (object != null) {
if (object.hasSomeCondition) {
executeFooBaa();
}
}

What does everyone else do?

Lionel.
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-14-2008
Lionel van den Berg wrote:

> if (object != null && object.hasSomeCondition) {
> executeFooBaa();
> }


When Arne called my code "not good," I think he was referring to the use
of side effects and the post increment ++ operator in a comparison. Or
at least I assumed he was.

Short circuit operators like above are fine. Side effects can be
questionable. ++ and -- are both especially subject to abuse.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      04-15-2008
Mark Space wrote:
> Lionel van den Berg wrote:
>> if (object != null && object.hasSomeCondition) {
>> executeFooBaa();
>> }

>
> When Arne called my code "not good," I think he was referring to the use
> of side effects and the post increment ++ operator in a comparison. Or
> at least I assumed he was.
>
> Short circuit operators like above are fine. Side effects can be
> questionable. ++ and -- are both especially subject to abuse.


I use that construct myself.

Extremely strictly speaking then crashing versus not crashing could
be considered a side effect.

But I don't think this is a real problem.

The ++ and -- could be a problem, but even those are
not the worst.

It is:

if(something || sometmethodthatchangestate())

that really make it hard to follow the logic.

Arne
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-24-2008
On Sat, 12 Apr 2008 20:59:34 -0700 (PDT), (E-Mail Removed)
wrote, quoted or indirectly quoted someone who said :

>Why is the following true?:
>
>(ok || (n1 < 0))


it isn't. It is true if and only if the boolean variable ok is true or
if n1 < 0.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java xml basic questions Rolf.Kemper@eu.necel.com Java 4 05-02-2007 02:02 PM
Clicking on Sun Java Plugin installation evokes 3 basic java setup questions Bonnie Johnson Java 2 01-10-2007 10:13 AM
Java install on Linux FC4 (Two very basic questions). AnonymousFC4 Java 3 07-29-2005 12:05 AM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments