Numbers and Math

Every programming language has some kind of way of doing numbers and math. Don't worry, programmers lie frequently about being math geniuses when they really aren't. If they were math geniuses, they would be doing math, not writing ads and social network games to steal people's money.

This exercise has lots of math symbols so let's name them right away so you know what they're called. As you type this one in, say the names. When saying them feels boring you can stop saying them. Here are the names:

Notice how the operations are missing? After you type in the code for this exercise you are to go back and figure out what each of these does and complete the table. For example, + does addition.

public class NumbersAndMath
{
	public static void main( String[] args )
	{
		System.out.println( "I will now count my chickens:" );

		System.out.println( "Hens " + ( 25 + 30 / 6 ) );
		System.out.println( "Roosters " + ( 100 - 25 * 3 % 4 ) );

		System.out.println( "Now I will count the eggs:" );

		System.out.println( 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 );

		System.out.println( "Is it true that 3 + 2 < 5 - 7?" );

		System.out.println( 3 + 2 < 5 - 7 );

		System.out.println( "What is 3 + 2? " + ( 3 + 2 ) );
		System.out.println( "What is 5 - 7? " + ( 5 - 7 ) );

		System.out.println( "Oh, that's why it's false." );

		System.out.println( "How about some more." );

		System.out.println( "Is it greater? " + ( 5 > -2 ) );
		System.out.println( "Is it greater or equal? " + ( 5 >= -2 ) );
		System.out.println( "Is it less or equal? " + ( 5 <= -2 ) );
	}
}

What You Should See

I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
false
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's false.
How about some more.
Is it greater? true
Is it greater or equal? true
Is it less or equal? false

What You Should Do on Your Own

Assignments turned in without these things will not receive any points.

  1. Above each line, use two slashes // to write a comment to yourself explaining what the line does.
  2. Notice the math seems "wrong"? There are no fractions, only whole numbers. Find out why by researching what a "floating point" number is.
  3. Rewrite NumbersAndMath.java to use floating point numbers so it's more accurate (hint: 20.0 is floating point).

Frequently-Asked Questions

Why is the % character called "modulus" instead of "percent"?
Mostly that's just how the designers of Java chose to use that symbol. In normal writing you are correct to read it as a "percent", as in "100%" is "one hundred percent". In programming this calculation is typically done with simple division and the / operator. The % modulus is a different operation that just happens to use the % symbol.
How does modulus (%) work?
Another way to say it is, "X divided by Y with J remaining." As in, "100 divided by 16 with 4 remaining." The result of % is the J part, or the remainder part.
What is the order of operations?
In the United States we use an acronym called PEMDAS which stands for Parentheses Exponents Multiplication Division Addition Subtraction. That's the order Java follows as well.
Why does / (divide) round down?
It's not really rounding down, it's just dropping the fractional part after the decimal. Try doing 7.0 / 4.0 and compare it to 7 / 4 and you'll see the difference. The first way uses "floating point division" and the second way uses "integer division".


Copyright © 2010 Zed A. Shaw. Used by permission.

(The original Python version of this assignment is part of Zed Shaw's excellent Learn Python the Hard Way course and was translated to/reinterpreted for Java by Graham Mitchell.)