欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

按要求编写一个Java应用程序:编写一个矩形类

程序员文章站 2022-07-14 23:17:48
...

package Text5;

public class Text5 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	PlainRect ljt = new PlainRect();
	Rect ml = new Rect();
	ljt.showxy(20, 10, 10, 10);
	ml.area();
	ml.perimeter();
	ljt.isInside(25.5, 13);
}

}

package Text5;

//矩形类
public class Rect {
public double length = 10; //长
public double width = 10; //宽

public void show(double length, double width) {
	this.length = length;
	this.width = width;
}

public void area() {
	double sum;
	sum = width * length;
	System.out.println("面积为: " + sum);
}

public void perimeter() {
	double sum;
	sum = (width + length) * 2;
	System.out.println("周长为:"  + sum);
}

}

package Text5;

public class PlainRect extends Rect{
public double startx;
public double starty;

public void showxy(double length, double width, double startx, double starty) {
	System.out.println("请分别输入长,宽,x与y:");
	this.length = length;
	this.width = width;
	this.startx = startx;
	this.starty = starty;
	System.out.println("长为:" + length + "宽为:" + width);
	System.out.println("x为:" + startx + "y为:" + starty);
}

public void isInside(double x, double y) {
	if (x  >= startx && x <= (startx + width) && y < starty && y >= (starty-width))
		System.out.println("在!");
	else
		System.out.println("不在!");
}

}

相关标签: Java