Point p=new Point();
时间: 2024-01-03 13:04:23 浏览: 17
This line of code creates a new object of the Point class and initializes it with default values. The default constructor of the Point class is called, which initializes the x and y coordinates of the point to 0.0. The object is assigned to the variable p of type Point.
相关问题
使用Java定义Point类描述屏幕上的一个点。该类包括点的x和y两个坐标,及对点进行的相关操作,如:取得点的坐标值,对点的坐标进行赋值。此类属于MyPackageTwoD包。此包中另一个Rectangle类可以实现矩形的基本操作,包含Point、width、height等3个变量,及构造方法和其他方法。文件TestPackage.java中包含main()方法,定义如下://TestPackage.java import MyPackageTwoD.*; public class TestPackage{ public static void main(String ars[]){ Point p=new Point(1,2); Rectangle r1=new Rectangle(p,10,10); System.out.println("The area of the rectangle is "+r1.area()); } }
以下是Java代码实现:
Point.java
```java
package MyPackageTwoD;
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
```
Rectangle.java
```java
package MyPackageTwoD;
public class Rectangle {
private Point point;
private int width;
private int height;
public Rectangle(Point point, int width, int height) {
this.point = point;
this.width = width;
this.height = height;
}
public int area() {
return width * height;
}
}
```
TestPackage.java
```java
import MyPackageTwoD.*;
public class TestPackage {
public static void main(String args[]) {
Point p = new Point(1, 2);
Rectangle r1 = new Rectangle(p, 10, 10);
System.out.println("The area of the rectangle is " + r1.area());
}
}
```
【问题描述】 分别设计点类Point和圆类Circle, 点类有两个私有数据纵坐标和横坐标; 圆类有也两个私有数据圆心和半径,其中圆心是一个点类对象; 要求如下所述: (1) 通过构造方法初始化数据成员,数据成员的初始化通过构造方法的参数传递; (2) 分别编写点和圆的输出函数,其中圆的输出包括圆心和半径的输出;点的输出x,y; 【输入形式】无 【输出形式】main方法执行内容 【样例输入】无 【样例输出】 x=10;y=20; center=(10,20);r=5; 【样例说明】 【评分标准】 public class Circle{ public void showInfo(){System.out.println("center=("+center.getx()+","+center.gety()+");r="+r+";");} public static void main(String args[]){ Point p=new Point(10,20); Circle c=new Circle(5,p); p.showInfo(); c.showInfo(); } } class Point{ private int x,y; }
Point类和Circle类的实现如下:
```
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void showInfo() {
System.out.println("x=" + x + ";y=" + y + ";");
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Circle {
private Point center;
private int r;
public Circle(int r, Point center) {
this.r = r;
this.center = center;
}
public void showInfo() {
System.out.println("center=(" + center.getX() + "," + center.getY() + ");r=" + r + ";");
}
public static void main(String[] args) {
Point p = new Point(10, 20);
Circle c = new Circle(5, p);
p.showInfo();
c.showInfo();
}
}
```
运行结果为:
```
x=10;y=20;
center=(10,20);r=5;
```
以上为角色扮演回答,不代表任何立场和观点。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)