Showing posts with label Logical. Show all posts
Showing posts with label Logical. Show all posts

Thursday, January 17, 2013

! operator (NOT)



The exclamation character is the NOT operator !
The NOT operator is used to evaluate an expression to true or false.


Here's the NOT operator using a simple if statement format:

if (!(condition1)) {
   doSomething();
}

Here's the NOT operator using a if-then-else statement format:



if (!(condition1)) {
   doSomething();
}

else {
   doSomethingElse();
}


The method doSomething() will only execute if the condition in the if statement evaluates to false.

Let's take a look at another, more concrete example:


if (!(respawnRemaining >= 1)) {
   gameOver();
}


The method gameOver() will only execute if respawnRemaining is NOT greater than or equal to 1.

In the following example, we use an if-then-else statement:



if (!(respawnRemaining >= 1)) {
   gameOver();
}

else {
   respawn();
}

If respawnRemaining is NOT greater than or equal to 1, the gameOver method will execute. If it is greater than or equals to 1, the respawn method will execute.



Also see
&& Operator (AND)
|| Operator (OR)


Tuesday, January 8, 2013

&& operator (AND)


A double ampersand is the logical AND operator &&

The AND operator is used to evaluate multiple expressions during decision making with if statements.


Here's the AND operator using a simple if statement format:

if (condition1 && condition2) {
   doSomething();
}

Here's the AND operator using a if-then-else statement format:


if (condition1 && condition2) {
   doSomething();
}
else {
   doSomethingElse();
}


The method doSomething() will only execute if both of the conditions in the if statement evaluate to true.

Let's take a look at another, more concrete example:


if (maxDamage > 100 && ammoCount == 0) {
   gameOver();
}


The method gameOver() will only execute if maxDamage is greater than 100 AND ammoCount is equal to 0. If either condition does not evaluate to true, the gameOver() method will not execute.

In the following example, we use an if-then-else statement:


if (maxDamage > 100 && ammoCount == 0) {
   gameOver();
}
else {
   continue();
}


If both conditions are true, the gameOver() method will execute. If both of the conditions are false, the continue() method will execute.

Also see
|| operator (OR)
! Operator (NOT)

|| operator (OR)



A double pipe is the logical OR operator ||

The OR operator is used to evaluate multiple expressions during decision making with if statements.


Here's the OR operator using a simple if statement format:

if (condition1 || condition2) {
   doSomething();
}

Here's the OR operator using a if-then-else statement format:


if (condition1 || condition2) {
   doSomething();
}
else {
   doSomethingElse();
}


The method doSomething() will only execute if one or both of the conditions in the if statement evaluate to true.

Let's take a look at another, more concrete example:


if (maxDamage > 100 || ammoCount == 0) {
   gameOver();
}


The method gameOver() will only execute if either maxDamage is greater than 100 (and or) OR ammoCount is equal to 0. If either condition does not evaluate to true, the gameOver() method will not execute.

In the following example, we use an if-then-else statement:


if (maxDamage > 100 || ammoCount == 0) {
   gameOver();
}
else {
   continue();
}

If either or both of the conditions are true, the gameOver() method will execute. If both of the conditions are false, the continue() method will execute.

Also see
&& Operator (AND)
! Operator (NOT)


Friday, December 21, 2012

Consider if 2 objects are equal....

Demo Code Project: OverridingEquals.zip

Let's say you have the need to ensure that no 2 objects have the same contents - you want to check if one object is equal to another?

What do you consider equal?

Read on...

equals(Object)
The default implementation of the equals method, provided in all java objects (java.lang.Object) is to compare the memory location of one object, with another.


Consider the example above.

Two objects of type Asteroid1 have been instantiated using identical parameters - both a1 and a2 objects have the same contents. They do not, however, have the same memory location.

When the new operator is used, it causes the system to allocate memory space for the data and as such, a1 and a2 objects have unique memory locations.

When the equals method is used to compare the equality of these 2 objects, the equals method will return false - because each object has a different memory location.

If, for the purposes of your program, you consider that object equality is based on memory location only, the default implementation of the equals method is sufficient.

If however, you consider that object equality is based on the contents of objects themselves (more likely), read on...

overriding equals(Object)

All java objects (java.lang.Object) contain a default implementation of an equals method which compares the memory location of objects to determine equality. When writing your own libraries, you may find that this version of the method is not fit for your purpose. In this case, you can override the method and implement your own version of what you consider to be equality. It is usually the case that object equality relies on comparing the contents of the objects - not the memory locations.

When a class overrides a method, it is providing it's own version of a method that has already been provided by it's parent class (such as java.lang.Object).

You can override the equals method simply by providing your own, in your class. A couple of things to remember though:

The method signature must be the same as the one provided by java.lang.Object:

  • Public
  • Boolean return type
  • method name must be equals
  • takes a single parameter of Object type

...which looks like...



OK, now for the implementation. Our goal is to compare the contents of the object parameter with the contents of the current object.

Here is the definition of the class called Asteroid2:



Note that the equals method has been included in the method list.

The contents of this class are the values for the name, size and rotationRate fields. In our implementation of equals, we will write code which considers equality based upon the values of these fields.

Basically, if the current object has the same name as the parameter object and the current object has the same size as the parameter object and the current object has the same rotationRate as the parameter object, we will consider the current object and the parameter object to be equal.

Here's the code:






The first line ensures that the parameter object (aThat) being passed to the method is of the correct type - in this case Asteroid2. The instanceof operator (compares an object to a specified type) is used to determine the type. The method returns false immediately if this is not found to be true - after all, if the parameter object isn't of type Asteroid2 then it will never be equal to any instance of Asteroid2.

The second line then goes ahead and casts (converts) the parameter object to the type it is being compared to - in this case, Asteroid2. Once the cast has taken place, we can then treat the parameter object (aThat) as an instance of Asteroid2 (That) and call each of it's methods to begin determining equality.

The return statement uses the logical operator && to join together a number of expressions, relating to the fields on the objects, which will ultimately evaluate to true or false.

Once again...basically, if the current object has the same name as the parameter object and the current object has the same size as the parameter object and the current object has the same rotationRate as the parameter object, the method will return true.

Calling the equals method
Let's assume that we wish to add a new Asteroid2 object to an existing ArrayList of Asteroid2 objects but we want to make sure that an object with the same contents does not already exist in the ArrayList.

The example below uses a loop to compare the new Asteroid2 object with each object in the ArrayList:


Because the Asteroid2 class has overriden equals, we can be assured that only objects containing unique contents will be added to the ArrayList.

Incidentally, the ArrayList has a contains method which evaluates object equality in the same way - dispensing with the need for a loop. It simply calls the equals method on each object in the ArrayList - equality is based on whether the method has been overriden by the class in question (contents) or not (memory location).

One last thing...

We can also quickly return from the equals method by checking if the parameter object has the same memory location (and hence is the *same* object) as the current object.


When the equal to operator (==) is used on objects (reference types), the references (memory locations) are compared.

see also:

"Object (Java Platform SE 7 )" ( http://bit.ly/YrEmpk )

"Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)" ( http://bit.ly/Zn8eop )