An exercise in which an object is passed as a parameter

Source: Internet
Author: User

(1) Sets a Cirlce class that contains a RADIUS attribute of type double that represents the radius of the circle.

Includes a Findarea method that returns the area of a circle.

(2) Set up a Passobject class that defines a Printareas method in a class that refers to the following:

public void Printareas (Crilce c,int time) {}//object passed as a parameter

In the Printareas method, the RADIUS value of each integer from 1 to time is printed, as well as the area calculated by this radius.

For example: Time is 5, the radius of the output 1,2,3,4,5 and their corresponding circle area

Then call the Printareas method in the main method, and then output the current RADIUS value after the call is complete.

1  Packagecom.cnblogs.exer;2 3 classcirlce{4     DoubleRadius//radius property of a circle5      Public DoubleFindarea () {//set a method to calculate the area of a circle6         returnmath.pi*radius*radius;7     }8      Public DoubleGetradius () {//Get Radius9         returnradius;Ten     } One      Public voidSetradius (DoubleR) {//Set Radius Aradius=R; -     } - } the  -  Public classPassobject { -      Public Static voidMain (string[] args) { -Passobject po=NewPassobject (); +Cirlce c=Newcirlce (); -Po.printareas (c, 5);//call method The radius and area of the output circle +System.out.println ("Now radius is:" +C.getradius ()); A     } at     //set a method the radius and area of the output circle -      Public voidPrintareas (Cirlce C,intTime ) { -System.out.println ("Radius:" + "\t\t" + "area:"); -         intI=1; -          while(i<=Time ) { -C.setradius (i);//Set radius =i inSystem.out.println (C.getradius () + "\t\t" +C.findarea ()); -i++; to         } +C.setradius (i);//calculates the radius value of the current circle -     } the}

Operation Result:

An exercise in which an object is passed as a parameter

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.