Java defines a circle class that contains a double-type RADIUS attribute that represents the radius of the circle, and a Findarea () method that returns the area of the circle

Source: Internet
Author: User

The requirements are as follows:
(1defines a circle class that contains a double-type RADIUS attribute that represents the radius of the circle, and a Findarea () method returns the area of the circle. (2Define a class Passobject, define a Method Printareas () in the class, and the method is defined as follows: Public voidPrintareas (Cirlce C,intTimes ) Prints each integer radius value between 1 and time in the Printareas method, along with the corresponding area. For example, the Times is 5, the output radius is 1,2,3,4,5, as well as the corresponding circle area. Call the Printareas () method in the main method, and output the current RADIUS value after the call is complete. Packagestudy03; Public classCircle {Private Doubleradius; Public voidSetradius (Doubleradius) { This. Radius =radius; } Public DoubleGetradius () {returnradius; } Public DoubleFindarea () {return3.14*radius*radius; }} Packagestudy03; Public classPassobject { Public voidPrintArea (Circle C,intTime ) {System.out.println ("Radius\t\tarea"); for(inti = 1; I <= time; i++) {C.setradius (i); System.out.println (C.getradius ()+ "\t\t" +C.findarea ()); } }} Packagestudy03; Public classTestpassobject { Public Static voidMain (string[] args) {Circle C=NewCircle (); Passobject PO=NewPassobject (); Po.printarea (c,5); }}


The output results are as follows:

Radius area
1.0 3.14
2.0 12.56
3.0 28.259999999999998
4.0 50.24
5.0 78.5

Java defines a circle class that contains a double-type RADIUS attribute that represents the radius of the circle, and a Findarea () method returns the area of the circle

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.