Inf1 OP : Lab Sheet Week 2 Q7 - Safer Fixed Divider
Overview

Programming errors in Java fall into 3 major categories:

  • Compile-time errors
  • Runtime errors
  • Logic errors

We will dedicate more time during lectures and tutorials to error types, bug hunting and error prevention. For now, here is a quick overview.

Compile-time errors are those that prevent the program from compiling and cause javac to complain. Examples of these are misspelling variable names or forgetting semicolons. Your program will not compile and you will not be able to run it.

By contrast, runtime errors are produced when the program is running. An example is attempting to divide by zero; it is difficult/impossible for the compiler to tell in advance that this will happen, especially if the input comes from the user, and when it does happen it will cause the program to stop running.

Logic errors happen when the program compiles and executes without crashing but does not do what it is supposed to do. For example, you want two numbers to be added but instead the program subtracts them. Those errors can be very hard to fix.

In this question, we will start off by implementing a program that divides one number by another, where the two numbers are given on the command-line. Next we will improve our program to ensure that the denominator (i.e. the divisor of a fraction) is not zero.

Fixed Divider

Start off by creating a program FixedDivider with a main() method which reads two floating point arguments from the command-line into double variables, called numerator and denominator. Your program should print out the value of numerator/denominator. This exercise is similar to Adder and Multiplier in the first week. For example:

:java FixedDivider 4.0 2.0
2.0000000000

:java FixedDivider 5.0 2.0
2.50000000000

:java FixedDivider 2.0 4.0
0.50000000000

:java FixedDivider 10.0 3.0
3.33333333333

An automated test has been created for this exercise: FixedDividerTest.java.

Breaking the Program

Now let’s try to divide by 0.0:

:java FixedDivider 10.0 0.0
Infinity
:

What has happened is that when we take the quotient 10.0/0.0, Java returns Infinity and this is the value that is printed. However, this is not a very user-friendly error, so let’s change our program to check if the divisor is zero and print something a little more informative to the user. To make decisions on what a program performs we often use an if-then-else statement. This is a simple instruction that compares a true/false (boolean) expression, and performs different actions based upon the result, as shown below:

if ( boolean expression ) {
        // Do something if the expression is true
} else {
        // Do something if the expression is false
}

As with Haskell, the boolean expression that we are testing often involves comparing two values. In our current example, we might check whether the expression myNumber == 0 is true. Here are some more examples of boolean expressions:

a == b // The value of variable a is equal to that of variable b
a != b // The value of variable a is not equal to that of variable b
a < 0  // The value of variable a is less than zero
b > 5  // The value of variable b is greater than five
a <= 0 // The value of variable a is less than or equal to zero
b >= 5 // You should be getting the idea by now..
Safer Divider

Your task now is to write a class SaferDivider that will show the following behaviour:

: javac SaferDivider.java
: java Divider 10.0 2.0
5.0
: java SaferDivider 10.0 0.0
I can't divide by zero!

An automated test has been created for this exercise: SaferDividerTest.java.