定义一个点类Point,包含两个成员变量x、y,分别表示横、纵坐标;

定义一个点类Point,包含两个成员变量x、y,分别表示横、纵坐标;两个构造方法Point()和Point(int x0,y0);一个movePoint(int dx,int dy)方法,可实现点的位置移动。创建两个Point对象p1、p2,分别调用movePoint()方法后,输出p1和p2的坐标。

public class Point {
		private double x;
		private double y;
		public Point(double x0,double y0) {
			this.x=x0;
			this.y=y0;
		}
		public Point() {
			super();
		}
		 
		public double getX() {
			return x;
		}
		public void setX(double x) {
			this.x = x;
		}
		public double getY() {
			return y;
		}
		public void setY(double y) {
			this.y = y;
		}
		public void movePoint(double dx,double dy) {
			x+=dx;
			y+=dy;

		}
		public static void main(String[] args) {
			Point p1 = new Point (3,4);
			p1.movePoint(2, 5);
			System.out.println("p1坐标为:"+p1.x+","+p1.y);
			
			Point p2 = new Point (6,8);
			p2.movePoint(50, 60);
			System.out.println("p2坐标为:"+p2.x+","+p2.y);
		}

}

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Point类和Circle类的实现: ```c++ #include <iostream> #include <cmath> // 用到了 pow 和 sqrt 函数 using namespace std; class Point { private: double x; double y; public: Point(): x(0), y(0) {} // 默认构造函数,将 x 和 y 初始化为 0 Point(double x, double y): x(x), y(y) {} // 有参数的构造函数 Point(const Point& p): x(p.x), y(p.y) {} // 拷贝构造函数 double getX() const { return x; } double getY() const { return y; } }; class Circle { private: Point center; double radius; public: Circle(): center(), radius(0) {} // 默认构造函数,将圆心初始化为 (0, 0),半径初始化为 0 Circle(double x, double y, double radius): center(x, y), radius(radius) {} // 有参数的构造函数 Circle(const Point& p, double radius): center(p), radius(radius) {} // 有参数的构造函数 Circle(const Circle& c): center(c.center), radius(c.radius) {} // 拷贝构造函数 double getArea() const { return 3.14159265 * pow(radius, 2); } // 求圆的面积 double getPerimeter() const { return 2 * 3.14159265 * radius; } // 求圆的周长 bool isInTheCircle(const Point& p) const { // 判断点是否在圆内部 double distance = sqrt(pow(p.getX() - center.getX(), 2) + pow(p.getY() - center.getY(), 2)); return distance < radius; } }; int main() { double x, y, radius; cin >> x >> y; cin >> radius; Circle c(x, y, radius); cin >> x >> y; Point p(x, y); cout << c.getArea() << endl; cout << c.getPerimeter() << endl; if (c.isInTheCircle(p)) cout << "该点在圆内部!" << endl; else cout << "该点不在圆内部!" << endl; return 0; } ``` 输入样例: ``` 1 2 3 4 5 ``` 输出样例: ``` 28.2743 18.8496 该点不在圆内部! ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值