java圆类代码,java圆的周长代码
JAVA定义圆形类。
public class Circle{
创新互联公司-专业网站定制、快速模板网站建设、高性价比雅安网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式雅安网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖雅安地区。费用合理售后完善,十多年实体公司更值得信赖。
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());
}
}
Java实验,代码怎么写?
Shape.java接口代码
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圆类代码
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * this.radius * this.radius;
}
public double perimeter() {
return 2 * PI * this.radius;
}
}
Cylinder.java圆柱体类代码
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double area() {
return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
return super.area() * this.height;
}
}
X5_3_6.java主类代码
public class X5_3_6 {
public static void main(String[] args) {
Circle cir1 = new Circle(5);
System.out.println("圆的面积为:" + cir1.area());
System.out.println("圆的周长为:" + cir1.perimeter());
Cylinder cy1 = new Cylinder(10, 15);
System.out.println("圆柱体的表面积为:" + cy1.area());
System.out.println("圆柱体的体积为:" + cy1.volume());
}
}
上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。
急求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;
}
JAVA基础实现圆类Circle
/////写着也够累的....
public class Cylinder extends Circle{
private double h;
public Cylinder(double h, double r, int x, int y) {
super(r, x, y);
this.h = h;
}
public Cylinder(){
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
public double area(){
return 2 * super.area() + super.perimeter() * h;
}
public double volume() {
return super.area() * h;
}
public static void main(String[] args) {
Cylinder cy = new Cylinder(4.5, 2.3, 2 , 3);
System.out.println("area: " + cy.area());
System.out.println("volume: " + cy.volume());
}
}
class Circle{
private int x;
private int y;
private double r;
public Circle(){
}
public Circle(double r, int x, int y) {
this.r = r;
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double perimeter(){
return 2 * Math.PI * r;
}
public double area(){
return Math.PI * r * r;
}
}
用JAVA编写圆
代码如下:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class TestSw extends JFrame { public static void main(String[] args) { new TestSw(); } public TestSw(){ super("Test"); this.setSize(new Dimension(400,300)); this.setContentPane(new Mypane()); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class Mypane extends JPanel{ public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.setXORMode(Color.white); g.drawArc(20, 20, 100, 100, 0, 360); ///此方法将画一个直径100的圆.红色. } } }
当前文章:java圆类代码,java圆的周长代码
分享URL:http://ybzwz.com/article/dsgcjeh.html