Java-encapsulate three triangular trapezoid circles and test the three types of objects with one program execution entry

Source: Internet
Author: User
Public class diliuzhou_1 {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub} class trangle {double sidea, sideb, sidec, area, length; Boolean boo; Public trangle (double A, double B, double C) {sidea = A; // parameter A, B, C is assigned to sidea, sideb, sidec sideb = B; sidec = C; if (a + B> C & A + C> B & B + C>) // condition expression {boo = true for A, B, and C triangle; // assign a value to boo.} Else {boo = false; // assign a value to boo.} Double getlength () {length = sidea + sideb + sidec; // method body. The value of length must be calculated and return length;} public double getarea () {If (BOO) {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) {sidea = A; sideb = B; sidec = C; // parameters A, B, and C are assigned to sidea, sideb, and side respectively. C If (a + B> C & A + C> B & B + C> A) // A, B, and C constitute the conditional expression of the triangle {boo = true; // assign a value to boo.} Else {boo = false; // assign a value to boo. }}} Class lader {double above, bottom, height, area; lader (double A, double B, double H) {above = A; // method body, set parameter, B and C are assigned to the values above, bottom, and height Bottom = B; Height = H;} double getarea () {area = (above + bottom) * Height/2.0f; // method body, which requires that return area;} class circle {double radius, area, length; Circle (double r) {radius = R; // method body} double getarea () {area = 3.14 * radius; // method body, which requires that return area;} double getlength () {length = 6.28 * radius; // code of the getarea method body. Return length is required to be calculated.} void setradius (double newradius) {radius = newradius;} double getradius () {return radius ;}}

Create a program entry

 

Public class main {public static void main (string ARGs []) {double length, area; circle = NULL; trangle; lader; trangle TT = new trangle (2, 5, 8); lader LL = new lader (3, 5, 8); Circle cc = new circle (3); // create an object circle length = cc. getlength (); // call the method to return the perimeter and assign it to length system. out. println ("circle perimeter:" + length); Area = cc. getarea (); // call the Circle Method to return the area and assign it to area system. out. println ("Area of the circle:" + Area); Length = TT. getlength (); // call the trangle method to return the perimeter and assign it to length system. out. println ("Triangle perimeter:" + length); Area = TT. getarea (); // call the trangle method to return the area and assign it to area system. out. println ("Triangle Area:" + area); Area = ll. getarea (); // lader calls the method to return the area and assigns it to area system. out. println ("Trapezoid area:" + area); TT. setabc (, 1); // set three edges in the trangle call method. You must change the three edges to, 34, and 1. Area = TT. getarea (); // call the trangle method to return the area and assign it to area system. out. println ("Area of the modified triangle:" + area); length = TT. getlength (); // call the trangle method to return the perimeter and assign it to length system. out. println ("the perimeter of the modified triangle:" + 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.