ITEC 120
Evaluating Java Expressions

Objectives

After successfully completing this lab you will understand how division and modulus operators in Java work. You will also learn how to write a program to test your prediction.

Assignment

First, download and print this worksheet: EvaluatingExpressions.pdf
(if you haven't already been given a copy)

Evaluate each expression the way java would, and write your answer on the paper. You are testing yourself to see if you understand how division ( / ) and modulus (%) operators work in Java.

Then, you will check your answers by writing a program to see what Java really does. It's important to do the work on paper first, then write the program. If you missed any of the answers, it's important to figure out why and how you misunderstood.

Writing the program to test your answers

Take a look at the first problem: 7/3

To see how Java evaluates that, you could write and run this program:

And you would see the output:

2

Is that what you expected? If not, you need to figure out why, and what is going on.

There are 24 more expressions to add to this program. If you just print the answer to each expression, soon your output will be very confusing. So it would be better to label your output, like so:

When you run this program, you'll see:

7/3 is 2

In a similar fashion, print out results for all the expressions on the worksheet. Compile and run often and check your results as you go. Don't type for 30 minutes and then try to compile. If you made a mistake, you've probably made it dozens of times! Type for a couple minutes, then compile and run and see what you've got.

How did you do?

If you made a lot of mistakes on the worksheet, you might want to try the exercise again to see if you've got it. You can make up a sheet of new problems to test yourself, and then code them to see if you are right! Don't wait for the first exam to see if you've mastered division and modulus! Master those concepts now.

In this course, you can always code something to see if it works the way you think. Good programmers often write test programs to understand exactly how something works.

 

Some notes on / and %

The division operator in Java is /

When you divide a number by another number in Java, the type of each number will affect what kind of division happens. If either one of the numbers (operands) or both are a floating point number (type double in Java), then "regular" division happens, as if you typed the expression into a calculator, and the result is a double.

But, if both operands are integers (type int in Java), then a different kind of division happens. Integer division happens. The result is an integer, and it is the number of times the bottom number "goes into" the top number.

The modulus operator in Java is %

Chances are good you are less familiar with the modulus operator, also called remainder. If both operands are integers, the result is an integer that is what is left over after the top number is divided by the bottom number.

This operator has some very useful applications in computer programming!

 

Submit Your Assignment

Submit your java program to the Lab Submission Folder on D2L.