Welcome back! This is the third tutorial in a series of Java programming tutorials. If you haven’t checked out the first two parts, be sure to check it out here.

In this tutorial we will look at arithmetic operators, constants, math class, data types and we will write two more Java programs.

Arithmetic Operators: Java arithmetic operators work just like math operators. You can use + for addition, – for subtraction, * for multiplication, / for division and % for modulus (division that gives you the remainder). There are also other operators that can be used to compare numbers or other expressions.

The following will show you the precedence of operators.

Unary Operators (!, +, -)

Multiplication, Division, Modulus (*, /, %)

Addition, Subtraction (+, -)

Less than, Less than or equal, Greater than, Greater than or equal ()

Equal, Not equal (==, !=)

And (&&)

Or (||)

Assignment Operator (=)

Contstant: Constants can be used when you are creating a program with multiple methods and you want to use the variable in more than one method. Constants will be written in all caps and will go at the top of the program. In order to make it a constant we will use the word “final” when declaring it.

Math Class: The math class is used to perform certain math functions such as absolute values, max, min square root and random. To use the math method, you will need to import it at the top of a program, by typing an import statement: import static java.lang.Math.*; Absolute Values can be calculated with a statement like this: var = abs(var);. Max or min can be used to compare two or more numbers by using a statement such as: int var = min(1,2); This would return the min value of 1. Square root can be used to calculate the square root of a number. This must be a double variable type and will return a double number by using a statement like: double var = sqrt (25);. This would return 5, the square root of 25. Random can be used to generate a random number. This function will return a number greater than or equal to 0.0 and less than 1.0 by using a statement like: double var = random().

Data Types: We talked about data types in the last tutorial, so if you haven’t already read that be sure to check it out first. We will look at two data types in this tutorial. Boolean, which can be used to test and store a true or false value. Float, this can be used to store information with a decimal in it. Remember when looking at mixed expressions that an int + float = float.

Now onto our programs! Into this tutorial we will write two programs. The first program will compute the date of Easter Sunday and the second program will use the Math class.

Start by entering your comments, and opening your program like we did in the last program.

//Name //Reason public class Tutorial2 { public static void main(String[] args) {

Now we will set the variable y equal to the year, divide y by 19 and call the remainder variable a. Then we will divide y by 100 to get a quotient variable b and a remainder variable c. Let’s take a look at what that would look like: int y = 2009; int a = y % 19; int b = y/100; int c = y % 100; Next we will divide b by 4 to get a quotient variable d and a remainder variable e. We will divide 8 * b + 13 by 25 to get a quotient variable g and ignore the remainder. We will also divide 19 * a + b – d – g + 15 by 30 to get a remainder variable h and ignore the quotient. Wow, sounds like alot but lets look at what it will look like. int d = b / 4; int e = b % 4; int g = (8 * b + 13) / 25; int h = (19 * a + b – d – g + 15) % 30;

Sounds like alot but remember you have to think about the order of operations and what each sentence says. It seems like a lot to figure out what day easter is on but a few more steps and we will be done. Divide c by 4 to get a quotient j and a remainder k. Then divide a + 11 * h by 319 to get a quotient m, ignoring the remainder. Then divide 2 * e + 2 * j – k – h + m + 32 by 7 to get a remainder r and ignore the quotient. Divide h – m + r + 90 by 25 to get a quotient n and ignore the remainder. Finally divide h – m + r + n + 19 by 32 to get a remainder p, ignoring the quotient. Let’s take a look at what this will look like:

int j = c /4; int k = c % 4; int m = (a + 11 * h) / 319; int r = (2 * e + 2 * j – k – h + m + 32) % 7; int n = (h – m + r + 90) 25; int p = (h – m + r + n + 19) % 32;

Ok, now all we need to do is print out the information and see what dat Easter falls on. Easter falls on day p of month n. So we will set our program to print both p and n to the screen: System.out.println(“n = “+n); System.out.println(“p = “+p); } }

Now our program should tell us what day Easter falls on, all you have to do is change the y variable. Let’s look at the whole program and then compile and run it so that we can see how it works. And now we will run our program. So in 2009, Easter was on April 12.

For our second program we will take a look at the math class. I’ll give you the instructions first and you can try to write the java program and then I’ll show you what mine looks like.

1. Declare a variable of type int called a. Initialize this variable to have a value of -6. Take the absolute value of a and store it in an int valled b. Print the value of b to the screen.

2. Declare a variable of type double called c. Initialize this variable to have a value of 25. Take the square root of c and store it in a double called d. Print the value of d to the screen.

3. Use the max method to find the maximum value between b and d. Store this value in a double called e.

4. Use the min method to find the minimum value between b and d. Store this value as a double called f.

Ok, give the program a try and don’t forget to import the math class into your program (Hint: It goes before your class). And now we will run our program.

Now that we’ve written a few java programs try and do one for yourself! Let me know what you write and good luck until next time. And don’t forget that you can see the completed program and what it looks like after it’s run, here.