[Java]
/* (Start of program header annotation) </p> <p> * copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* Author: Li Zhaoqing
* Completion date: January 1, September 24, 2012
* Input description:
* Requirement Analysis: encapsulate a type of Triangle object Triangle, which has three edge attributes and has the ability to initialize a Triangle,
Modify the side length function, determine whether the three sides can constitute a triangle, calculate the perimeter function, and calculate the area function.
* End the comment in the program Header
*/
Import javax. swing. JOptionPane;
Class Triangle
{
Private double a, B, c; // defines the variables used to store the length, width, and height of the triangle, respectively.
Triangle () // Triangle class constructor, which initializes the three sides of the Triangle and assigns a cut value of 0;
{
A = 0.0;
B = 0.0;
C = 0.0;
}
Public void setLeng (double a, double B, double c) // defines the "setLeng" behavior, which is used to modify three edges.
{
This. a = a; // this keyword calls attribute a of this object and re-assigns a value.
This. B = B; // this keyword calls attribute B of this object and re-assigns a value.
This. c = c; // this keyword calls the attribute c of this object and re-assigns a value.
}
Public boolean judge (Triangle t) // defines the "judge" action to determine whether a Triangle is used.
{
If (a> 0) & (B> 0) & (c> 0) // The premise is that the length of each side must be greater than zero, that is, the length of each side must be meaningful every day.
{
If (t. a + t. b)> t. c) & (t. a + t. c)> t. b) & (t. B + t. c)> t. a) // conditions for determining the formation of a triangle
{
Return true;
}
}
Return false;
}
Public double getGirth (Triangle t) // defines the "getGirth" behavior to obtain the Triangle Area.
{
If (judge (t) = false)
{
System. out. println ("sorry, the three sides of the triangle you entered are invalid! ");
Return 0;
}
Double s = a + B + c;
Return s;
}
Public double getArea (Triangle t) // defines the "getArea" behavior to obtain the Triangle Area.
{
If (judge (t) = false)
{
System. out. println ("sorry, the three sides of the triangle you entered are invalid! ");
}
Double s = (a + B + c)/2;
Return Math. sqrt (s * (s-a) * (s-B) * (s-c ));
}
}
Public class Num {
/**
* @ Param args
*/
Public static void main (String [] args)
{
Triangle tri = new Triangle (); // create a Triangle object "tri" using the keyword "new ";
String str = JOptionPane. showInputDialog ("Enter the first side length of the triangle ");
Double a = Integer. parseInt (str );
String str1 = JOptionPane. showInputDialog ("enter the second side length of the triangle ");
Double B = Integer. parseInt (str1 );
String str2 = JOptionPane. showInputDialog ("enter the third side length of the triangle ");
Double c = Integer. parseInt (str2 );
System. out. println ("the three sides of the input triangle are:" + a + "" + B + "" + c );
Tri. setLeng (a, B, c );
System. out. println (tri. judge (tri ));
System. out. println ("Area:" + tri. getArea (tri ));
System. out. println ("perimeter:" + tri. getGirth (tri ));
}
}