[Android Software Development] tjdwowh:
Lesson #1-6: Fun(?) with Math

Math is pretty straightforward. Let's begin with a few arithmetic operators.

+ : addition
- : subtraction
/ : division
* : multiplication
% : returns the value of the remainder.

Examples:

class MathLearning {

[COLOR="SeaGreen"]	// These are class-wide variables:
[/COLOR]	static int a = 10;
static int b = 15;
static int c = 22;

[COLOR="Blue"]	public static void main(String[] args) {
[/COLOR]
[COLOR="SeaGreen"]// The following are local variables
// They belong to the main method only.[/COLOR]

int result;
int resultTwo;
int resultThree;
int resultFour;
int resultFive;

[COLOR="SeaGreen"]	// These assign values to the five "result variables".
// (You could've assigned when you declared them above)[/COLOR]

result = a + b;
resultTwo = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;

[COLOR="Green"]	//The following statements
//invoke the blahBlahBlah method
//by "inputting" the required
//integer parameter.
[/COLOR]
blahBlahBlah(result);
blahBlahBlah(resultTwo);
blahBlahBlah(resultThree);
blahBlahBlah(resultFour);
blahBlahBlah(resultFive);

[COLOR="blue"]	}
[/COLOR]
[COLOR="DeepSkyBlue"]	public static void blahBlahBlah(int output) {
[/COLOR]

[COLOR="SeaGreen"]	//This method takes the parameter
//and creates a variable called output.
//This variable is then displayed
//in the below statement.
[/COLOR]

System.out.println(output);
[COLOR="DeepSkyBlue"]}[/COLOR]

}

Before you copy and paste the code to Eclipse, let's see if you know what the code will do.

Try and follow the code to see if you can write down the five outputs.

If you get stuck, check below:

1. We first declare a new class called MathLearning.

2. Next we declare 3 class-wide variable integers: a, b, and c.

3. We move on to create the main method with its typical parameter (String[] args).

4. Then we create 5 integers. Unlike before, we don't initialize (meaning we don't give them a starting value).

5. We assign values to the 5 integers that we created in step 4.

6. The statements that begin with blahBlahBlah... are invoking the blahBlahBlah method with a required integer parameter.

7. We setup the blahBlahBlah(int output) method.

Important notes:

The location of the blahBlahBlah(int output) method does not matter. We can put it above the main method or below the main method.

The main method, if you recall, will ALWAYS run first.

When the statements from step 6 above are "reached" (again, think Top to Bottom in the main method), Java will look for the method by the name of blahBlahBlah and follow

the instructions contained therein.

The resulting output is...

25
-5
220
1
7

Wait a second. Everything makes sense, but resultFour is completely off!

22/15 is not 1... or is it?

Remember that the variables c and b (22 and 15) are both integers.

resultFour, which is calculated using c / b is also an integer. This means that when the calculation happens, the computer will

round down to the nearest whole number to keep the value of resultFour as an integer, as in the remainder is just thrown away.

This is why you do not see a decimal result, but a lower value of 1.

The solution? Let's change all the int declarations to double declarations.

There's another problem. We get five lines of numbers, but they aren't labeled.

If we want each of the lines to display:

Result 1 is 25.

Result 2 is -5.

Result 3 is 220.

...

...

What could we do?

There's only one line that outputs text (System.out.println...) within the blahBlahBlah class, and it treats all the inputs the same way.

It has no idea of telling whether a given input is result or resultFive.

This is when we use a counter.

To do so, we will learn... two more operators today:

++ : adds one to the variable.
-- : subracts 1 from the variable.

There are differences between:

//This is the pre-increment operator
++variable;

and

/This is the post-increment operator
variable++;

But the latter is much more commonly used and you can (for the most part) accomplish the exact same things without the former,

so we won't waste time discussing it at the moment.

Example of a counter follows:

class Counter {

[COLOR="SeaGreen"]	//Create a class-wide integer called counter.
[/COLOR]	static int counter = 0;

[COLOR="Blue"]public static void main(String[] args) {[/COLOR]

[COLOR="SeaGreen"]//This is a loop. It will keep
//repeating everything contained
//inside the loop while the
//conditions are met![/COLOR]

[COLOR="Orange"]while (counter < 10){[/COLOR]

[COLOR="SeaGreen"]//This post-increment operator adds 1.[/COLOR]
counter++;
[COLOR="SeaGreen"]	//Display the new counter value.[/COLOR]
System.out.println(counter);
[COLOR="ORange"]}[/COLOR]

[COLOR="Blue"]}[/COLOR]

}

Let's dissect this class.

1. We declare a Counter class.

2. We create an integer called counter with initial value of zero.

3. We create a main method.

4. Inside the main method, we create a "while loop" (we will learn this soon!)

5. The while loop, as long as counter is below 10, will add one, and display that number.

int counter starts at zero. As soon as Java reaches the while loop, it will add one, and print the new value of counter: 1.

int counter is now 1. The loop will add 1 again and print the new value of counter: 2.

int counter is now 2. The loop will add 1 again and print the new value of counter: 3.

...

int counter is now 10. The while (counter < 10) condition is no longer met, so the loop ends.

________________

Now let's apply this to the first MathLearning class example:

class MathLearning {

[COLOR="SeaGreen"]// These are class-wide variables:[/COLOR]
static double a = 10;
static double b = 15;
static double c = 22;

[COLOR="SeaGreen"]	//Setup a counter.
[/COLOR]	static int counter = 0;

[COLOR="Blue"]	public static void main(String[] args) {
[/COLOR]
[COLOR="SeaGreen"]// The following are local variable
// They belong to the main method only.[/COLOR]

double result;
double resultTwo;
double resultThree;
double resultFour;
double resultFive;

[COLOR="SeaGreen"]// These assign values to the five "result variables".[/COLOR]

result = a + b;
resultTwo = a - b;
resultThree = a * c;
resultFour = c / b;
resultFive = c % b;

[COLOR="SeaGreen"]//The following statements
//invoke the blahBlahBlah method
//by "inputting" the required
//integer parameter.[/COLOR]

blahBlahBlah(result);
blahBlahBlah(resultTwo);
blahBlahBlah(resultThree);
blahBlahBlah(resultFour);
blahBlahBlah(resultFive);

[COLOR="Blue"]
}[/COLOR]

[COLOR="RoyalBlue"]public static void blahBlahBlah(double output) {[/COLOR]

[COLOR="SeaGreen"]//Add one to counter.[/COLOR]
counter++;

[COLOR="SeaGreen"]//This method takes the parameter
//and creates a variable called output.
//This variable is then displayed
//in the below statement.[/COLOR]

System.out.println("Result " + counter + " is " + output);
[COLOR="RoyalBlue"]}[/COLOR]

}

Compare this to today's first example.

1. I changed all the int declarations to double declarations to show decimal values.

2. I created a static int called counter (if this is not static, each time that the method is invoked,

the value of counter will be 1, as each instance of this method will get its own counter integer), and used it to label each line of output.

The console will display:

Result 1 is 25.0
Result 2 is -5.0
Result 3 is 220.0
Result 4 is 1.466...
Result 5 is 7.0

3. Let's talk about this:

System.out.println("Result " + counter + " is " + output);

You are familiar with System.out.println();

But what are the pluses used for?

In this case, these pluses are not additions. They just let you combine different variables and literals into one String piece that can be displayed

using System.out.println();

So what ("Result " + counter + " is " + output) is really showing is:

"Result counter is output"

And we will conclude Day 4 here. :D

Thanks for reading!

Confused? PM me. I will reply ASAP! :)