Tuesday, October 6, 2015

Lesson_10: Adding else and else if

The Problem with “if” Statements

Using if statements in java makes things interesting. But by themselves they have a couple of limitations.

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.




Output:

Some of you may have noticed while completing your labs that the rollDice() method did not cover all of the possible conditions that could occur if we compared cR and yR. You may have asked “What happens if cR and yR are equal?” You may even have seen this happen, causing there to be no output from the program. 






In order to make the program run properly, we would need to cover cR == yR.


This actually works for the DiceRoll() program. But what if we wanted one response for a certain condition, and another response for every other condition? But the problem with using only ifs is that you would need to write a separate if statement for every possible condition that exists.


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.
Output:


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.



The conditions in this program are arranged from highest number to lowest number. Therefore, an album’s sales will be compared to the highest category it belongs to first. Then, it will encounter an else if. Remember that the word else means “In all other cases….”. But it also means only in other cases. So if the first condition is true, that response runs, and the others are ignored.

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 rewrite our AlbumSales program with the OOP architecture. Create a new class and call it “SalesRunner”.




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...



Set up your constructor and modifier…



Just below the modifier, add your accessors….



Now let’s write the main method…..





Output:


And run...





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....



We get the output....
















-------------------------------------------------------------------------------------------------------------------------

APLab_10

Follow the link to your labs here.