Triangle
/*
* Filename: "Triangle.java"
* Created by 1,000_naymes
* Purpose: to calculate the area of a square and circle based on input from user
*/
import javax.swing.JOptionPane;
public class Triangle
{
public static void main( String[] args )
{
/**************************************************************
declare and initialize variables
**************************************************************/
String openingMessage,
sideOneInputMessage,
sideOneString,
sideTwoInputMessage,
sideTwoString,
sideThreeInputMessage,
sideThreeString,
triangleMessage,
errorMessage,
outputMessage;
double sideOne, sideTwo, sideThree, expression1, expression2, expression3,
perimeter;
/**************************************************************
display opening message
**************************************************************/
openingMessage = "Welcome to 1KN's Triangle program, which will read three
numbers and compute the perimeter.";
JOptionPane.showMessageDialog( null, openingMessage );
/**************************************************************
input
**************************************************************/
sideOneInputMessage = "Please enter the length of one side of your triangle.";
sideOneString = JOptionPane.showInputDialog( sideOneInputMessage );
sideOne = Double.parseDouble( sideOneString ); //converting string to
double
sideTwoInputMessage = "Please enter the length of a second side of your
triangle.";
sideTwoString = JOptionPane.showInputDialog( sideTwoInputMessage );
sideTwo = Double.parseDouble( sideTwoString ); //converting string to
double
sideThreeInputMessage = "Please enter the length of the last side of your
triangle.";
sideThreeString = JOptionPane.showInputDialog( sideThreeInputMessage );
sideThree = Double.parseDouble( sideThreeString ); //converting string to
double
perimeter = ( sideOne + sideTwo + sideThree);
/**************************************************************
checking sides:
**************************************************************/
triangleMessage = "The perimeter of your triangle is " + perimeter + ".";
Page 1
Triangle
errorMessage = "Sorry, the data you entered is not valid; \nthe sum of two of
the sides you entered is too large.";
if( sideOne < sideTwo + sideThree &&
sideTwo < sideOne + sideThree &&
sideThree < sideOne + sideTwo )
JOptionPane.showMessageDialog( null, triangleMessage );
else
JOptionPane.showMessageDialog( null, errorMessage );
System.exit(0);
} //end main
} //end class
Page 2