“中关村黑马程序员训练营”练习题(一)

 1.  使用面向对象的思想设计一个圆类,实现类的封装。要求属性有圆心和半径,有获得面积和周长的方法。圆心需要设计为Point(点)类,该类具有x,y(表示点的横、纵坐标)两个属性,并定义两个构造方法,一个无参数,另一个以坐标值为参数,设置x,y为给定坐标值。Point类的show方法输出该点的坐标值。

这道题相对比较简单,不多做解释了

 

 

 

package com.itcast.exercise;

public class CircleTest {

public static void main(String[] args) {

Point p = new Point(4,4);

Circle c = new Circle(4,p);

p.show();

c.getArea();

c.getL();

}

}

 

 

package com.itcast.exercise;
 
public class Circle {
private double r;
private Point p;
private double area;
private double perimeter;
public Circle(double r, Point p) {
this.r = r;
this.p = p;
}
public double getArea() {
area = Math.PI*r*r;
System.out.println("面积 = "+area);
return area;
}
public double getL() {
perimeter = 2*Math.PI*r;
System.out.println("周长 = "+perimeter);
return perimeter;
}
}
 
 
package com.itcast.exercise;
 
public class Point {
private double x;
private double y;
public Point() {
this.x = 0.0;
this.y = 0.0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void show() {
System.out.println("x = "+x+", y = "+y);
}
}
 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值