「java用构造函数构造圆」构造函数怎么构造

博主:adminadmin 2023-01-15 20:45:06 564

今天给各位分享java用构造函数构造圆的知识,其中也会对构造函数怎么构造进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

javascript带参数的构造函数定义一个对象Circle(圆)

script type="text/javascript"

function Circle(x,y,radius){

this.x=x;

this.y=y;

this.radius=radius;

}

Circle.prototype.getDiameter=function(){

return 2*this.radius;

}

Circle.prototype.getCircumference=function(){

return Math.PI*2*this.radius;

}

Circle.prototype.getArea=function(){

return Math.PI*this.radius*this.radius;

}

//test for the object;

var circle1=new Circle(10,10,10);

alert(circle1.getDiameter());

alert(circle1.getCircumference());

alert(circle1.getArea());

/script

Java定义一个圆类circle,它有一个变量radius(半径)从键盘输出数据,通过构造函数的参数 传递给radius

程序没错,如果你的意思是实现从控制台输入,那么下边是示例代码

BufferedReader stdin = new BufferedReader(

new InputStreamReader(System.in));

String s;

 

while((s = stdin.readLine()) != null s.length()!= 0)

System.out.println(s);

急求,,在java中创建Circle类,有成员r,有待参数的构造函数来初始化r

public class TestCircle {

public static void main(String[] args) {

Circle[] circles = new Circle[]{

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10)),

new Circle((int)(Math.random() * 10))

};

for (int i=0; icircles.length; i++) {

System.out.println("Circle_" + i +":R=" + circles[i].r+";area=" + circles[i].getArea());

}

}

}

class Circle{

int r;

Circle() {

this.r = (int)(Math.random() * 10);

}

Circle(int r) {

this.r = r;

}

public double getArea() {

return 3.14 * r * r;

}

}

用java语言定义一个Circle类求圆面积,用有、无参构造方法,设计并实现两点间距离.

import java.lang.*;

public class Circle {

    private int radius; // 圆半径

    private double area; // 圆面积

    final double PI = 3.1415926; // 圆周率

    

    // 带参构造函数

    public Circle(int r){

        radius = r; 

    }

    

    // 无参构造函数

    public Circle(){

        this(0); // 调用有参构造函数,初始化半径为0

    }

    

// 求面积

public double area(){

    return radius*radius*PI;

}

// 定义Circle类的对象,通过对象成员方法输出圆的面积

public static void main(String args[]) {

    Circle circle = new Circle();

    System.out.println("圆面积:"+circle.area().toString);

}

}

// 采用面向对象的思想,设计并实现两点间距离

public class Point {

    private int x; // 点的x坐标

    private int y; // 点的y坐标

    

    public Point(int x, int y){

        this.x = x;

        this.y =y;

    }

    

    // 求距离

    public double distans(Point p){

        return Math.sqrt(Math.pow(p.x - this.x) +Math.pow(p.y - this.y)); // 两点间距离公式 

    }

    // 实例化对象并调用方法同上,这里就不写了

}

java用构造函数构造圆的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于构造函数怎么构造、java用构造函数构造圆的信息别忘了在本站进行查找喔。