//接口
public interface shapeTool {
public double perimeter();
public double area();
}
//矩形类
public class Rectangle implements shapeTool{
private double length;//矩形的长度
private double width;//矩形的宽度
/**
* 矩形的无参构造方法(生成一个默认的矩形,默认的矩形是一个边长为1的正方形)
*/
public Rectangle(){
length=1;
width=1;
}
/**
* 矩形的有参构造方法
* @param length
* @param width
*/
public Rectangle(double length,double width){
if(length>0&&width>0)
{
this.length=length;
this.width=width;
}
else
{
System.out.println("矩形的长度和宽度都应该大于0!");//当用户非法构造矩形时,将生成默认的矩形
this.length=1;
this.width=1;
}
}
/**
* 修改矩形的长度
* @param length
*/
public void setlength(double length){
if(length>0)
this.length=length;
else
System.out.println("矩形的长度修改失败!");
}
/**
* 访问矩形的长度属性
* @return
*/
public double getlength(){
return length;
}
/**
* 修改矩形的宽度
* @param width
*/
public void setwidth(double width){
if(width>0)
this.width=width;
else
System.out.println("矩形的宽度修改失败!");
}
/**
* 访问矩形的宽度属性
* @return
*/
public double getwidth(){
return width;
}
//计算矩形的周长
public double perimeter(){
return length*2+width*2;
}
//计算矩形的面积
public double area(){
return length*width;
}
}
//测试类
public class Testshap {
public static void main(String[] args){
System.out.println("构造了三个矩形(用来调试类的构造方法):");
Rectangle one=new Rectangle();
Rectangle two=new Rectangle(2,3);
Rectangle three=new Rectangle(-1,2);
System.out.println("分别输出三个矩形的周长与面积:");
System.out.println(one.perimeter());
System.out.println(two.perimeter());
System.out.println(three.perimeter());
System.out.println(one.area());
System.out.println(two.area());
System.out.println(three.area());
System.out.println("调试访问器:");
System.out.println("**修改矩形的长度和宽度前:");
System.out.println(one.getlength());
System.out.println(one.getwidth());
System.out.println(one.perimeter());
System.out.println(one.area());
System.out.println("**利用访问器修改矩形的长度和宽度后:");
one.setlength(2);
one.setwidth(2);
System.out.println(one.getlength());
System.out.println(one.getwidth());
System.out.println(one.perimeter());
System.out.println(one.area());
System.out.println("**非法修改矩形的长度和宽度后:");
one.setlength(-1);
one.setwidth(-2);
System.out.println(one.getlength());
System.out.println(one.getwidth());
System.out.println(one.perimeter());
System.out.println(one.area());
}
}
public class Rectangle {
// top, left 左上角那个顶点的坐标
// width: 宽
// heigth: 长
private double top, left, width, height;
// 构造函数
public Rectangle(double top, double left, double width, double height) {
this.top = top;
this.left = left;
this.width = width;
this.height = height;
}
// 改变顶点坐标,即改变矩形坐标位置
public void location(double top, double left) {
this.top = top;
this.left = left;
}
// 改变宽,高,即改变矩形宽高
public void size(double width, double height) {
this.width = width;
this.height = height;
}
// 计算面积,宽×高
public double area() {
return width * height;
}
// 判断某点是否在矩形内
public boolean isInside(double x, double y) {
// 这里采用的是数学上的坐标系,即向上向右为正
// 如果采用向下向右为正的话,则要改
// return x > this.left && x < this.left + this.width && y < this.top +
// this.height && y > this.top;
// 这里点不包括在边上,如果在边上也算的话,把小于号或大于号改成小于等于及大于等于
return x > this.left && x < this.left + this.width
&& y > this.top - this.height && y < this.top;
}
}
4