A. java 定义一个圆类Circle,就解
publicclassCircle{
//圆的半径
privatedoubleradius;
//求圆的面积
publicdoublefindArea(){
returnMath.PI*radius*radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
}
测试类自己去写。
B. JAVA中怎么定义一个圆类 要求如下
public class Circle {
private double r;
public Circle(double r) {
this.r = r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
/**
* 面积
*
* @return
*/
public double getArea() {
return Math.PI * Math.pow(r, 2);
}
/**
* 周长
*
* @return
*/
public double getLong() {
return Math.PI * r * 2;
}
}class Cylinder extends Circle {
private double h;
public Cylinder(double r, double h) {
super(r);
this.h = h;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
/**
* 体积 = 底面积*高
*
* @return
*/
public double getVolume() {
return super.getArea() * h;
}
/***
* 表面积=测面积+2个底面积
*
* @return
*/
public double getSurfacearea() {
return super.getLong() * h + 2 * super.getArea();
}
}
测试代码就不写了
C. JAVA定义圆形类。
public class Circle{
private double r;
public void setR(double r){
this.r=r;
}
public double getArea(){
return Math.PI*r*r;
}
public double getPerimeter(){
return 2*Math.PI*r;
}
}
public class TestCircle{
public static void main(String [] args){
//两个不同对象
Circle circle1 = new Circle();
Circle circle2 = new Circle();
//设置不同半径
circle1.setR(2);
circle2.setR(3);
//显示结果
system.out.println("半径为2的圆面积是"+circle1.getArea()+", 周长是"+circle1.getPerimeter());
system.out.println("半径为3的圆面积是"+circle2.getArea()+", 周长是"+circle2.getPerimeter());
}
}
D. 急求Java代码,定义一个Circle(圆类型)
public class Exam
{
public static void main(String[] args)
{
Circle c=new Circle(3,4,5);
System.out.printf("圆心:(%f,%f),半径:%f,面积:%f",c.x,c.y,c.r,c.countArea());
}
}
class Circle
{
public Circle()
{
this(0,0,0);
}
public Circle(double x,double y,double r)
{
this.x=x;
this.y=y;
this.r=r;
}
public double countArea()
{
return Math.PI*r*r;
}
/*private*/public double x,y,r;
}
E. java编程:定义一个圆circle类,包含如下内容
代码如下:
importjava.util.Scanner;
classCircle{
privatedoubleradius;
publicCircle(doubleradius){
this.radius=radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
publicdoublegetArea(){
returnMath.PI*radius*radius;
}
publicdoublegetPerimeter(){
return2*Math.PI*radius;
}
}
publicclassApp{
publicstaticvoidmain(String[]argv){
Scannerscanner=newScanner(System.in);
System.out.print("请输入圆半径:");
doubler=scanner.nextDouble();
Circlec=newCircle(r);
System.out.println("面积:"+c.getArea()+",周长:"+c.getPerimeter());
}
}
运行结果: