「java圆个数」圆的java代码
本篇文章给大家谈谈java圆个数,以及圆的java代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
JAVA参数画圆
//画圆一般通过继承JPanel 或者JFrame ,通过调用
//panel或者frame中的Graphics实例完成画图
public void drawCircle(int x,int y,int r,Color color)
{
Graphics g=this.getGraphics();
g.setColor(color);
g.drawOval(x-r, y-r, 2*r, 2*r);
}
其他参数的情况可以重载这个方法,并调用方法中通过设定参数
调用这个public void drawCircle(int x,int y,int r,Color color)方法
来实现
如:
public void drawCircle(int x,int y,int r)
{
this.drawCircle( x, y , r ,Color.Black);
}
public void drawCircle(int x,int y,Color color)
{
Random random=new Random;
r=10+random.nextInt(90);
this.drawCircle( x, y, r , color);
}
public void drawCircle(int x,int y)
{
this.drawCircle(x, y, 40 , Color.red);
}
用java创建10个由圆
class Circle {
public static final double PI = 3.14159; //圆周率
private double radius; //半径
//构造方法,将半径置为0
public Circle() {
radius = 0.0;
}
//构造方法,创建Circle对象时将半径初始化为r
public Circle(double r) {
this.radius = r;
}
//获得圆的半径值
public double getRadius(){
return this.radius;
}
//获得圆的周长
public double getArea() {
return PI * radius * radius;
}
//获得圆的面积
public double getPerimeter() {
return 2 * PI * radius;
}
// 将圆的半径、周长、面积输出到屏幕
public void disp() {
System.out.println("圆的半径为:"+getRadius()+" 圆的周长为:"+getPerimeter()+" 圆的面积为:"+getArea());
}
}
//测试类
class testCircle {
public static void main(String args[]) {
Circle[] circleArray = new Circle[10];
for (int i = 0; i circleArray.length; i++) {
circleArray[i] = new Circle(Math.random()*100);
}
for (Circle circle : circleArray) {
circle.disp();
}
}
}
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();
}
}
测试代码就不写了
java中如何定义一个圆
定义圆,只要定义一个圆类,类属性包括:
1.
坐标信息
包括横、纵坐标两个属性,类型为浮点数;
2.
半径
浮点数;
随后再创建圆类的对象,将以上两种数据写入该对象即可。
java圆个数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于圆的java代码、java圆个数的信息别忘了在本站进行查找喔。