定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(intx0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标
public class Point {
public static void main(String args[]){
Point p1 = new Point(1,1);
Point p2 = new Point(2,2);
p1.movePoint(p2.x,p2.y);
System.out.println(p1.x+" "+p1.y);
}
int x;
int y;
Point(){
}
Point(int x,int y){
this.x = x;
this.y = y;
}
public void movePoint(int dx,int dy){
this.x += dx;
this.y += dy;
}
}
定义一个矩形类Rectangle:(知识点:对象的创建和使用)[必做题]
• 2.1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
• 2.2 有2个属性:长length、宽width
• 2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
• 2.4 创建一个Rectangle对象,并输出相关信息
public class Rectangle {
public static void main(String args[]){
Rectangle rectangle = new Rectangle(2,3);
rectangle.shouAll();
}
public double length;
public double width;
Rectangle(double length,double width){
this.width = width;
this.length = length;
}
public double getArea(){
return length*width;
}
public double getPer(){
return 2*(length+width);
}
public void shouAll(){
System.out.println(this.length+" "+this.width+" "+this.getArea()+" "
+this.getPer());
}
}