您好,欢迎访问三七文档
1.设计一个名为figure的图形软件包(package)。包中包含三角形、矩形、圆三个类。要求:(1)每个类都要构造方法并为成员设置get和set方法;(2)每个类都要有计算周长和面积的成员方法;(3)完成该软件包后的编码后,在另一个包的含有main方法的类中编写代码,分别使用图形软件包中的三个类,生成三个对象,并打印出其周长和面积。******************************************************packagefigure;publicclassjuxing{//矩形的计算函数privatedoublelenth,width;publicjuxing(){lenth=0;width=0;}publicjuxing(doublex,doubley){lenth=x;width=y;}publicvoidsetLenth(doublelenth){this.lenth=lenth;}publicdoublegetLenth(){returnlenth;}publicvoidsetWidth(doublewidth){this.width=width;}publicdoublegetWidth(){returnwidth;}publicdoubleperimeter(){returnlenth*width;}publicdoubleArea(){return(lenth+width)*2;}}******************************************************packagefigure;importjava.math.*;publicclasssanjiaoxing{//三角形的计算函数privatedoubleside1;privatedoubleside2;privatedoubleside3;publicsanjiaoxing(){side1=0;side2=0;side3=0;}publicsanjiaoxing(doublex,doubley,doublez){side1=x;side2=y;side3=z;}publicvoidsetSide1(doubleside1){this.side1=side1;}publicdoublegetSide1(){returnside1;}publicvoidsetSide2(doubleside2){this.side2=side2;}publicdoublegetSide2(){returnside2;}publicvoidsetSide3(doubleside3){this.side3=side3;}publicdoublegetSide3(){returnside3;}publicdoubleperimeter(){returnside1+side2+side3;}publicdoubleArea(){doublep=(side1+side2+side3)/2;returnMath.sqrt(p*(p-side1)*(p-side2)*(p-side3));}}******************************************************packagefigure;importjava.math.*;publicclassyuanxing{//圆形的计算函数privatedoubleradius;publicyuanxing(){radius=0;}publicyuanxing(doublex){radius=x;}publicvoidsetRadius(doubleradius){this.radius=radius;}publicdoublegetRadius(){returnradius;}publicdoubleperimeter(){return2*Math.PI*radius;}publicdoubleArea(){returnMath.PI*radius*radius;}}******************************************************packagefigure_show;importfigure.juxing;importfigure.sanjiaoxing;importfigure.yuanxing;publicclassmain{publicstaticvoidmain(String[]args){sanjiaoxingt=newsanjiaoxing(3,4,5);System.out.println(三角形的周长:+t.perimeter());System.out.println(三角形的面积:+t.Area());juxingr=newjuxing(3,4);System.out.println(矩形的周长:+r.perimeter());System.out.println(矩形的面积:+r.Area());yuanxingc=newyuanxing(5);System.out.println(圆形的周长;+c.perimeter());System.out.println(圆形的面积:+c.Area());}}2.设计一个教师类Teacher(属于cn.net.sdkd包),要求:1)属性有编号(intno)、姓名(Stringname)、年龄(intage)、所属学院(seminary),为这些属性设置相应的get和set方法。2)为Teacher类重写equals方法,要求:当两个教师对象的no相同时返回true。3)重写Teacher类的toString方法,通过该方法可以返回“编号为**、姓名为**、年龄为**的**学院老师”形式的字符串。4)由多个Teacher对象所形成的数组可以以两种方法排序(编号由低到高排序):1)使用Arrays.sort(Object[]a)方法;2)使用Arrays.sort(Object[]a,Comparatorc)方法。5)再定义一个类TeacherManagement(属于cn.sd包),提供方法search,方法可以在一组给定的教师中,根据姓名(或年龄)返回等于指定姓名(或年龄)的教师的字符串信息,信息格式为:“编号为**、姓名为**、年龄为**的**学院老师”。如果没有满足条件的教师,则返回“没有符合条件的教师”。6)构造main方法进行测试。******************************************************packagecn.net.sdkd;publicclassTeacherimplementsComparable{privateintno;privateStringname;privateintage;privateStringseminary;publicTeacher(intno,Stringname,intage,Stringseminary){this.no=no;this.name=name;this.age=age;this.seminary=seminary;}publicvoidsetNo(intno){this.no=no;}publicintgetNo(){returnno;}publicvoidsetName(Stringname){this.name=name;}publicStringgetName(){returnname;}publicvoidsetAge(intage){this.age=age;}publicintgetAge(){returnage;}publicvoidsetSeminary(Stringseminary){this.seminary=seminary;}publicStringgetSeminary(){returnseminary;}publicbooleanequals(Objecto){booleanresult=false;Teachert=(Teacher)o;if(this.no==t.no){returntrue;}returnresult;}publicStringtoString(){return编号为+getNo()+,姓名为+getName()+,年龄为+getAge()+的+getSeminary()+学院老师;}//compareTo接口publicintcompareTo(Objecto){TeacherotherTea=(Teacher)o;if(nootherTea.no)return-1;if(nootherTea.no)return1;return0;}}packagecn.net.sdkd;importjava.util.Arrays;importcn.sd.*;publicclassmain{publicstaticvoidmain(String[]args){Teachert1=newTeacher(0001,白玉,28,信息);Teachert2=newTeacher(0002,李兰馨,36,地科);Teachert3=newTeacher(0003,张成宇,40,材料);Teacher[]TeaArray=newTeacher[3];TeaArray[0]=newTeacher(1001,白玉,28,信息);TeaArray[1]=newTeacher(1005,李兰馨,42,地科);TeaArray[2]=newTeacher(1003,张成宇,40,材料);System.out.println(t1.toString());System.out.println(t2.toString());System.out.println(t3.toString());System.out.print(\n);t1.setNo(t2.getNo());System.out.println(***临时修改教师t3的信息***);System.out.println(教师t1与教师t2是否相同?);System.out.println(t1.equals(t2));System.out.println(教师1与教师t3是否相同?);System.out.println(t1.equals(t3));System.out.print(\n);System.out.println(按编号升序排列:);Arrays.sort(TeaArray);for(inti=0;iTeaArray.length;i++){System.out.print(编号为+TeaArray[i].getNo()+、姓名为+TeaArray[i].getName()+、年龄为+TeaArray[i].getAge()+的+TeaArray[i].getSeminary()+学院老师);System.out.println();}System.out.println();System.out.println(年龄为28的教师的信息:);System.out.println(TeacherManagement.search(28,TeaArray));}}4.一个公司有三种不同类型的员工,他们的薪水分别按年(计算方法:年薪*工作年数)、按月(计算方法:月薪*工作月数)、按周(计算方法:周薪*工作周数)结算。编写类Company,提供计算所有员工总薪水的方法getEarnings,该方法能够根据输入的一组员工(包含各类员工)返回这组员工的总薪水。packagepay;publicclasspay{publicintwages;publicintday;publicpay(intwages,intday){this.wages=wages;this.day=day;}publicpay(){}publicintsh
本文标题:JAVA实验2复习
链接地址:https://www.777doc.com/doc-2880935 .html