How to do and 'and' and an 'or' in the one if statement?

Please show me if there is an easier way but i'm just stuck at the moment with this code

if((currentYear - aVehicle.getYearOfManufacture() >= 7) || (currentYear - aHGV.getYearOfManufacture() >= 4))

{

System.out.println("This vehicle needs a roadworthy test:");

System.out.println(aVehicle.toString());

System.out.println(aOwner.toString());

}else{

System.out.println("No vehicles need a road test yet");

}

I havn't bothered pasting all the code as i don't think there's any need. What i want this method to be able to do is:

if the currentYear minus the vehicles year of manufacture >=7 AND/OR the currentYear minus the HGV's year of manufacture >=4

its prints out the correct procedure.

I don't know how to implement the AND/OR part if you understand? As you can see all i have in is the ||. I tried ||&& but that gives me comile error as you probably know

Thanks.

Message was edited by:

Capsud

[1243 byte] By [Capsuda] at [2007-11-27 4:28:13]
# 1
A || B is true if and only if at least one of A, B is true. So this is already "and/or".
Hippolytea at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 2
|| is basically and/or. If only one of the conditions is true, or if both conditions are true, then || will evaluate to true. Exclusive or is different in that it's only true if only one condition is true, but not both.
hunter9000a at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 3
you can use && instead of ||
dooie07a at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 4
> you can use && instead of ||He can use whatever he wants to, it just won't give him the correct results. && is and, it won't work if he wants and/or.
hunter9000a at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 5
could he use a boolean of booleans, ie if(condition 1 = false && condition 2 = false {return false;} else {return true;}New to java but thats how i would try doing it.Doo
dooie07a at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 6

> could he use a boolean of booleans, ie

>

> if(condition 1 = false && condition 2 = false {

> return false;

> } else {

> return true;

> }

>

> New to java but thats how i would try doing it.

>

> Doo

then you would be doing much more work than needed.

Using || is the simplest way, and the simplest code is the easiest code to maintain.

Aknibbsa at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 7
> then you would be doing much more work than needed.> > > Using || is the simplest way, and the simplest code> is the easiest code to maintain.But in theroy would it work? Get it working, see it working, make it better?
dooie07a at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 8

> > then you would be doing much more work than

> needed.

> >

> >

> > Using || is the simplest way, and the simplest

> code

> > is the easiest code to maintain.

>

> But in theroy would it work? Get it working, see it

> working, make it better?

No, what he wants is to check if either both are true, (which && and || both would work for) or if one is true and one is false, (which || only would work).

condition1 | condition2 | && |||

--

T|T| T|T

T|F| F|T

F|T| F|T

F|F| F|F

hunter9000a at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 9

no using && is not the same as and/or

Best way to remember it is the following:

when someone asks you if you want cream or sugar in your coffee that is an inclusive or -you can get both of them

when someone asks you if you want a car or a truck that is an exclusive one - you only get one of the two but not both

Aknibbsa at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 10
Great thanks guys!
Capsuda at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...
# 11

Aside: if you want exclusive-or (soup or salad) you can write:

if ( A ^ B ) {

}

One could make an aside here about conditional execution, but I don't nest asides!

Extra credit: when is (A ^ B) ^ C true? How about A1 ^ A2 ^ ... An?

Hippolytea at 2007-7-12 9:36:56 > top of Java-index,Java Essentials,New To Java...