Function encapsulation in java

Source: Internet
Author: User

[Java]
// Triangle class, used to encapsulate attributes and functions of a Triangle.
Package qing;
 
Public class Triangle {
Double sideA, sideB, sideC, area, length;
Boolean boo;
Triangle ()
{
This. sideA = 3;
This. sideB = 4;
This. sideC = 5;
}
Public boolean Trangle (double a, double B, double c)
{
This. sideA =;
This. sideB = B;
This. sideC = c;
// Parameters a, B, and c are assigned to sideA, sideB, and sideC respectively.
If (this. sideA + this. sideB> this. sideC & this. sideA + this. sideC> this. sideB & this. sideB + this. sideC> this. sideA) // a, B, and c constitute the conditional expression of the triangle.
{
This. boo = true; // assign a value to boo.
}
Else
{
This. boo = false; // assign a value to boo.
}
Return boo;
}
Public double getLength ()
{
If (this. sideA + this. sideB> this. sideC & this. sideA + this. sideC> this. sideB & this. sideB + this. sideC> this. sideA)
{
This. length = this. sideA + this. sideB + this. sideC;
}
Else
{
System. out. println ("not a triangle, perimeter cannot be calculated ");
This. length = 0;
}
Return this. length;
}
Public double getArea ()
{
If (this. sideA + this. sideB> this. sideC & this. sideA + this. sideC> this. sideB & this. sideB + this. sideC> this. sideA)
{
Double p = (sideA + sideB + sideC)/2.0;
Area = Math. sqrt (p * (p-sideA) * (p-sideB) * (p-sideC ));
Return area;
}
Else
{
System. out. println ("not a triangle, cannot calculate area ");
Return 0;
}
}
Public void setABC (double a, double B, double c)
{
This. sideA =;
This. sideB = B;
This. sideC = c; // parameters a, B, and c are assigned to sideA, sideB, and sideC respectively.
If (this. sideA + this. sideB> this. sideC & this. sideA + this. sideC> this. sideB & this. sideB + this. sideC> this. sideA) // a, B, and c constitute the conditional expression of the triangle.
{
This. boo = true; // assign a value to boo.
}
Else
{
This. boo = false; // assign a value to boo.
}
}
 
}

 

[Java]
// Lader class, used to encapsulate Lader attributes and functions.
 
Package qing;
 
Public class Lader {
Double above, bottom, height, area;
Lader (double a, double B, double h)
{
This. above =;
This. bottom = B;
This. height = h; // method body. Assign the parameters a, B, and c to the values above, bottom, and height respectively.
}
Public double getArea ()
{
This. area = (this. above + this. bottom) * this. height/2; // method body, which must be calculated and returned by area
Return this. area;
}
 
}

 

[Java]
// Circle class, used to encapsulate attributes and functions of Circle.
 
Package qing;
 
Public class Circle {
Double radius, area;
Circle (double r)
{
This. radius = r; // method body
}
Public double getArea () // code of the getArea method body
{
This. area = Math. PI * this. radius * this. radius; // method body, which must be calculated and returned by area
Return this. area;
}
Public double getLength ()
{
Double length;
Length = 2 * Math. PI * this. radius; // The length is calculated and returned.
Return length;
}
Void setRadius (double newRadius)
{
Radius = newRadius;
}
Double getRadius ()
{
Return radius;
}
 
}

 

[Java]
// Test class, used to Test whether the functions of each class can be implemented.
Package qing;
 
Public class Test {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
 
Double length, area;
Circle circle = null;
Triangle trangle;
Lader lader;
Circle = new Circle (3); // create an object circle
Trangle = new Triangle (); // creates the trangle object.
Lader = new Lader (3, 4, 5); // create an object lader
Length = circle. getLength (); // the circumference of the method called by circle is returned and assigned to length.
System. out. println ("circle perimeter:" + length );
Area = circle. getArea (); // The area returned by the method called by circle and assigned to area
System. out. println ("area of the circle:" + area );
Length = trangle. getLength (); // call the trangle method to return the perimeter and assign it to length.
System. out. println ("Triangle perimeter:" + length );
Area = trangle. getArea (); // call the trangle method to return the area and assign it to the area
System. out. println ("Triangle area:" + area );
Area = lader. getArea (); // lader calls the method to return the area and assigns it to area
System. out. println ("Trapezoid area:" + area );
Trangle. setABC (12, 34, 1); // set three edges in the trangle call method. You must change the three edges to 12, 34, and 1.
Area = trangle. getArea (); // call the trangle method to return the area and assign it to the area
System. out. println ("Triangle area:" + area );
Length = trangle. getLength (); // call the trangle method to return the perimeter and assign it to length.
System. out. println ("Triangle perimeter:" + length );
}
 
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.