the huntrods zone - teaching

General Programming Hints

On choosing variable types, or method return types:

1. Any method with "get" in the name should return something.

2. What type of number is it (i.e. interest rate, temperature)? What are some typical values for that type of number?

3. Here's a tough bit - some numbers (like interest rates) can be in more than one format (like % or a decimal fraction; 0.055 is the same as 5.5%) Does the problem statement give any hints as to which might be preferred? If not (i.e. if you control all aspects of the program), then simply choose one or the other and then be consistent.

4. Further hint on 3. In the absence of any other information, choose the typical human preference. For example, humans like to express interest rates as percents rather than decimals - so the bank says the current prime rate is 3.5% (for example), not 0.033. If there is no obvious reason to use one format over another, choose the "human readable" format.

On drawing a curve like sin(x):

1. The microsoft calculator has a scientific mode (or use a calculator with a "SIN" button).

2. Take a piece of graph paper, and draw the axes from the text on it (0,0 is in the center; the vertical axis runs from 0 to 1 upwards and 0 to -1 downwards; the horizontal axis runs from 0 to 2PI to the right and 0 to -2PI to the left). Starting with X = (-2 * PI) (3.1415), use the calculator to calculate the value of SIN(X). Now increase X by about 0.1 (or 0.2 for faster drawing). Recalculate SIN(X) with the new value. Do this until X = (+2 * PI) and you will have a graph that should be exactly what you are looking for.

3. That is a design. Transfer the "pencil and paper" motions into Java, and you have your program. The only "sticky" points are that the screen on a PC goes from 0 to XXX in the horizontal and vertical direction. Since you are building a panel application, you get to set the size (most people use 400X400), so the drawing screen goes from 0,0 (top left hand corner) to 400,400 (bottom right hand corner). Transfer these numbers onto your paper drawing to guide you. This will give you an idea of the "coordinate transform" (that's the fancy wording for it) required. It simply means that the middle of your paper drawing (point 0, 0 on your axes) represents a point (200, 200) in your screen coordinates. The X and Y axes are easy. Draw them first. Then work on transforming the X and Y values of the sin curve.

copyright © Richard S. Huntrods
all rights reserved

last updated: