Calculate the perimeter (interface and polymorphism) of various graphs

Source: Internet
Author: User

Calculate the perimeter (interface and polymorphism) of various graphs
Time limit:1000 Ms Memory limit:65536 KiB
Submit statistic
Problem Description
Defines the interface shape, defining the method length () for the perimeter.
Define an abstract method that implements the interface shape of the following class:
(1) Triangular class triangle (2) Rectangular class Rectangle (3) Circular class circle and so on.
Define the test class Shapetest, define the variable shape with the shape interface, use it to point to different class-shaped objects, and output the perimeter of various graphs. It also provides a good extensibility for other shape interface implementation classes.
Input
Enter multiple sets of numeric data (double);
If there are 1 numbers in a row, the radius of the circle is represented;
The length and width of a rectangle, if there are 2 digits (spacing between spaces) in a row.
The length of three sides of a triangle, if there are 3 digits (spacing between spaces) in a row.

If there is a negative number in the input data, it does not represent any graphics, and the perimeter is 0.
Output
The number of rows corresponds to the input, which is the perimeter of the graph evaluated based on the input data for each row (retains 2 decimal digits).
Sample Input
1
2 3
4 5 6
2
-2
-2-3
Sample Output
6.28
10.00
15.00
12.56
0.00
0.00
Hint
When constructing triangles, we should judge whether the length of the given three sides can form a triangle, which conforms to the rules of both sides and greater than the third side.
Pi takes 3.14 when calculating the circumference of a circle.
Source
Zhouxq

Import java.util.*;
    Interface shape{double length ();} class Circle implements shape{double R;
        Circle (Double R) {if (r<=0) THIS.R = 0;
    else THIS.R = R;
    public double Length () {return 3.14*r*2;
    } class Rectangle implements shape{double L, W; Rectangle (Double L, double W) {if (l<=0| |
        w<=0) {L = 0; w = 0; } THIS.L = l;
    THIS.W = W;
    public double Length () {return 2* (l+w);
    } class Triangle implements shape{double X, y, Z;
        Triangle (double A, double b, double c) {int sum=0;
        if (a< (b+c) &&b< (a+c) &&c< (a+b)) {sum = 1; } if (a<=0| | b<=0| | c<=0| |
        sum==0) {a = 0; b = 0; c = 0;
    } x = A;y = B;z = C;
    public double Length () {return (X+Y+Z); } public class Main {public static void main (String []args) {Scanner cin = newScanner (system.in);
            while (Cin.hasnext ()) {String str = cin.nextline ();
            String strs[] = Str.split ("");
            Double s[] = new DOUBLE[100];
            int i;
            For (i=0 i<strs.length; i++) {S[i] = Integer.parseint (strs[i));
                } if (i==1) {Circle c = new Circle (s[0));
            System.out.printf ("%.2f\n", C.length ());
                else if (i==2) {Rectangle p = new Rectangle (s[0), s[1]);
            System.out.printf ("%.2f\n", P.length ());
                else if (i==3) {triangle T = new triangle (s[0), s[1], s[2]);
            System.out.printf ("%.2f\n", T.length ());
    } cin.close (); }
}

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.