Java Number Exploration Lab

Jeremy Grifski - Jun 4 '19 - - Dev Community

To kick off the Renegade Curriculum series, I wanted to start with a lab that I’ve used a lot in the past to introduce a programming language. In particular, I want to introduce Java through a number exploration lab.

Overview

In this lab, students will be exploring various types of numbers in Java through all of the basic mathematical operators. The goal is to observe the behavior of the various operators and attempt to make some conclusions about their behavior. In the following subsections, we’ll cover exactly who could benefit from the lab, what types of tools are needed to complete the lab, and how to conduct the lab.

Target Audience

Ideally, the Java number exploration lab should target absolute beginners. However, I could see where this type of activity might be useful to someone with no experience in Java-like languages.

In terms of background knowledge, students should be comfortable with basic math operations like addition, subtraction, multiplication, and division. Students should also be comfortable with relational math operations like less than, greater than, and equal to.

Tools

In order to complete this lab, students should obtain a copy of DrJava. I recommend DrJava because it allows us to interact with Java like it’s an interpreted programming language. In other words, we can experiment with code snippets without creating files.

For those that would prefer not to use DrJava, they can use jshell which is included in the Java JDK since version 9. At the time of writing, Java 12 was the latest version of the JDK.

Instructions

For this type of lab, I recommend giving the students about an hour to work through as many exercises as they can. In addition, I’m a bit partial to pair programming, so letting students work in pairs can be fun. That also means less grading for the instructor.

Periodically, students should be asked to report their findings to their instructor. I recommend every couple of exercises, but it depends on the strength of the class. Some groups will need more help than others, so it’s a good idea to pace around the room during the lab.

In this particular lab, students will not really be writing their own code. Instead, they’ll be experimenting with code snippets in the interactions pane of Dr. Java. As a result, each exercise comes with a handful of questions the students will be expected to answer. Feel free to collect their answers after the lab.

Ultimately, the lab should be a low stress activity where students can feel comfortable interacting with the instructor. The goal isn’t to teach all the material in the lab but rather to reinforce some of the lecture material. In addition, the lab may be useful as an introduction to a larger project.

Exercises

In this section, the students will be asked to complete a handful of exercises. At the end of each exercise, the student will be asked to answer some questions about the code that they’ve written.

Exercise 0: Java Primitive Types Primer

In Java, numbers are finite. In other words, they have some range due to their size in bits (the number of zeroes and ones that make them up). To determine the range of numbers we can support, we need to know the type of the number.

Luckily, there are only a handful of types of numbers in Java. For our purposes, Java defaults to two types in particular: int and double. The int type represents 32-bit signed integers (i.e. 4, 311, -7, etc.) while the double type represents 64-bit decimals (i.e. 3.14, -14.5, 0.0001, etc.). In the following exercises, we’ll get to see these two types of numbers in action.

Exercise 1: Java Arithmetic Operators 1

With Dr. Java open, navigate to the interactions pane and enter the following code snippets one line at a time:

2 + 4
2 + 4.0
3 + -2
3 + -2.0
Enter fullscreen mode Exit fullscreen mode

If it’s not clear what these lines of code are doing, feel free to try some of your own examples. Otherwise, answer the following questions:

  • What does the + operator do?
  • How is the result of 2 + 4 different from 2 + 4.0?
  • What sort of rule could you write to describe your previous answer?

Exercise 2: Java Arithmetic Operators 2

Once again, enter the following code snippets one line at a time:

12 - 7
12 - 7.0
5 - -6
5 - -6.0
Enter fullscreen mode Exit fullscreen mode

As always, if you need to try more examples to figure out what the - operator does, feel free. Otherwise, answer the following questions:

  • What does the - operator do?
  • How is the result of 12 - 7 different from 12 - 7.0?
  • Does your rule from the previous exercise still work?

Exercise 3: Java Arithmetic Operators 3

With two operators out of the way, let’s try another:

3 * 3
3 * 3.0
-4 * -1
-4 * -1.0
Enter fullscreen mode Exit fullscreen mode

