Programming Resources For Fun and Learning

# AreaApplet

 Description Area.java AreaApplet.java

Area.java

```/*
*  Filename:       Area.java
*  Created:        August 29, 2002, 12:35 PM
*  Author:         David Reid
*  Class:          JDE155H - Introduction to Computer Science I
*  Instructor:     Chuck Cusack
*
*  Description:    Area is a simple Java program that calculates areas of five shapes from
*                  input by the user.  It performs these actions without a GUI, but rather
*                  message boxes, because I only know how to use them at the moment.  Kudos
*					to Mike Kok and Dan Christensen for solving a simple problem that took me
*					two hours and a dinner break to figure out.  :)
*/

import javax.swing.JOptionPane;  //  Import the JOptionPane for message and input boxes.

public class Area {

public static void main(String args[]) {

try {

//  Declare variables.
Object msgValue;
String shape, msgResponse, strLength1, strLength2;
int intLength1, intLength2, loopAgain;

//  The beginning of the giant loop to recalculate another shape.
do {

//  Create shape options.
Object[] msgOptions = {"Circle", "Ellipse", "Triangle", "Square", "Rectangle"};
//  Input shape
msgValue = JOptionPane.showInputDialog(null, "Please select a shape below.", "Dave's Magical Area Finder > Menu", JOptionPane.QUESTION_MESSAGE, null, msgOptions, msgOptions[0]);
//  Convert shape to a string (so it can be compared easily)
shape = msgValue.toString();

//  Deduce which shape the user selected.
if (shape == "Circle") {
//  Input necessary values to calculate area.
strLength1 = JOptionPane.showInputDialog(null, "Enter the radius", "Area: Circle", JOptionPane.QUESTION_MESSAGE);
//  Convert string to integer so we can work math with it.
intLength1 = Integer.parseInt(strLength1);
//  Display area.
JOptionPane.showMessageDialog(null, "The area of a circle with a radius of "+intLength1+" is "+(intLength1*intLength1*Math.PI), "Dave's Magical Area Finder > Circle", JOptionPane.INFORMATION_MESSAGE);
}
else if (shape == "Ellipse") {
strLength1 = JOptionPane.showInputDialog(null, "Enter the first radius", "Dave's Magical Area Finder > Ellipse", JOptionPane.QUESTION_MESSAGE);
strLength2 = JOptionPane.showInputDialog(null, "Enter the second radius", "Dave's Magical Area Finder > Ellipse", JOptionPane.QUESTION_MESSAGE);
intLength1 = Integer.parseInt(strLength1);
intLength2 = Integer.parseInt(strLength2);
JOptionPane.showMessageDialog(null, "The area of an ellipse with radi of "+intLength1+" and "+intLength2+" is "+((intLength1+intLength2)/2*Math.PI), "Dave's Magical Area Finder > Ellipse", JOptionPane.INFORMATION_MESSAGE);
}
else if (shape == "Triangle") {
strLength1 = JOptionPane.showInputDialog(null, "Enter the base length", "Dave's Magical Area Finder > Triangle", JOptionPane.QUESTION_MESSAGE);
strLength2 = JOptionPane.showInputDialog(null, "Enter the height", "Dave's Magical Area Finder > Triangle", JOptionPane.QUESTION_MESSAGE);
intLength1 = Integer.parseInt(strLength1);
intLength2 = Integer.parseInt(strLength2);
JOptionPane.showMessageDialog(null, "The area of a triangle with a base length of "+intLength1+" and a height of "+intLength2+" is "+((intLength1*intLength2)/2), "Dave's Magical Area Finder > Triangle", JOptionPane.INFORMATION_MESSAGE);
}
else if (shape == "Square") {
strLength1 = JOptionPane.showInputDialog(null, "Enter the length of one side", "Dave's Magical Area Finder > Square", JOptionPane.QUESTION_MESSAGE);
intLength1 = Integer.parseInt(strLength1);
JOptionPane.showMessageDialog(null, "The area of a square with a side lenght of "+intLength1+" is "+(intLength1*intLength1),"Dave's Magical Area Finder > Square",JOptionPane.INFORMATION_MESSAGE);
}
else if (shape == "Rectangle") {
strLength1 = JOptionPane.showInputDialog(null, "Enter the first length", "Dave's Magical Area Finder > Rectangle", JOptionPane.QUESTION_MESSAGE);
strLength2 = JOptionPane.showInputDialog(null, "Enter the second length", "Dave's Magical Area Finder > Rectangle", JOptionPane.QUESTION_MESSAGE);
intLength1 = Integer.parseInt(strLength1);
intLength2 = Integer.parseInt(strLength2);
JOptionPane.showMessageDialog(null, "The area of a rectangle with side lengths of "+intLength1+" and "+intLength2+" is "+(intLength1*intLength2), "Dave's Magical Area Finder > Rectangle", JOptionPane.INFORMATION_MESSAGE);
}
else {
//  This is not necessary.  It was left over from when I had the user type in the shape.  It is now here just as a precautionary measure.
JOptionPane.showMessageDialog(null, "Shape not recognized", "Area: Error", JOptionPane.ERROR_MESSAGE);
}

//  Ask whether to recalculate another shape and loop.
loopAgain = JOptionPane.showConfirmDialog(null, "Thank you sir.  May I have another?", "Dave's Magical Area Finder > Again?", JOptionPane.YES_NO_OPTION);

}
//  Loop until the user clicks 'No' (1 = Yes, 2 = No).
while (loopAgain == 0);

//  Say goodbye and exit.
JOptionPane.showMessageDialog(null, "Thank you.  Please come again.\n\nI'll be back...", "Dave's Magical Area Finder", JOptionPane.PLAIN_MESSAGE);
System.exit(0);

}

catch(Exception e) {
JOptionPane.showMessageDialog(null, "The error "+e+" has been caught!\nThe program will now exit.\n\nWe're sorry for the inconvenience.", "Dave's Magical Area Finder > ERROR", JOptionPane.ERROR_MESSAGE);
}

finally {
System.exit(0);
}

}

}```