The Problem with “if” Statements
Responses for All Conditions
To explore the first limitation of if statements by themselves, let’s revisit our DiceRoll application. Recall the following rollDice() method below.In order to make the program run properly, we would need to cover cR == yR.
“if”-”else”
Let’s pretend we’re writing code for a space shooter game. You would need to return a “hit” when one of your laser blasts hits an enemy. The problem is that there are an infinite number of places your laser blasts could fly, and only one location where the enemy is. To deal with this, we need to add an else statement. You can think of an else as meaning “In all other cases….”.if ( condition is true )
{
do something;
}
else //In all other cases...
{
do something else;
}
An else statement does not need a condition in parentheses because it covers all other conditions if none of the previous ones evaluate to true. As an example of an else, let’s try to imagine what our space shooter code would look like. 
As the enemy moves around, we try to aim our laser at the right target. If we pick the right target, we get a hit response…...For all other situations, we get a “Keep trying!” message.
Exclusion: else if
All of the conditions proposed in Lesson_09 were mutually exclusive. In programming, the term mutually exclusive means that two conditions could never both be true. For example, in the OddOrEven class, the two possible conditions were mutually exclusive because no number could ever be both even and odd. But in many of our functions we will need to add exclusions using an else if. An else if statement adds exclusion from one condition to the next.
As an example, if you were writing software for a record company, you may want to write a program that categorizes albums into 4 categories.
Output: |
The conditions state that if a record sells one million or more copies it is designated as a “Top Seller”. A “Good Seller” has sold five hundred thousand copies, while an “Average Seller” has sold one hundred thousand. If an album sells less than one hundred thousand, it may need to be discontinued, and is therefore designated as “Needs Review”.
Despite the fact that our friend James Brown sold 1,500,000 units (and was the greatest musician to ever make music ), he is still given the category “Average Seller”. Can you guess what happened? In this program, James Brown’s album meets the first condition, making it a “Top Seller”. However, the album meets the second condition as well, making it a “Good Seller”. The code runs top to bottom, so sadly our dear James ends up on the last condition he meets, which is “Average Seller”.
To fix this injustice, we must add exclusion to each of the statements, so that each album gets placed in the highest category it meets the conditions for.
An else if evaluates the next condition only if the previous condition is not met….
if (condition 1) //if true
{
run this code;
}
else if(condition 2) //if true (only if condition 1 is false)
{
run this code;
}
else if (condition 3) //if true (only if condition 2 is false)
{
run this code;
}
else //in all other cases...
{
run this code;
}
Try it With OOP Architecture
Now let’s fix our AlbumSales class to make it object-oriented…
Delete the variables, and the main method. The main method will now be in SalesRunner. Place the if statements in a method called categorize().
Declare your instance variables and set up the default constructor...
Just below the modifier, add your accessors….
The toString() Method
Java contains a class called Object, and every object we instantiate is a member of the Object class. Therefore, there are a few things that are common to all objects. One of this things is the toString() method. The toString() method is defined in the Object class, and therefore every object we create gets a toString() implicitly. For our purposes, implicit means that something is present but not visible in our code. Some code is explicit, meaning the programmer types it in, and it is visible to other programmers.
So what do we do with toString()? The simplest explanation: toString() is method built into all Java objects that returns a String representation of an object. It is designed to take the data stored in an object, and represent it in a String of characters. Unless told otherwise, Java stores your data as hash code, or a 32-bit character String.
If call the james object directly, without giving any additional information……
the james object’s toString() method returns the hash code value.
But we can override the default toString() with our own method. Write the following code onto the end of your Sales class….
Now when we run....
-------------------------------------------------------------------------------------------------------------------------
APLab_10
Follow the link to your labs here.