// TODO 自动生成的方法存根 return pl . https://www.doczj.com/doc/b05920733.html,pareTo( p2. name);
}
}
package person; import
public class CompareBirthday
impleme nts ComparatorvPers on>{
@Override
public int compare(Pers on
p1 , Pers on
p2) {
// TODO 自动生成的方法存根 return p1 . birthday .compareTo( p2. birthday );
} }
package person; import
public class CompareAge
impleme nts ComparatorvPers on>{
@SuppressWarnings ("deprecation" ) @Override
public int compare(Pers on
pl , Pers on p2) {
// TODO 自动生成的方法存根
return new Integer( pl . age ).compareTo( System. out .println(
"姓名:"+person [ i ]. name+" 性别:"+person [ i ]. sex +" 年
龄:"+person [ i ]. age+"生日:"+person [ i ]. birthday );
newlnteger( p2. age));
} }
package import public
pers on; class Pers on Text
{ public static
// TODO void main( Str in g[] 自动生成的方法存根 args ) {
int n=5; Pers on[]
pers on [0]= new Pers on( "张三“, "4569", "1996 年6月6日", '男’,22); pers on [1]= new Pers on( "李四",
“1234",
"1995 年4月3日", '男’,23); pers on [2]= new Pers on( "王五", "2589", "1997 年3月5日", '男’,21); pers on [3]= new Pers on( "赵丽“, "3698",
"1998 年2月5日", '女',20);
pers on [4]= new Pers on( "宋月“, "5869", "1998 年1月 1日", '女',20); System. out .pri ntl n( “原来顺序为 :");
for (int i =0; i
■
System. out .pr in tl n( "姓名: :"+pers on [i]. name+"性别 :"+pers on 龄:"+pers on } System. Arrays. for (int out .println( "\n 按姓名排序后顺序为 sort ( person , new CompareName()); i =0; i pers on =new Pers on[ n]; age+" 生日:"+person [ i ]. birthday [i ]. ); [i ]. sex +"年 } System. out .println( "\n 按年龄排序后顺序为 :"); Arrays. sort ( person , new CompareAge()); for (int i =0; i System. out .println( "姓名:"+person [ i ]. name+" 性别:"+person [ i ]. sex +" 年 龄:"+person [ i ]. age+"生日:"+person [ i ]. birthday ); } System. out .println( "\n 按生日排序后顺序为 :"); Arrays. sort ( person , new CompareBirthday()); for (int i =0; i System. out .println( "姓名:"+person [ i ]. name+" 性别:"+person [ i ]. sex +" 年 龄:"+person [ i ]. age+"生日:"+person [ i ]. birthday ); } } } 实验结果: 2.首先定义基本几何图形接口( IShape ),然后定义IShape 的实现类Circle(圆 形)、Square (正 方形)和Triangle(三角形)。要求计算各种图形的面积和周长。 (提示:求三角形的面积用海伦公式: S L * (L a)*(L b )*(L c),其中a,b,c 是三角形的边长,L=(a+b+c)/2 ,即周长的一半。) 源代码: package jiekoushape; public in terface IShape { this . r = r ; } public Stri ng getName() { return n ame; public void setName(Stri ng this . name = name; public double public double } package jiekoushape; public class Circle area(); len gth(); impleme nts IShape{ private private Str ing double n ame; public Circle(Str ing super (); this . name = name; n ame, double r) { public double area() { // TODO自动生成的方法存根 return a* a; } @Override public double len gth() { // TODO自动生成的方法存根 return 4* a; } @Override public String toString() { // TODO自动生成的方法存根 } } package jiekoushape; public class Trian gle impleme nts IShape{ private Str ing n ame; private double a; private double b; private double c; public Tria ngle(Stri ng n ame, double a, double b, double c) { super (); this . name = name; this . a = a; this . b = b; this . c = c; } public Stri ng getName() { return n ame; } public void setName(Stri ng n ame) { this . name = name; } public double getA() { return a; } public void setA( double a) { this . a = a; } public double getB() { return b; } public void setB( double b) { this . b = b; } public double getC() { return c; } public void setC( double c) { this . c = c; } public double area() { // TODOAuto-ge nerated method stub double l =( a+b+c)/2; return Math. sqrt ( l *( l - a)*( l - b)*( l - c)); } public double len gth() { // TODOAuto-ge nerated method stub retur n a+b+c; } @Override public Stri ng toStri ng() { // TODO 自动生成的方法存根 return name+"周长是:"+length()+ "," +"面积是:"+area(); } } package jiekoushape; public class DemoIShape { public static void main( Str in g[] args ) { // TODO 自动生成的方法存根 实验结果: 实验总结(结论或问题分析) : 本次实验,主要是对接口、 https://www.doczj.com/doc/b05920733.html, ng 包中的基础类库和 java.util 包中的工具 类库的使用,其中最难得就是使用 Comparator 查阅资料了解了 Comparator 如何使用,受益匪浅。 IShape shape =n ull shape =new Circle( System. out .println( shape =new Square( System. out .println( shape =new Trian gle( System. "圆形“ ,3); shape .toString()); "正方形",4); shape .toString()); "三角形",3, 4, 5) shape .toString());