(Updated November 3, 2026)
When designing a basic application program, certain details may be elusive. For example, how many variables will you need, or how many methods/functions will you need to create? There is no simple prescription that can handle all of the possibilities. The only thing you can do is start processing the information and formulate a plan of attack.
When planning the strategy, other details will surface and stare glaringly back. More may be present waiting to be discovered. The next example details when the project needs more to be complete. In the program, the following will be addressed:
- The set of variables.
- The approach to gathering information.
- Dealing with bad input.
- Providing a method of escape.
- Project morphing.
In Perimeter1, the program is simply calculating the perimeter of a quadrilateral using the length and width represented by l and w, respectively. The approach is simple, direct, and uses a GUI to gather data and report results. However, the program is fraught with issues.
- The programmer has chosen
showInputDialog()for user input. This always returns a String. - The calculation can only be done with doubles, so each
Stringwill need to be converted usingparseDouble()from theDoublewrapper class. - The
parseDouble()method will throw aNumberFormatExceptionon values that are not in floating point format. - There are two places where an exception can occur: where
parseDouble()is invoked.
import javax.swing.JOptionPane;
public class Perimeter1 {
public static void main(String[] args) {
double l, w, p;
String i, output;
i = JOptionPane.showInputDialog("Enter the length");
l = Double.parseDouble(i);
i = JOptionPane.showInputDialog("Enter the width");
w = Double.parseDouble(i);
p = 2 * w + 2 * l;
output = String.format("The length is %.2f%n", l)
+ String.format("The width is %.2f%n", w)
+ String.format("The perimiter is %.2f", p);
JOptionPane.showMessageDialog(null, output);
System.exit(0);
}
}
The Perimeter2 program attempts to address the issue of exceptions. This introduces new problems that must be handled.
- Wrapping the
parseDouble()calls with atryblock isolated the scope of assignment. This causes the compiler to believe that bothlandwmay not have been initialized due to their assignment within the scope of thetryblock. The variableslandwneed to be initialized prior to use. - The
catchblocks useshowMessageDialog()to display a message of failure. Still, the programmer is relegated to terminating the program, since continuing is not an option.
import javax.swing.JOptionPane;
public class Perimeter2 {
public static void main(String[] args) {
double l = 0.0, w = 0.0, p;
String i, output;
try {
i = JOptionPane.showInputDialog("Enter the length");
l = Double.parseDouble(i);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Naughty!");
System.exit(1);
}
try {
i = JOptionPane.showInputDialog("Enter the width");
w = Double.parseDouble(i);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Naughty!");
System.exit(1);
}
p = 2 * w + 2 * l;
output = String.format("The length is %.2f%n", l)
+ String.format("The width is %.2f%n", w)
+ String.format("The perimiter is %.2f", p);
JOptionPane.showMessageDialog(null, output);
System.exit(0);
}
}
Perimeter3 attempts to address the problems presented in Perimeter2 and does a relatively good job in doing so. Of course, it introduces new issues that must be handled.
- Wrapping the
try/catchblocks inwhileloops can help to promote a self-adjusting, self-healing solution. It also requires adding abooleanto track when the input succeeds. - The
okvariable needs to be reset before starting the second loop for the width, as this would now be true from the previous loop, causing the program to bypass the second loop altogether. - Those paying close attention to this process are now considering that the code has repeated since we wrote
Perimeter1and has escalated to the point where a method is a better fit to eliminate repetition and reduce the number of variables.
import javax.swing.JOptionPane;
public class Perimeter3 {
public static void main(String[] args) {
double l = 0.0, w = 0.0, p;
String i, output;
boolean ok = false;
while (!ok)
try {
i = JOptionPane.showInputDialog("Enter the length");
l = Double.parseDouble(i);
ok = true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Naughty! Try again.");
}
ok = false;
while (!ok)
try {
i = JOptionPane.showInputDialog("Enter the width");
w = Double.parseDouble(i);
ok = true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Naughty! Try again");
}
p = 2 * w + 2 * l;
output = String.format("The length is %.2f%n", l)
+ String.format("The width is %.2f%n", w)
+ String.format("The perimiter is %.2f", p);
JOptionPane.showMessageDialog(null, output);
System.exit(0);
}
}
Perimeter4 is a complete solution that addresses all issues discovered throughout the process.
- The
getValue()method eliminates the need for a boolean and simply loops forever. - The
parseDouble()call is the object of thereturnstatement eliminating additional variables. - The user prompt is passed as an argument to
getValue(). - The
main()method is smaller, simpler, and no longer requires the initialization oflandw.
import javax.swing.JOptionPane;
public class Perimeter4 {
public static double getValue(String p) {
String i;
while (true)
try {
i = JOptionPane.showInputDialog(p);
return Double.parseDouble(i);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Naughty! Try again.");
}
}
public static void main(String[] args) {
String output;
double l, w, p;
l = getValue("Enter the length");
w = getValue("Enter the width");
p = 2 * w + 2 * l;
output = String.format("The length is %.2f%n", l)
+ String.format("The width is %.2f%n", w)
+ String.format("The perimiter is %.2f", p);
JOptionPane.showMessageDialog(null, output);
System.exit(0);
}
}
At this point you may have many questions. This is not a perfect solution – it’s a solution. Some programmers may have iterated only once, while others possibly 10 times. The points being made here are:
- Take things slow as you assess the logic needed to arrive at the solution.
- Entering into certain designs can be achieved after we’ve achieved a sense of understanding of the problem and solution sets.
- Don’t bite off more than you can handle in a single method. Seasoned programmers will be able to jump into solutions alluded to in the previous bullet. New programmers need to spend time with the problem to understand the scope and nuance of a proposed solution.
- You can always rush headlong into the solution. You will definitely learn from mistakes made along the way. This write-up is not intended to discourage jumping into the deep end. Rather, we want to encourage sitting with the problem and understanding it completely as you work through the solution.