1. Lớp HinhCN
public class HinhCN implements
Comparable
{
private double cdai;
private double crong;
public HinhCN()
{
}
public HinhCN(double cdai,double crong)
{
this.cdai=cdai;
this.crong=crong;
}
public double getCRong()
{
return crong;
}
public double getCDai()
{
return cdai;
}
public void setCRong(double a)
{
this.crong=a;
}
public void setCDai(double a))
{
this.cdai=a;
}
public double tinhDT()
{
return cdai*crong;
}
public double tinhCV()
{
return (cdai+crong)*2;
}
public boolean equals(HinhCN h2)
{
if(this.tinhDT()==h2.tinhDT())
{
return true;
}
return false;
}
public int compareTo(Object h2)
{
HinhCN hcn=(HinhCN)h2;
if(this.tinhDT()<hcn.tinhDT())
{
return -1;
}
if(this.tinhDT()==hcn.tinhDT())
{
return 0;
}
return 1;
}
}
2.Lớp test
public class Test
{
public static void main(String [] args)
{
HinhCN a=new HinhCN(5,6);
HinhCN b=new HinhCN(5,6);
System.out.println("Su dung phuong
thuc equals: ");
if(a.equals(b))
{
System.out.println("Dien tich
hai hinh bang nhau");
}
else
{
System.out.println("Dien tich
hai hinh khong bang nhau");
}
HinhCN c=new HinhCN(9,9);
HinhCN d=new HinhCN(10,9);
System.out.println("Su dung phuong thuc compareTo: ");
if(c.compareTo(d)==-1)
{
System.out.println("Dien tich
c<d");
}
else if(c.compareTo(d)==0)
{
System.out.println("Dien tich
c=d");
}else
{
System.out.println("Dien tich
c>d");
}
}
}