(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