Search⌘ K
AI Features

A Silent Computational Error

Explore how silent computational errors occur in Java class implementations, especially in numerical calculations. Learn to methodically identify mistakes like argument misplacement in methods and improve debugging skills to ensure accurate program results.

Silent errors

Some programs will give you incorrect results, but will not warn you with an error message. In our first Debugging Interlude, we named this kind of error a silent error. It is an error in the logic of our program. Such errors can be extremely dangerous.

Programs that perform numerical computations are susceptible to silent errors. Here is an example.

A problem solved: Computing a cylinder’s volume

Imagine that your grandmother has asked you to help her make some candles. She has collected some cardboard mailing tubes to serve as molds. How much wax will she need? To find out, we can compute the volume of each cylindrical tube.

We begin by writing some pseudocode to describe what we need to do. It consists of three straightforward steps:

  • Prompt for and read the cylinder’s height and radius.

  • Compute the volume.

  • Display the height, radius, and volume.

Since cylinders are the focus of this problem, let’s define a class Cylinder to represent the various cylindrical mailing tubes.

The class Cylinder

A cylinder has a radius and a height, so these components can be the data fields of our class. In addition to creating a constructor, we can define accessor methods for the radius and height, as well as one for the volume.

We need the formula for the volume of a cylinder. That volume is simply the area of the cylinder’s circular cross-section multiplied by the cylinder’s height. The cross-section area of a cylinder is the ...