As always, feel free to try a few more examples. Otherwise, answer the following questions:

  • What does the * operator do?
  • Does your rule from the previous exercises still work?

Exercise 4: Java Arithmetic Operators 4

At this point, things should start to get a little interesting. Enter the following code snippets one line at a time:

5 / 2
5 / 2.0
5.0 / 2.0
5.0 / 2
-5 / 2
Enter fullscreen mode Exit fullscreen mode

Since the / operator is so different from the previous operators, it’s important to explore many combinations of values. When you’re ready, answer the following questions:

  • What does the / operator do?
  • How is the result of 5 / 2 different from 5 / 2.0?
  • Does your rule from the previous exercises still hold?
  • What sort of rule could you write to describe what is happening with 5 / 2 vs. -5 / 2?

Exercise 5: Java Arithmetic Operators 5

At long last, here’s the final arithmetic operator:

5 % 3
6 % 3
7 % 3
8 % 3
4 % 2
5 % 2
Enter fullscreen mode Exit fullscreen mode

Until now, each operator should have been more or less obvious: addition, subtraction, multiplication, and division. Naturally, the % operator is a bit different. As a hint, it’s related to division, but you’re likely most familiar with it in the context of long division.

Once you have a good idea of what the operator does, answer the following questions:

  • What does the % operator do?
  • How might the % operator be useful (i.e. 5 % 2 vs. 4 % 2)?

Exercise 6: Java Arithmetic Operators 6

Once again, enter the following code snippets one line at a time:

3 * 7
4 + 5 * 6
(4 + 5) * 6
2 * 3 - 5
Enter fullscreen mode Exit fullscreen mode

Feel free to try a few examples of your own. Otherwise, answer the following questions:

  • What rule does Java use to evaluate these expressions? In other words, how does Java know which operation to perform first?

Exercise 7: Java Relational Operators

With the arithmetic operators out of the way, time to introduce the relational operators:

2 > 5
3 >= 2
4 == 10
8 <= 2
1 < 2
5 = 6
Enter fullscreen mode Exit fullscreen mode

As usual, feel free to try more examples. Otherwise, answer the following questions:

  • What do each of the following operators do: >, >=, ==, <=, <?
  • When might these operators be useful?
  • Why does 5 = 6 fail?

Exercise 8: Java Number Limits

With the operators out of the way, let’s take a look at just how big (and small) the int type is (don’t worry too much about the syntax):

Integer.MAX_VALUE
Integer.MAX_VALUE + 1
Integer.MIN_VALUE
Integer.MIN_VALUE - 1
Enter fullscreen mode Exit fullscreen mode

Here, we’ve checked out the limits of an integer and attempted to go just beyond them. If that makes sense, answer the following questions:

  • What is the largest possible integer? What happens when we add one to that value?
  • What is the smallest possible integer? What happens when we subtract one from that value?

Exercise 9: Java Variables

At this point, all of the basic operators have been explored, and we’ve even tested the limits of integers. Let’s use that knowledge to create some variables:

int x = 5
x = x + 1
int y = x + 5
double z = (x * y) / 2.0
Enter fullscreen mode Exit fullscreen mode

Unlike before, do not try any more examples before answering the following questions:

  • What does each variable store at the end of executing all three lines?
  • How is z different from x and y?
  • What is the meaning of the = operator?
  • How does x = x + 1 work?

Wrapping Up!

After completing this lab, students should feel much more comfortable with the various arithmetic and relational operators. If they got far enough, they might even have gotten some exposure to variables. In addition, they should have a better understanding of integer and floating point arithmetic.

After a lab like this, it would be safe to introduce variables, primitive types, and even custom types (classes). With Java being object-oriented, I recommend introducing classes as soon as possible. Otherwise, they’ll have to learn Java through some bizarre template, and objects will be much more difficult to introduce later.

At any rate, if this lab seemed interesting to you, feel free to use it for whatever you need. I plan to make more of this type of content, so expect to see more advanced labs soon. If you’d like future material to hit your inbox, become a member. I appreciate the support.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .