Calculate the area of different shape shapes based on perimeter? Calculate the area of multiple graphs

Source: Internet
Author: User

/** * Calculate the area of different shapes according to perimeter? Calculates the area of a variety of graphs, * and compares the maximum values of various graphic areas. The square area formula is: 0.0625*c*c. * Circle Area formula is: 0.0796*c*c, where c represents the perimeter of the graph. */
/** * Calculate the area of different shapes according to perimeter? Calculates the area of a variety of graphs, * and compares the maximum values of various graphic areas. The square area formula is: 0.0625*c*c. * Circle Area formula is: 0.0796*c*c, where c represents the perimeter of the graph. */ Public classAreatest { Public Static voidMain (string[] args) {//have learned the collection, try to use the collection to do this problem://defines a collection and adds the address of the graphical element instance to the collection        /*list<shape> shapeslist = new arraylist<shape> ();        Shapeslist.add (New Circle (1));        Shapeslist.add (New Circle (15));        Shapeslist.add (New Square (5)); Shapeslist.add (new Square);*/        //Compare Size        /*1. The method of comparing the size of an array is to assume a maximum value, and then compare it in turn, exchanging the maximum value of * 2. How about a collection? Try the same way.         * 3. Assuming that the first element is the maximum value, the area of the first garden needs to be calculated and assigned to double Max;         * 4. Iterate through the collection, can you swap the elements with iterators? * */        /*either way, the first step is to set the maximum value. * Collection gets elements by List.get (element subscript)*/        /*Double maxarea = shapeslist.get (0). Area ();*/        /*The new loop iterates through the collection, but cannot manipulate the specific subscript, so I don't know the subscript for the largest graph*/        /*For (Shape shapes:shapeslist) {if (Maxarea < Shapes.area ()) {Maxarea = Shapes.area (); }} System.out.println ("Maximum area:" +maxarea);*/        /*with the old cycle, the maximum area and subscript position can be obtained.*/        /*int indexmax = 0; for (int i =0;i<shapeslist.size (), i++) {if (Maxarea < Shapeslist.get (i). Area ()) {Maxarea = s                Hapeslist.get (i). area ();            Indexmax = i;        }} System.out.println ("Maximum area:" +maxarea); System.out.println ("The maximum area corresponding subscript is:" +indexmax);*/        /*Iterator Procedure*/        /*iterator<shape> it = Shapeslist.iterator ();            while (It.hasnext ()) {Shape shapes = It.next ();            if (Maxarea < Shapes.area ()) {Maxarea = Shapes.area (); }} System.out.println ("Maximum area:" +maxarea);*/        /*Array Practices*/        //defines an array that stores graphical objects.shape[] Shapes =Newshape[4]; shapes[0] =NewCircle (1); shapes[1] =NewCircle (2); shapes[2] =NewSquare (5); shapes[3] =NewSquare (4); DoubleMaxarea = shapes[0].area (); intindex =0;  for(intI=0; i<shapes.length;i++){            DoubleArea =Shapes[i].area (); if(Maxarea <Area ) {Maxarea=Area ; Index=i; }} System. out. println ("The maximum area is:"+Maxarea); System. out. println ("The subscript for the maximum area corresponds to:"+index); }}
View Code

Classes that define individual graphs

/*figure area, because do not know the specific graphics, you must define an abstract class, the graph has a perimeter, but the area of the method is different, each method to write specific*/Abstract classshape{protected DoubleC; //The perimeter of the graph, which is titled a given value, can be used as a known property to directly construct the method when assigning C.    Abstract DoubleArea (); //because the area formulas of different graphs are not the same, they are written in abstract way, and the subclasses are implemented according to the different formulas.}classCircleextendsShape {/*Circular construction method when creating a circular instance, the value of C is assigned*/     PublicCircle (Doublec) {         This. C =C; }    /*finding the area of the garden according to the area formula of Circle*/@OverrideDoubleArea () {returnc*c*0.0796; }}classSquareextendsshape{/*constructor method to create a square instance, the value of C is assigned*/     PublicSquare (Doublec) {         This. C =C; }    /*Calculate square area According to Square area formula*/@OverrideDoubleArea () {returnc*c*0.0625; }}
View Code

Calculate the area of different shape shapes based on perimeter? Calculate the area of multiple graphs

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.