INTRO TO JAVA (PART 2) -
WRITTEN BY: M.WORTHINGTON [12.08.01]
...well if you've checked out the first tutorial and everything went as planned, you made
your first program and played with it, than you are definetly bored with it and ready to
learn more. in this second installment to the java tutorials, you'll be learning about
number variables and some mathematical operations.
in our class FunWithNumbers we will be using two different variable types: int and double.
an int can be any integer (-1,0,1,2...) from -2147483648 (MIN_VALUE) to 2147483648
(MAX_VALUE). and int does not support decimals; that is where the variable type double
comes in. examples of what a double variable could be .5, 3.14, 2., 19.8888, and so on.
in this program, we will also be using some simple math calculations. they are as follows:
+ for addition (while the plus sign is used for math,
it is also used in printing out multiple things in a line or adding on to a string. so
when you think of +, think 'plus' or 'and'.
- for subtraction
* for multiplication
/ for division
% to get the remainder of one number dividing into another
to create, use, and manipulate numbers with variable types such as int and double.
public class FunWithNumbers
public static void main(String args)
int a = 2;
int b = 5;
System.out.println("a + b = " + (a+b)); // Line 1
System.out.println("a - b = " + (a-b)); // Line 2
System.out.println("a * b = " + (a*b)); // Line 3
d = (b/a);
System.out.println("d = " + d); // Line 4
d = (5./2.);
System.out.println("d = " + d); // Line 5
e = (b/a);
System.out.println("e = " + e); //Line 6
System.out.println("b / a = " + (b/a) + " b % a =
" + (b%a)); // Line 7
System.out.println("a / b = " + (a/b) + " a % b =
" + (a%b)); // Line 8
ABOUT THE PROGRAM
on each line of coding that is going to print something out is a comment that has an
assigned number line. this is just so referring to each line will be easier to understand.
be sure to compile the program and then run it. refer back to the first tutorial for
the following is output
from each line(minus the line reference) as you should have when you run the program:
Line 1: a + b = 7
Line 2: a - b = -3
Line 3: a * b = 10
Line 4: d = 2.0
Line 5: d = 2.5
Line 6: e = 2
Line 7: b / a = 2 b % a = 1
Line 8: a / b = 0 a % b = 2
this is just basic mathematics. the only real parts to explain would be the different
outputs of the variable d. on line 4 it prints out 2.0 rather than 2.5, which is on line
5. the reason for this is because in the first case, d equals the division of two
integers. while, on line 5, d equals 5. divided by 2. which both have decimal points and
therefore will produce what comes up after the decimal place.
if you're still having
trouble with the program and understanding it, your best bet is to just mess around with
it. change up the variables, variable types, values, and calculations; before running the
program, try to predict what is going to be printed out.
any other questions, hit up the message
boards. if you chat there you can easily go ahead of tutorials and learn more
please let me know what you think of this tutorial by going to the message boards or the contact page to submit your
comments. thank you.