[Android Software Development] tjdwowh:
Lesson #1-7: More Fun with Math

A quick review:

In Java, you will frequently see statements written like so:

Given that number is an integer...

number = number + 5;

This tends to confuse many beginning programmers, because they are missing the fundamental purpose of the equal sign.

Like I've said before, the equal sign does not just state equality; instead, it is used to ASSIGN a value.

In other words, it doesn't just state that the operand on the left and the value on the right are equal. Instead, it says that the operand is now equal to the value.

Interpreting the example above, you should think to yourself, 'number is now equal to number + 5.' This will save you a lot of headache.

If you understand this, here is a list of equivalent expressions that will save you some time in programming:

number = number + 5;
//can be written as:
number += 5;

number = number - 5;
//can be written as:
number -= 5;

number = number * 5;
//can be written as:
number *= 5;

number = number / 5;
//can be written as:
number /= 5;

Lesson #1-8: Randomization

Predictability makes for boring games. If you want games to challenge players, you will want to use randomization.

Let's use TUMBL as an example. For those of you who are not familiar with the game, click here.

Each row is generated randomly, meaning that the player will not be able to figure out a pattern that lets them stay alive longer.

Stars are generated randomly on the left or right side of the screen.

Power-ups appear randomly.

And so on.

So how do you integrate randomization to a game?

It's quite simple. Take a look at this example class:

[COLOR="SeaGreen"]//In this class, we will create a Random object called rand.
[/COLOR]
[COLOR="Blue"]class Randomization {
[/COLOR]
[COLOR="RoyalBlue"]	public static void main(String[] args) {[/COLOR]

Random rand = new Random();

[COLOR="RoyalBlue"]}[/COLOR]

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

In this class named Randomization, we create a Random object called rand.

Let's break up this statement:

Random rand

think of this just like:

int number

You are creating a new object called rand using the type Random.

The second part of the statement:

= new Random()

assigns a new Random object with a default seed (I will explain seed in a second) as the value of the newly-created rand.

In other words...

you are basically telling the computer:

"I want to create a Random object called rand. Use the Random class to create this new object."

If you are confused, that's okay. We will go into object creation and constructors in a lot more detail in the coming lessons.

Just know that:

Random rand = new Random();

creates a new Random object called rand (you can change this name).

Mini Lesson : Imports

When you copy and paste the following class into Eclipse:

//In this class, we will create a Random object called rand.

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

Random rand = new Random();
[COLOR="RoyalBlue"]}[/COLOR]
[COLOR="blue"]
}
[/COLOR]

You will see red squiggly lines below the word Random, indicating the presence of an error:

This error occurs because the "Random class" is not recognized by your current class, Randomization.

Earlier, I mentioned that the src folder contains all your Java code and that the JRE System Library contains importable code

that you can incorporate into your own projects.

Think of this Library as one containing "books" (Java classes). When you write an essay and you reference a book, you cite it.

Same with Java. When you use a "book" (Java classes) from the Library, you must state that you are using this "book."

This is accomplished by importing.

How do we import? It's pretty simple.

1. The easiest way is to press:

Ctrl + Shift + O

This will automatically import the class that the compiler thinks you are most likely to use.

2. Another way is to put your mouse over the words that show an error (in this case Random) and click on the quick fix that suggests importing

the Random class from Java.util.

Either way, when you successfully import, the error will disappear, and you will see the following above your class name:

import java.util.Random;

- indicating that your class makes use of this Random class.

The full class, after importing, will look like this.

[COLOR="SeaGreen"]//Here we import the java.util.Random class.[/COLOR]

[COLOR="Red"]import java.util.Random;
[/COLOR]
[COLOR="SeaGreen"]//In this class, we will create a Random object called rand.[/COLOR]
[COLOR="Blue"]class Randomization {[/COLOR]
[COLOR="RoyalBlue"]public static void main(String[] args) {[/COLOR]

[COLOR="SeaGreen"]	//This creates a new Random object.[/COLOR]
Random rand = new Random();

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

Well, now that we have a random object called rand, we can use it to randomize things.

The easiest method we will learn is:

.nextInt(int maxPlusOne)

What does this method do?

When you call this from a random object such as rand like so:

rand.nextInt(11);

It will generate an integer between 0 and 10.

Using this, you can simulate chance and probability.

How? We will go over probability and if statements tomorrow!

If you have any questions, contact me!

If you are learning from and enjoying the tutorials, please support Kilobolt Studios!

And Like us on Facebook to receive updates when a new lesson is posted!